]>
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 | ||
57daa971 JF |
269 | static CGFloat CYStatusBarHeight(UIInterfaceOrientation orientation) { |
270 | CGSize size([[UIApplication sharedApplication] statusBarFrame].size); | |
271 | return UIInterfaceOrientationIsPortrait(orientation) ? size.height : size.width; | |
272 | } | |
273 | ||
68f1828e | 274 | /* NSForcedOrderingSearch doesn't work on the iPhone */ |
808c6eb6 | 275 | static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; |
677b8415 JF |
276 | static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; |
277 | static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareCaseInsensitive | kCFCompareNonliteral | kCFCompareLocalized | kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; | |
1e7a90f5 | 278 | |
eef4ccaf JF |
279 | /* Insertion Sort {{{ */ |
280 | ||
df213583 JF |
281 | CFIndex SKBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
282 | const char *ptr = (const char *)list; | |
283 | while (0 < count) { | |
284 | CFIndex half = count / 2; | |
285 | const char *probe = ptr + elementSize * half; | |
286 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 287 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
df213583 JF |
288 | ptr = (cr < 0) ? ptr : probe + elementSize; |
289 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
290 | } | |
291 | return (ptr - (const char *)list) / elementSize; | |
292 | } | |
293 | ||
eef4ccaf JF |
294 | CFIndex CFBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
295 | const char *ptr = (const char *)list; | |
296 | while (0 < count) { | |
297 | CFIndex half = count / 2; | |
298 | const char *probe = ptr + elementSize * half; | |
299 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 300 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
eef4ccaf JF |
301 | ptr = (cr < 0) ? ptr : probe + elementSize; |
302 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
303 | } | |
304 | return (ptr - (const char *)list) / elementSize; | |
305 | } | |
306 | ||
307 | void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFComparatorFunction comparator, void *context) { | |
308 | if (range.length == 0) | |
309 | return; | |
310 | const void **values(new const void *[range.length]); | |
311 | CFArrayGetValues(array, range, values); | |
312 | ||
1539387a | 313 | #if HistogramInsertionSort > 0 |
df213583 JF |
314 | uint32_t total(0), *offsets(new uint32_t[range.length]); |
315 | #endif | |
316 | ||
eef4ccaf JF |
317 | for (CFIndex index(1); index != range.length; ++index) { |
318 | const void *value(values[index]); | |
df213583 JF |
319 | //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); |
320 | CFIndex correct(index); | |
1539387a JF |
321 | while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { |
322 | #if HistogramInsertionSort > 1 | |
323 | NSLog(@"%@ < %@", value, values[correct - 1]); | |
324 | #endif | |
df213583 JF |
325 | if (--correct == 0) |
326 | break; | |
1539387a | 327 | } |
eef4ccaf | 328 | if (correct != index) { |
df213583 JF |
329 | size_t offset(index - correct); |
330 | #if HistogramInsertionSort | |
331 | total += offset; | |
332 | ++offsets[offset]; | |
333 | if (offset > 10) | |
334 | NSLog(@"Heavy Insertion Displacement: %u = %@", offset, value); | |
335 | #endif | |
336 | memmove(values + correct + 1, values + correct, sizeof(const void *) * offset); | |
eef4ccaf JF |
337 | values[correct] = value; |
338 | } | |
339 | } | |
340 | ||
341 | CFArrayReplaceValues(array, range, values, range.length); | |
342 | delete [] values; | |
df213583 | 343 | |
1539387a | 344 | #if HistogramInsertionSort > 0 |
df213583 JF |
345 | for (CFIndex index(0); index != range.length; ++index) |
346 | if (offsets[index] != 0) | |
347 | NSLog(@"Insertion Displacement [%u]: %u", index, offsets[index]); | |
348 | NSLog(@"Average Insertion Displacement: %f", double(total) / range.length); | |
349 | delete [] offsets; | |
350 | #endif | |
eef4ccaf JF |
351 | } |
352 | ||
807ae6d7 JF |
353 | /* }}} */ |
354 | ||
bf8476c8 JF |
355 | /* Apple Bug Fixes {{{ */ |
356 | @implementation UIWebDocumentView (Cydia) | |
357 | ||
358 | - (void) _setScrollerOffset:(CGPoint)offset { | |
359 | UIScroller *scroller([self _scroller]); | |
360 | ||
361 | CGSize size([scroller contentSize]); | |
362 | CGSize bounds([scroller bounds].size); | |
363 | ||
364 | CGPoint max; | |
365 | max.x = size.width - bounds.width; | |
366 | max.y = size.height - bounds.height; | |
367 | ||
368 | // wtf Apple?! | |
369 | if (max.x < 0) | |
370 | max.x = 0; | |
371 | if (max.y < 0) | |
372 | max.y = 0; | |
373 | ||
374 | offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; | |
375 | offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; | |
376 | ||
377 | [scroller setOffset:offset]; | |
378 | } | |
379 | ||
380 | @end | |
381 | /* }}} */ | |
382 | ||
680eb135 JF |
383 | NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { |
384 | size_t length([self length] - state->state); | |
385 | if (length <= 0) | |
386 | return 0; | |
387 | else if (length > count) | |
388 | length = count; | |
389 | for (size_t i(0); i != length; ++i) | |
390 | objects[i] = [self item:state->state++]; | |
391 | state->itemsPtr = objects; | |
392 | state->mutationsPtr = (unsigned long *) self; | |
393 | return length; | |
394 | } | |
395 | ||
bfc87a4d | 396 | /* Cydia NSString Additions {{{ */ |
2388b078 | 397 | @interface NSString (Cydia) |
a54b1c10 | 398 | - (NSComparisonResult) compareByPath:(NSString *)other; |
2fc76a2d JF |
399 | - (NSString *) stringByCachingURLWithCurrentCDN; |
400 | - (NSString *) stringByAddingPercentEscapesIncludingReserved; | |
2388b078 JF |
401 | @end |
402 | ||
449ef9d5 JF |
403 | @implementation NSString (Cydia) |
404 | ||
a54b1c10 JF |
405 | - (NSComparisonResult) compareByPath:(NSString *)other { |
406 | NSString *prefix = [self commonPrefixWithString:other options:0]; | |
407 | size_t length = [prefix length]; | |
408 | ||
409 | NSRange lrange = NSMakeRange(length, [self length] - length); | |
410 | NSRange rrange = NSMakeRange(length, [other length] - length); | |
411 | ||
412 | lrange = [self rangeOfString:@"/" options:0 range:lrange]; | |
413 | rrange = [other rangeOfString:@"/" options:0 range:rrange]; | |
414 | ||
415 | NSComparisonResult value; | |
416 | ||
417 | if (lrange.location == NSNotFound && rrange.location == NSNotFound) | |
418 | value = NSOrderedSame; | |
419 | else if (lrange.location == NSNotFound) | |
420 | value = NSOrderedAscending; | |
421 | else if (rrange.location == NSNotFound) | |
422 | value = NSOrderedDescending; | |
423 | else | |
424 | value = NSOrderedSame; | |
425 | ||
426 | NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] : | |
427 | [self substringWithRange:NSMakeRange(length, lrange.location - length)]; | |
428 | NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] : | |
429 | [other substringWithRange:NSMakeRange(length, rrange.location - length)]; | |
430 | ||
431 | NSComparisonResult result = [lpath compare:rpath]; | |
432 | return result == NSOrderedSame ? value : result; | |
433 | } | |
434 | ||
2fc76a2d JF |
435 | - (NSString *) stringByCachingURLWithCurrentCDN { |
436 | return [self | |
65c27c56 JF |
437 | stringByReplacingOccurrencesOfString:@"://cydia.saurik.com/" |
438 | withString:@"://cache.cydia.saurik.com/" | |
2fc76a2d JF |
439 | ]; |
440 | } | |
441 | ||
442 | - (NSString *) stringByAddingPercentEscapesIncludingReserved { | |
443 | return [(id)CFURLCreateStringByAddingPercentEscapes( | |
bc11cf5b | 444 | kCFAllocatorDefault, |
2fc76a2d JF |
445 | (CFStringRef) self, |
446 | NULL, | |
447 | CFSTR(";/?:@&=+$,"), | |
448 | kCFStringEncodingUTF8 | |
449 | ) autorelease]; | |
450 | } | |
451 | ||
2388b078 | 452 | @end |
bfc87a4d | 453 | /* }}} */ |
2388b078 | 454 | |
bfc87a4d | 455 | /* C++ NSString Wrapper Cache {{{ */ |
8e8fca7f JF |
456 | static _finline CFStringRef CYStringCreate(const char *data, size_t size) { |
457 | return size == 0 ? NULL : | |
458 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingUTF8, NO, kCFAllocatorNull) ?: | |
459 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingISOLatin1, NO, kCFAllocatorNull); | |
460 | } | |
461 | ||
462 | static _finline CFStringRef CYStringCreate(const char *data) { | |
463 | return CYStringCreate(data, strlen(data)); | |
464 | } | |
465 | ||
f79a4512 JF |
466 | class CYString { |
467 | private: | |
468 | char *data_; | |
469 | size_t size_; | |
470 | CFStringRef cache_; | |
471 | ||
472 | _finline void clear_() { | |
18873623 | 473 | if (cache_ != NULL) { |
f79a4512 | 474 | CFRelease(cache_); |
18873623 JF |
475 | cache_ = NULL; |
476 | } | |
f79a4512 JF |
477 | } |
478 | ||
479 | public: | |
480 | _finline bool empty() const { | |
481 | return size_ == 0; | |
482 | } | |
483 | ||
484 | _finline size_t size() const { | |
485 | return size_; | |
486 | } | |
487 | ||
488 | _finline char *data() const { | |
489 | return data_; | |
490 | } | |
491 | ||
492 | _finline void clear() { | |
493 | size_ = 0; | |
494 | clear_(); | |
495 | } | |
496 | ||
497 | _finline CYString() : | |
498 | data_(0), | |
499 | size_(0), | |
18873623 | 500 | cache_(NULL) |
f79a4512 JF |
501 | { |
502 | } | |
503 | ||
504 | _finline ~CYString() { | |
505 | clear_(); | |
506 | } | |
507 | ||
508 | void operator =(const CYString &rhs) { | |
509 | data_ = rhs.data_; | |
510 | size_ = rhs.size_; | |
511 | ||
512 | if (rhs.cache_ == nil) | |
513 | cache_ = NULL; | |
514 | else | |
515 | cache_ = reinterpret_cast<CFStringRef>(CFRetain(rhs.cache_)); | |
516 | } | |
517 | ||
97814287 JF |
518 | void copy(apr_pool_t *pool) { |
519 | char *temp(reinterpret_cast<char *>(apr_palloc(pool, size_ + 1))); | |
520 | memcpy(temp, data_, size_); | |
521 | temp[size_] = '\0'; | |
522 | data_ = temp; | |
523 | } | |
524 | ||
f79a4512 JF |
525 | void set(apr_pool_t *pool, const char *data, size_t size) { |
526 | if (size == 0) | |
527 | clear(); | |
528 | else { | |
529 | clear_(); | |
530 | ||
97814287 | 531 | data_ = const_cast<char *>(data); |
f79a4512 | 532 | size_ = size; |
97814287 JF |
533 | |
534 | if (pool != NULL) | |
535 | copy(pool); | |
f79a4512 JF |
536 | } |
537 | } | |
538 | ||
539 | _finline void set(apr_pool_t *pool, const char *data) { | |
540 | set(pool, data, data == NULL ? 0 : strlen(data)); | |
541 | } | |
542 | ||
543 | _finline void set(apr_pool_t *pool, const std::string &rhs) { | |
544 | set(pool, rhs.data(), rhs.size()); | |
545 | } | |
546 | ||
547 | bool operator ==(const CYString &rhs) const { | |
548 | return size_ == rhs.size_ && memcmp(data_, rhs.data_, size_) == 0; | |
549 | } | |
550 | ||
8e8fca7f JF |
551 | _finline operator CFStringRef() { |
552 | if (cache_ == NULL) | |
553 | cache_ = CYStringCreate(data_, size_); | |
554 | return cache_; | |
df213583 JF |
555 | } |
556 | ||
557 | _finline operator id() { | |
558 | return (NSString *) static_cast<CFStringRef>(*this); | |
f79a4512 | 559 | } |
4c0ed943 JF |
560 | |
561 | _finline operator const char *() { | |
562 | return reinterpret_cast<const char *>(data_); | |
563 | } | |
f79a4512 | 564 | }; |
bfc87a4d JF |
565 | /* }}} */ |
566 | /* C++ NSString Algorithm Adapters {{{ */ | |
f79a4512 JF |
567 | extern "C" { |
568 | CF_EXPORT CFHashCode CFStringHashNSString(CFStringRef str); | |
569 | } | |
570 | ||
571 | struct NSStringMapHash : | |
572 | std::unary_function<NSString *, size_t> | |
573 | { | |
574 | _finline size_t operator ()(NSString *value) const { | |
575 | return CFStringHashNSString((CFStringRef) value); | |
576 | } | |
577 | }; | |
578 | ||
579 | struct NSStringMapLess : | |
580 | std::binary_function<NSString *, NSString *, bool> | |
581 | { | |
582 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
583 | return [lhs compare:rhs] == NSOrderedAscending; | |
584 | } | |
585 | }; | |
586 | ||
587 | struct NSStringMapEqual : | |
588 | std::binary_function<NSString *, NSString *, bool> | |
589 | { | |
590 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
591 | return CFStringCompare((CFStringRef) lhs, (CFStringRef) rhs, 0) == kCFCompareEqualTo; | |
592 | //CFEqual((CFTypeRef) lhs, (CFTypeRef) rhs); | |
593 | //[lhs isEqualToString:rhs]; | |
594 | } | |
595 | }; | |
bfc87a4d | 596 | /* }}} */ |
f79a4512 | 597 | |
5f6bff8c | 598 | /* CoreGraphics Primitives {{{ */ |
02012733 | 599 | class CYColor { |
b4d89997 JF |
600 | private: |
601 | CGColorRef color_; | |
602 | ||
6a575b5e JF |
603 | static CGColorRef Create_(CGColorSpaceRef space, float red, float green, float blue, float alpha) { |
604 | CGFloat color[] = {red, green, blue, alpha}; | |
605 | return CGColorCreate(space, color); | |
606 | } | |
607 | ||
b4d89997 | 608 | public: |
02012733 | 609 | CYColor() : |
36bb2ca2 JF |
610 | color_(NULL) |
611 | { | |
612 | } | |
613 | ||
02012733 | 614 | CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) : |
6a575b5e | 615 | color_(Create_(space, red, green, blue, alpha)) |
36bb2ca2 JF |
616 | { |
617 | Set(space, red, green, blue, alpha); | |
618 | } | |
619 | ||
620 | void Clear() { | |
621 | if (color_ != NULL) | |
622 | CGColorRelease(color_); | |
b4d89997 JF |
623 | } |
624 | ||
02012733 | 625 | ~CYColor() { |
36bb2ca2 JF |
626 | Clear(); |
627 | } | |
628 | ||
629 | void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) { | |
630 | Clear(); | |
6a575b5e | 631 | color_ = Create_(space, red, green, blue, alpha); |
b4d89997 JF |
632 | } |
633 | ||
634 | operator CGColorRef() { | |
635 | return color_; | |
636 | } | |
637 | }; | |
b4d89997 JF |
638 | /* }}} */ |
639 | ||
36bb2ca2 | 640 | /* Random Global Variables {{{ */ |
3178d79b | 641 | static const int PulseInterval_ = 50000; |
affeffc7 | 642 | |
57e8b225 JF |
643 | static const NSString *UI_; |
644 | ||
d791dce4 | 645 | static int Finish_; |
be860cc8 | 646 | static bool RestartSubstrate_; |
d791dce4 JF |
647 | static NSArray *Finishes_; |
648 | ||
affeffc7 | 649 | #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist" |
22f8bed9 | 650 | #define NotifyConfig_ "/etc/notify.conf" |
2a8d9add | 651 | |
dc63e78f JF |
652 | static bool Queuing_; |
653 | ||
02012733 JF |
654 | static CYColor Blue_; |
655 | static CYColor Blueish_; | |
656 | static CYColor Black_; | |
657 | static CYColor Off_; | |
658 | static CYColor White_; | |
659 | static CYColor Gray_; | |
660 | static CYColor Green_; | |
661 | static CYColor Purple_; | |
662 | static CYColor Purplish_; | |
3bd1c2a2 | 663 | |
dc63e78f JF |
664 | static UIColor *InstallingColor_; |
665 | static UIColor *RemovingColor_; | |
36bb2ca2 | 666 | |
7d2ac47f | 667 | static NSString *App_; |
d73cede2 | 668 | |
2388b078 | 669 | static BOOL Advanced_; |
a54b1c10 | 670 | static BOOL Ignored_; |
2388b078 | 671 | |
7b33d201 JF |
672 | static _H<UIFont> Font12_; |
673 | static _H<UIFont> Font12Bold_; | |
674 | static _H<UIFont> Font14_; | |
675 | static _H<UIFont> Font18Bold_; | |
676 | static _H<UIFont> Font22Bold_; | |
f641a0e5 | 677 | |
87f46a96 | 678 | static const char *Machine_ = NULL; |
f4b1c57f | 679 | static NSString *System_ = nil; |
56127854 JF |
680 | static NSString *SerialNumber_ = nil; |
681 | static NSString *ChipID_ = nil; | |
6ffdaae3 | 682 | static NSString *BBSNum_ = nil; |
9737d93e | 683 | static _H<NSString> Token_; |
56127854 | 684 | static NSString *UniqueID_ = nil; |
567e3972 | 685 | static NSString *PLMN_ = nil; |
56127854 JF |
686 | static NSString *Build_ = nil; |
687 | static NSString *Product_ = nil; | |
688 | static NSString *Safari_ = nil; | |
2a8d9add | 689 | |
d791dce4 JF |
690 | static CFLocaleRef Locale_; |
691 | static NSArray *Languages_; | |
692 | static CGColorSpaceRef space_; | |
36bb2ca2 | 693 | |
46dbfd32 | 694 | static NSDictionary *SectionMap_; |
b4d89997 | 695 | static NSMutableDictionary *Metadata_; |
7b0ce2da JF |
696 | static _transient NSMutableDictionary *Settings_; |
697 | static _transient NSString *Role_; | |
698 | static _transient NSMutableDictionary *Packages_; | |
699 | static _transient NSMutableDictionary *Sections_; | |
700 | static _transient NSMutableDictionary *Sources_; | |
bbb879fb | 701 | static bool Changed_; |
31bc18a7 | 702 | static time_t now_; |
8da60fb7 | 703 | |
f333f6c5 | 704 | bool IsWildcat_; |
57e8b225 | 705 | static CGFloat ScreenScale_; |
c138614d | 706 | static NSString *Idiom_; |
5df7ecfb | 707 | |
7b33d201 JF |
708 | static _H<NSMutableDictionary> SessionData_; |
709 | static _H<NSObject> HostConfig_; | |
710 | static _H<NSMutableSet> BridgedHosts_; | |
711 | static _H<NSMutableSet> PipelinedHosts_; | |
5e845121 | 712 | static _H<NSMutableSet> CachedURLs_; |
389133be | 713 | |
e4123ce0 JF |
714 | static NSString *kCydiaProgressEventTypeError = @"Error"; |
715 | static NSString *kCydiaProgressEventTypeInformation = @"Information"; | |
716 | static NSString *kCydiaProgressEventTypeStatus = @"Status"; | |
717 | static NSString *kCydiaProgressEventTypeWarning = @"Warning"; | |
36bb2ca2 | 718 | /* }}} */ |
d791dce4 | 719 | |
36bb2ca2 JF |
720 | /* Display Helpers {{{ */ |
721 | inline float Interpolate(float begin, float end, float fraction) { | |
722 | return (end - begin) * fraction + begin; | |
723 | } | |
2367a917 | 724 | |
1c1dfc2d | 725 | static _finline const char *StripVersion_(const char *version) { |
6a155117 | 726 | const char *colon(strchr(version, ':')); |
673ad3c3 | 727 | return colon == NULL ? version : colon + 1; |
6a155117 JF |
728 | } |
729 | ||
f79a4512 | 730 | NSString *LocalizeSection(NSString *section) { |
b1ce61ec | 731 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
9fcbca29 JF |
732 | if (title_r(section)) { |
733 | NSString *parent(title_r[1]); | |
734 | NSString *child(title_r[2]); | |
735 | ||
43f3d7f6 | 736 | return [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), |
9fcbca29 JF |
737 | LocalizeSection(parent), |
738 | LocalizeSection(child) | |
b1ce61ec | 739 | ]; |
9fcbca29 | 740 | } |
b1ce61ec JF |
741 | |
742 | return [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
f79a4512 JF |
743 | } |
744 | ||
4cf4165e JF |
745 | NSString *Simplify(NSString *title) { |
746 | const char *data = [title UTF8String]; | |
747 | size_t size = [title length]; | |
748 | ||
7b0ce2da JF |
749 | static Pcre square_r("^\\[(.*)\\]$"); |
750 | if (square_r(data, size)) | |
751 | return Simplify(square_r[1]); | |
752 | ||
753 | static Pcre paren_r("^\\((.*)\\)$"); | |
754 | if (paren_r(data, size)) | |
755 | return Simplify(paren_r[1]); | |
756 | ||
b1ce61ec | 757 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
4cf4165e | 758 | if (title_r(data, size)) |
7b0ce2da JF |
759 | return Simplify(title_r[1]); |
760 | ||
761 | return title; | |
4cf4165e | 762 | } |
36bb2ca2 JF |
763 | /* }}} */ |
764 | ||
d791dce4 JF |
765 | NSString *GetLastUpdate() { |
766 | NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]; | |
767 | ||
768 | if (update == nil) | |
769 | return UCLocalize("NEVER_OR_UNKNOWN"); | |
770 | ||
771 | CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); | |
772 | CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update); | |
773 | ||
774 | CFRelease(formatter); | |
775 | ||
776 | return [(NSString *) formatted autorelease]; | |
777 | } | |
778 | ||
6d9712c4 | 779 | bool isSectionVisible(NSString *section) { |
45447dc3 | 780 | NSDictionary *metadata([Sections_ objectForKey:(section ?: @"")]); |
677b8415 | 781 | NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); |
6d9712c4 JF |
782 | return hidden == nil || ![hidden boolValue]; |
783 | } | |
784 | ||
c31d7cdc | 785 | static NSObject *CYIOGetValue(const char *path, NSString *property) { |
947a8eef JF |
786 | io_registry_entry_t entry(IORegistryEntryFromPath(kIOMasterPortDefault, path)); |
787 | if (entry == MACH_PORT_NULL) | |
788 | return nil; | |
789 | ||
790 | CFTypeRef value(IORegistryEntryCreateCFProperty(entry, (CFStringRef) property, kCFAllocatorDefault, 0)); | |
791 | IOObjectRelease(entry); | |
792 | ||
793 | if (value == NULL) | |
794 | return nil; | |
795 | return [(id) value autorelease]; | |
796 | } | |
797 | ||
c31d7cdc | 798 | static NSString *CYHex(NSData *data, bool reverse = false) { |
947a8eef JF |
799 | if (data == nil) |
800 | return nil; | |
801 | ||
802 | size_t length([data length]); | |
803 | uint8_t bytes[length]; | |
804 | [data getBytes:bytes]; | |
805 | ||
806 | char string[length * 2 + 1]; | |
807 | for (size_t i(0); i != length; ++i) | |
be45a862 | 808 | sprintf(string + i * 2, "%.2x", bytes[reverse ? length - i - 1 : i]); |
947a8eef JF |
809 | |
810 | return [NSString stringWithUTF8String:string]; | |
811 | } | |
812 | ||
9cb0bff2 GP |
813 | @class Cydia; |
814 | ||
d36e83a3 | 815 | /* Delegate Prototypes {{{ */ |
36bb2ca2 JF |
816 | @class Package; |
817 | @class Source; | |
6915b806 | 818 | @class CydiaProgressEvent; |
36bb2ca2 | 819 | |
6915b806 | 820 | @protocol DatabaseDelegate |
5a09ae08 | 821 | - (void) repairWithSelector:(SEL)selector; |
8b29f8e6 | 822 | - (void) setConfigurationData:(NSString *)data; |
6915b806 | 823 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task; |
8b29f8e6 JF |
824 | @end |
825 | ||
f6e13561 | 826 | @class CYPackageController; |
f79a4512 | 827 | |
36bb2ca2 | 828 | @protocol CydiaDelegate |
54043703 JF |
829 | - (void) retainNetworkActivityIndicator; |
830 | - (void) releaseNetworkActivityIndicator; | |
dc63e78f | 831 | - (void) clearPackage:(Package *)package; |
36bb2ca2 | 832 | - (void) installPackage:(Package *)package; |
77801ff1 | 833 | - (void) installPackages:(NSArray *)packages; |
36bb2ca2 | 834 | - (void) removePackage:(Package *)package; |
c21004b9 JF |
835 | - (void) beginUpdate; |
836 | - (BOOL) updating; | |
36bb2ca2 | 837 | - (void) distUpgrade; |
fed0d010 | 838 | - (void) loadData; |
6d9712c4 | 839 | - (void) updateData; |
7b0ce2da | 840 | - (void) syncData; |
93460555 | 841 | - (void) addTrivialSource:(NSString *)href; |
21c6da4b | 842 | - (void) showSettings; |
7b0ce2da | 843 | - (UIProgressHUD *) addProgressHUD; |
d061f4ba | 844 | - (void) removeProgressHUD:(UIProgressHUD *)hud; |
cd79e8cf | 845 | - (CyteViewController *) pageForPackage:(NSString *)name; |
9daa7f25 | 846 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; |
4ba8f30a | 847 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 848 | @end |
d36e83a3 | 849 | /* }}} */ |
b4d89997 | 850 | |
dc5812ec JF |
851 | /* Status Delegation {{{ */ |
852 | class Status : | |
853 | public pkgAcquireStatus | |
854 | { | |
855 | private: | |
31f3cfff | 856 | _transient NSObject<ProgressDelegate> *delegate_; |
fc470c15 | 857 | bool cancelled_; |
dc5812ec JF |
858 | |
859 | public: | |
860 | Status() : | |
fc470c15 JF |
861 | delegate_(nil), |
862 | cancelled_(false) | |
dc5812ec JF |
863 | { |
864 | } | |
865 | ||
6915b806 | 866 | void setDelegate(NSObject<ProgressDelegate> *delegate) { |
dc5812ec JF |
867 | delegate_ = delegate; |
868 | } | |
869 | ||
670a0494 JF |
870 | NSObject<ProgressDelegate> *getDelegate() const { |
871 | return delegate_; | |
872 | } | |
873 | ||
dc5812ec JF |
874 | virtual bool MediaChange(std::string media, std::string drive) { |
875 | return false; | |
876 | } | |
877 | ||
878 | virtual void IMSHit(pkgAcquire::ItemDesc &item) { | |
dc5812ec JF |
879 | } |
880 | ||
881 | virtual void Fetch(pkgAcquire::ItemDesc &item) { | |
6915b806 | 882 | NSString *name([NSString stringWithUTF8String:item.ShortDesc.c_str()]); |
389133be | 883 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), name] ofType:kCydiaProgressEventTypeStatus forItem:item]); |
6915b806 | 884 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
885 | } |
886 | ||
887 | virtual void Done(pkgAcquire::ItemDesc &item) { | |
dc5812ec JF |
888 | } |
889 | ||
890 | virtual void Fail(pkgAcquire::ItemDesc &item) { | |
1d80f6b9 JF |
891 | if ( |
892 | item.Owner->Status == pkgAcquire::Item::StatIdle || | |
893 | item.Owner->Status == pkgAcquire::Item::StatDone | |
894 | ) | |
895 | return; | |
896 | ||
affeffc7 JF |
897 | std::string &error(item.Owner->ErrorText); |
898 | if (error.empty()) | |
899 | return; | |
900 | ||
389133be | 901 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError forItem:item]); |
6915b806 | 902 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
903 | } |
904 | ||
905 | virtual bool Pulse(pkgAcquire *Owner) { | |
2367a917 JF |
906 | bool value = pkgAcquireStatus::Pulse(Owner); |
907 | ||
bcbac8f7 | 908 | double percent( |
2367a917 JF |
909 | double(CurrentBytes + CurrentItems) / |
910 | double(TotalBytes + TotalItems) | |
911 | ); | |
912 | ||
bcbac8f7 JF |
913 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: |
914 | [NSNumber numberWithDouble:percent], @"Percent", | |
915 | ||
916 | [NSNumber numberWithDouble:CurrentBytes], @"Current", | |
917 | [NSNumber numberWithDouble:TotalBytes], @"Total", | |
918 | [NSNumber numberWithDouble:CurrentCPS], @"Speed", | |
919 | nil] waitUntilDone:YES]; | |
920 | ||
fc470c15 JF |
921 | if (value && ![delegate_ isProgressCancelled]) |
922 | return true; | |
923 | else { | |
924 | cancelled_ = true; | |
925 | return false; | |
926 | } | |
927 | } | |
928 | ||
929 | _finline bool WasCancelled() const { | |
930 | return cancelled_; | |
dc5812ec JF |
931 | } |
932 | ||
933 | virtual void Start() { | |
0210c2b5 | 934 | pkgAcquireStatus::Start(); |
aaae308d | 935 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES]; |
dc5812ec JF |
936 | } |
937 | ||
938 | virtual void Stop() { | |
0210c2b5 | 939 | pkgAcquireStatus::Stop(); |
aaae308d | 940 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:YES]; |
bcbac8f7 | 941 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:nil waitUntilDone:YES]; |
dc5812ec JF |
942 | } |
943 | }; | |
944 | /* }}} */ | |
36bb2ca2 | 945 | /* Database Interface {{{ */ |
68d927e2 JF |
946 | typedef std::map< unsigned long, _H<Source> > SourceMap; |
947 | ||
36bb2ca2 | 948 | @interface Database : NSObject { |
f79a4512 JF |
949 | NSZone *zone_; |
950 | apr_pool_t *pool_; | |
951 | ||
a1440b10 JF |
952 | unsigned era_; |
953 | ||
36bb2ca2 | 954 | pkgCacheFile cache_; |
5a09ae08 | 955 | pkgDepCache::Policy *policy_; |
36bb2ca2 JF |
956 | pkgRecords *records_; |
957 | pkgProblemResolver *resolver_; | |
958 | pkgAcquire *fetcher_; | |
959 | FileFd *lock_; | |
960 | SPtr<pkgPackageManager> manager_; | |
961 | pkgSourceList *list_; | |
5f6bff8c | 962 | |
34f70f5d | 963 | SourceMap sourceMap_; |
7b33d201 | 964 | _H<NSMutableArray> sourceList_; |
34f70f5d | 965 | |
077e9d90 | 966 | CFMutableArrayRef packages_; |
b4d89997 | 967 | |
6915b806 JF |
968 | _transient NSObject<DatabaseDelegate> *delegate_; |
969 | _transient NSObject<ProgressDelegate> *progress_; | |
970 | ||
36bb2ca2 | 971 | Status status_; |
8b29f8e6 | 972 | |
77fcccaf | 973 | int cydiafd_; |
36bb2ca2 | 974 | int statusfd_; |
8b29f8e6 | 975 | FILE *input_; |
fe33a23e JF |
976 | |
977 | std::map<const char *, _H<NSString> > sections_; | |
dc5812ec JF |
978 | } |
979 | ||
770f2a8e | 980 | + (Database *) sharedInstance; |
a1440b10 | 981 | - (unsigned) era; |
770f2a8e | 982 | |
77fcccaf | 983 | - (void) _readCydia:(NSNumber *)fd; |
36bb2ca2 JF |
984 | - (void) _readStatus:(NSNumber *)fd; |
985 | - (void) _readOutput:(NSNumber *)fd; | |
2367a917 | 986 | |
8b29f8e6 JF |
987 | - (FILE *) input; |
988 | ||
36bb2ca2 | 989 | - (Package *) packageWithName:(NSString *)name; |
dc5812ec | 990 | |
36bb2ca2 | 991 | - (pkgCacheFile &) cache; |
5a09ae08 | 992 | - (pkgDepCache::Policy *) policy; |
36bb2ca2 JF |
993 | - (pkgRecords *) records; |
994 | - (pkgProblemResolver *) resolver; | |
995 | - (pkgAcquire &) fetcher; | |
a3328c28 | 996 | - (pkgSourceList &) list; |
36bb2ca2 | 997 | - (NSArray *) packages; |
7b0ce2da | 998 | - (NSArray *) sources; |
d669236d | 999 | - (Source *) sourceWithKey:(NSString *)key; |
4ba8f30a | 1000 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 1001 | |
5a09ae08 | 1002 | - (void) configure; |
670a0494 | 1003 | - (bool) prepare; |
36bb2ca2 | 1004 | - (void) perform; |
670a0494 | 1005 | - (bool) upgrade; |
36bb2ca2 JF |
1006 | - (void) update; |
1007 | ||
670a0494 | 1008 | - (void) updateWithStatus:(Status &)status; |
36bb2ca2 | 1009 | |
6915b806 JF |
1010 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate; |
1011 | ||
1012 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate; | |
1013 | - (NSObject<ProgressDelegate> *) progressDelegate; | |
1014 | ||
7376b55c | 1015 | - (Source *) getSource:(pkgCache::PkgFileIterator)file; |
fe33a23e JF |
1016 | |
1017 | - (NSString *) mappedSectionForPointer:(const char *)pointer; | |
1018 | ||
670a0494 JF |
1019 | @end |
1020 | /* }}} */ | |
6915b806 JF |
1021 | /* ProgressEvent Implementation {{{ */ |
1022 | @implementation CydiaProgressEvent | |
670a0494 | 1023 | |
6915b806 JF |
1024 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type { |
1025 | return [[[CydiaProgressEvent alloc] initWithMessage:message ofType:type] autorelease]; | |
670a0494 JF |
1026 | } |
1027 | ||
6915b806 JF |
1028 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forPackage:(NSString *)package { |
1029 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); | |
1030 | [event setPackage:package]; | |
1031 | return event; | |
670a0494 JF |
1032 | } |
1033 | ||
c57867ea JF |
1034 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forItem:(pkgAcquire::ItemDesc &)item { |
1035 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); | |
1036 | ||
1037 | NSString *description([NSString stringWithUTF8String:item.Description.c_str()]); | |
1038 | NSArray *fields([description componentsSeparatedByString:@" "]); | |
1039 | [event setItem:fields]; | |
1040 | ||
1041 | if ([fields count] > 3) { | |
1042 | [event setPackage:[fields objectAtIndex:2]]; | |
1043 | [event setVersion:[fields objectAtIndex:3]]; | |
1044 | } | |
1045 | ||
1046 | [event setURL:[NSString stringWithUTF8String:item.URI.c_str()]]; | |
1047 | ||
1048 | return event; | |
1049 | } | |
1050 | ||
4ede7a3f JF |
1051 | + (NSArray *) _attributeKeys { |
1052 | return [NSArray arrayWithObjects: | |
c57867ea | 1053 | @"item", |
4ede7a3f JF |
1054 | @"message", |
1055 | @"package", | |
1056 | @"type", | |
c57867ea JF |
1057 | @"url", |
1058 | @"version", | |
4ede7a3f JF |
1059 | nil]; |
1060 | } | |
1061 | ||
1062 | - (NSArray *) attributeKeys { | |
1063 | return [[self class] _attributeKeys]; | |
1064 | } | |
1065 | ||
1066 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1067 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1068 | } | |
1069 | ||
6915b806 JF |
1070 | - (id) initWithMessage:(NSString *)message ofType:(NSString *)type { |
1071 | if ((self = [super init]) != nil) { | |
1072 | message_ = message; | |
1073 | type_ = type; | |
1074 | } return self; | |
670a0494 JF |
1075 | } |
1076 | ||
6915b806 JF |
1077 | - (NSString *) message { |
1078 | return message_; | |
1079 | } | |
6067f1b8 | 1080 | |
6915b806 JF |
1081 | - (NSString *) type { |
1082 | return type_; | |
1083 | } | |
1084 | ||
c57867ea JF |
1085 | - (NSArray *) item { |
1086 | return (id) item_ ?: [NSNull null]; | |
1087 | } | |
1088 | ||
1089 | - (void) setItem:(NSArray *)item { | |
1090 | item_ = item; | |
6915b806 JF |
1091 | } |
1092 | ||
c57867ea JF |
1093 | - (NSString *) package { |
1094 | return (id) package_ ?: [NSNull null]; | |
6915b806 JF |
1095 | } |
1096 | ||
1097 | - (void) setPackage:(NSString *)package { | |
1098 | package_ = package; | |
1099 | } | |
1100 | ||
c57867ea JF |
1101 | - (NSString *) url { |
1102 | return (id) url_ ?: [NSNull null]; | |
1103 | } | |
1104 | ||
1105 | - (void) setURL:(NSString *)url { | |
1106 | url_ = url; | |
1107 | } | |
1108 | ||
1109 | - (void) setVersion:(NSString *)version { | |
1110 | version_ = version; | |
1111 | } | |
1112 | ||
1113 | - (NSString *) version { | |
1114 | return (id) version_ ?: [NSNull null]; | |
1115 | } | |
1116 | ||
6915b806 JF |
1117 | - (NSString *) compound:(NSString *)value { |
1118 | if (value != nil) { | |
1119 | NSString *mode(nil); { | |
1120 | NSString *type([self type]); | |
389133be | 1121 | if ([type isEqualToString:kCydiaProgressEventTypeError]) |
6915b806 | 1122 | mode = UCLocalize("ERROR"); |
389133be | 1123 | else if ([type isEqualToString:kCydiaProgressEventTypeWarning]) |
6915b806 JF |
1124 | mode = UCLocalize("WARNING"); |
1125 | } | |
1126 | ||
1127 | if (mode != nil) | |
1128 | value = [NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), mode, value]; | |
1129 | } | |
1130 | ||
1131 | return value; | |
1132 | } | |
1133 | ||
1134 | - (NSString *) compoundMessage { | |
1135 | return [self compound:[self message]]; | |
1136 | } | |
1137 | ||
1138 | - (NSString *) compoundTitle { | |
1139 | NSString *title; | |
1140 | ||
83b78e5f | 1141 | if (package_ == nil) |
6915b806 | 1142 | title = nil; |
83b78e5f JF |
1143 | else if (Package *package = [[Database sharedInstance] packageWithName:package_]) |
1144 | title = [package name]; | |
1145 | else | |
1146 | title = package_; | |
6915b806 JF |
1147 | |
1148 | return [self compound:title]; | |
670a0494 JF |
1149 | } |
1150 | ||
dc5812ec | 1151 | @end |
36bb2ca2 | 1152 | /* }}} */ |
dc5812ec | 1153 | |
94b0b3e5 JF |
1154 | // Cytore Definitions {{{ |
1155 | struct PackageValue : | |
1156 | Cytore::Block | |
1157 | { | |
94b0b3e5 JF |
1158 | Cytore::Offset<PackageValue> next_; |
1159 | ||
1160 | uint32_t index_ : 23; | |
1161 | uint32_t subscribed_ : 1; | |
1162 | uint32_t : 8; | |
1163 | ||
1164 | int32_t first_; | |
1165 | int32_t last_; | |
1166 | ||
1167 | uint16_t vhash_; | |
1168 | uint16_t nhash_; | |
1169 | ||
1170 | char version_[8]; | |
1171 | char name_[]; | |
1172 | }; | |
1173 | ||
1174 | struct MetaValue : | |
1175 | Cytore::Block | |
1176 | { | |
9f357d11 | 1177 | uint32_t active_; |
94b0b3e5 JF |
1178 | Cytore::Offset<PackageValue> packages_[1 << 16]; |
1179 | }; | |
1180 | ||
1181 | static Cytore::File<MetaValue> MetaFile_; | |
1182 | // }}} | |
1183 | // Cytore Helper Functions {{{ | |
c65611b9 | 1184 | static PackageValue *PackageFind(const char *name, size_t length, bool *fail = NULL) { |
94b0b3e5 JF |
1185 | SplitHash nhash = { hashlittle(name, length) }; |
1186 | ||
1187 | PackageValue *metadata; | |
1188 | ||
1189 | Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]); | |
1190 | offset: if (offset->IsNull()) { | |
1191 | *offset = MetaFile_.New<PackageValue>(length + 1); | |
1192 | metadata = &MetaFile_.Get(*offset); | |
1193 | ||
c65611b9 JF |
1194 | if (metadata == NULL) { |
1195 | if (fail != NULL) | |
1196 | *fail = true; | |
1197 | ||
1198 | metadata = new PackageValue(); | |
1199 | memset(metadata, 0, sizeof(*metadata)); | |
1200 | } | |
1201 | ||
94b0b3e5 JF |
1202 | memcpy(metadata->name_, name, length + 1); |
1203 | metadata->nhash_ = nhash.u16[1]; | |
1204 | } else { | |
1205 | metadata = &MetaFile_.Get(*offset); | |
1206 | ||
1207 | if (metadata->nhash_ != nhash.u16[1] || strncmp(metadata->name_, name, length + 1) != 0) { | |
1208 | offset = &metadata->next_; | |
1209 | goto offset; | |
1210 | } | |
1211 | } | |
1212 | ||
94b0b3e5 JF |
1213 | return metadata; |
1214 | } | |
1215 | ||
1216 | static void PackageImport(const void *key, const void *value, void *context) { | |
c65611b9 JF |
1217 | bool &fail(*reinterpret_cast<bool *>(context)); |
1218 | ||
94b0b3e5 JF |
1219 | char buffer[1024]; |
1220 | if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1221 | NSLog(@"failed to import package %@", key); | |
1222 | return; | |
1223 | } | |
1224 | ||
c65611b9 | 1225 | PackageValue *metadata(PackageFind(buffer, strlen(buffer), &fail)); |
94b0b3e5 JF |
1226 | NSDictionary *package((NSDictionary *) value); |
1227 | ||
1228 | if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"]) | |
2f856365 | 1229 | if ([subscribed boolValue] && !metadata->subscribed_) |
94b0b3e5 JF |
1230 | metadata->subscribed_ = true; |
1231 | ||
1232 | if (NSDate *date = [package objectForKey:@"FirstSeen"]) { | |
1233 | time_t time([date timeIntervalSince1970]); | |
1234 | if (metadata->first_ > time || metadata->first_ == 0) | |
1235 | metadata->first_ = time; | |
1236 | } | |
1237 | ||
2f856365 JF |
1238 | NSDate *date([package objectForKey:@"LastSeen"]); |
1239 | NSString *version([package objectForKey:@"LastVersion"]); | |
5a933937 | 1240 | |
2f856365 | 1241 | if (date != nil && version != nil) { |
94b0b3e5 | 1242 | time_t time([date timeIntervalSince1970]); |
2f856365 | 1243 | if (metadata->last_ < time || metadata->last_ == 0) |
5a933937 JF |
1244 | if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { |
1245 | size_t length(strlen(buffer)); | |
1246 | uint16_t vhash(hashlittle(buffer, length)); | |
94b0b3e5 | 1247 | |
5a933937 JF |
1248 | size_t capped(std::min<size_t>(8, length)); |
1249 | char *latest(buffer + length - capped); | |
1250 | ||
1251 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); | |
1252 | metadata->vhash_ = vhash; | |
2f856365 JF |
1253 | |
1254 | metadata->last_ = time; | |
5a933937 | 1255 | } |
2f856365 | 1256 | } |
94b0b3e5 JF |
1257 | } |
1258 | // }}} | |
1259 | ||
36bb2ca2 JF |
1260 | /* Source Class {{{ */ |
1261 | @interface Source : NSObject { | |
6204f56a | 1262 | CYString depiction_; |
f9f6d9e8 JF |
1263 | CYString description_; |
1264 | CYString label_; | |
1265 | CYString origin_; | |
1266 | CYString support_; | |
dc5812ec | 1267 | |
f9f6d9e8 JF |
1268 | CYString uri_; |
1269 | CYString distribution_; | |
1270 | CYString type_; | |
8252b666 | 1271 | CYString base_; |
f9f6d9e8 | 1272 | CYString version_; |
2367a917 | 1273 | |
c08c8943 JF |
1274 | _H<NSString> host_; |
1275 | _H<NSString> authority_; | |
f9f6d9e8 JF |
1276 | |
1277 | CYString defaultIcon_; | |
dc5812ec | 1278 | |
c08c8943 | 1279 | _H<NSDictionary> record_; |
36bb2ca2 | 1280 | BOOL trusted_; |
dc5812ec JF |
1281 | } |
1282 | ||
f9f6d9e8 | 1283 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool; |
dc5812ec | 1284 | |
7b0ce2da JF |
1285 | - (NSComparisonResult) compareByNameAndType:(Source *)source; |
1286 | ||
6204f56a | 1287 | - (NSString *) depictionForPackage:(NSString *)package; |
dc63e78f JF |
1288 | - (NSString *) supportForPackage:(NSString *)package; |
1289 | ||
7b0ce2da | 1290 | - (NSDictionary *) record; |
36bb2ca2 | 1291 | - (BOOL) trusted; |
dc5812ec | 1292 | |
36bb2ca2 JF |
1293 | - (NSString *) uri; |
1294 | - (NSString *) distribution; | |
1295 | - (NSString *) type; | |
8252b666 JF |
1296 | |
1297 | - (NSString *) base; | |
1298 | ||
7b0ce2da JF |
1299 | - (NSString *) key; |
1300 | - (NSString *) host; | |
36bb2ca2 | 1301 | |
7b0ce2da | 1302 | - (NSString *) name; |
8d262908 | 1303 | - (NSString *) shortDescription; |
36bb2ca2 JF |
1304 | - (NSString *) label; |
1305 | - (NSString *) origin; | |
1306 | - (NSString *) version; | |
dc5812ec | 1307 | |
36bb2ca2 | 1308 | - (NSString *) defaultIcon; |
7b0ce2da | 1309 | |
dc5812ec | 1310 | @end |
dc5812ec | 1311 | |
36bb2ca2 | 1312 | @implementation Source |
dc5812ec | 1313 | |
a3328c28 | 1314 | - (void) _clear { |
f9f6d9e8 JF |
1315 | uri_.clear(); |
1316 | distribution_.clear(); | |
1317 | type_.clear(); | |
a3328c28 | 1318 | |
8252b666 JF |
1319 | base_.clear(); |
1320 | ||
f9f6d9e8 JF |
1321 | description_.clear(); |
1322 | label_.clear(); | |
1323 | origin_.clear(); | |
6204f56a | 1324 | depiction_.clear(); |
f9f6d9e8 JF |
1325 | support_.clear(); |
1326 | version_.clear(); | |
1327 | defaultIcon_.clear(); | |
1328 | ||
c08c8943 JF |
1329 | record_ = nil; |
1330 | host_ = nil; | |
1331 | authority_ = nil; | |
a3328c28 | 1332 | } |
dc5812ec | 1333 | |
6f1a15d9 | 1334 | + (NSArray *) _attributeKeys { |
e58ff941 | 1335 | return [NSArray arrayWithObjects: |
e58ff941 JF |
1336 | @"distribution", |
1337 | @"host", | |
1338 | @"key", | |
1339 | @"label", | |
1340 | @"name", | |
1341 | @"origin", | |
8d262908 | 1342 | @"shortDescription", |
e58ff941 JF |
1343 | @"trusted", |
1344 | @"type", | |
1345 | @"uri", | |
1346 | @"version", | |
1347 | nil]; | |
6f1a15d9 JF |
1348 | } |
1349 | ||
1350 | - (NSArray *) attributeKeys { | |
1351 | return [[self class] _attributeKeys]; | |
1352 | } | |
1353 | ||
1354 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1355 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1356 | } | |
1357 | ||
f9f6d9e8 | 1358 | - (void) setMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
a3328c28 | 1359 | [self _clear]; |
dc5812ec | 1360 | |
a3328c28 JF |
1361 | trusted_ = index->IsTrusted(); |
1362 | ||
f9f6d9e8 JF |
1363 | uri_.set(pool, index->GetURI()); |
1364 | distribution_.set(pool, index->GetDist()); | |
1365 | type_.set(pool, index->GetType()); | |
a3328c28 JF |
1366 | |
1367 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index)); | |
1368 | if (dindex != NULL) { | |
8252b666 JF |
1369 | base_.set(pool, dindex->MetaIndexURI("")); |
1370 | ||
18873623 JF |
1371 | FileFd fd; |
1372 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) | |
1373 | _error->Discard(); | |
1374 | else { | |
1375 | pkgTagFile tags(&fd); | |
f9f6d9e8 | 1376 | |
18873623 JF |
1377 | pkgTagSection section; |
1378 | tags.Step(section); | |
a3328c28 | 1379 | |
18873623 JF |
1380 | struct { |
1381 | const char *name_; | |
1382 | CYString *value_; | |
1383 | } names[] = { | |
1384 | {"default-icon", &defaultIcon_}, | |
6204f56a | 1385 | {"depiction", &depiction_}, |
18873623 JF |
1386 | {"description", &description_}, |
1387 | {"label", &label_}, | |
1388 | {"origin", &origin_}, | |
1389 | {"support", &support_}, | |
1390 | {"version", &version_}, | |
1391 | }; | |
f9f6d9e8 | 1392 | |
18873623 JF |
1393 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { |
1394 | const char *start, *end; | |
1395 | ||
1396 | if (section.Find(names[i].name_, start, end)) { | |
1397 | CYString &value(*names[i].value_); | |
1398 | value.set(pool, start, end - start); | |
1399 | } | |
f9f6d9e8 | 1400 | } |
36bb2ca2 | 1401 | } |
a3328c28 | 1402 | } |
7b0ce2da | 1403 | |
a3328c28 | 1404 | record_ = [Sources_ objectForKey:[self key]]; |
f9f6d9e8 | 1405 | |
7623f855 JF |
1406 | NSURL *url([NSURL URLWithString:uri_]); |
1407 | ||
1408 | host_ = [url host]; | |
1409 | if (host_ != nil) | |
c08c8943 | 1410 | host_ = [host_ lowercaseString]; |
7623f855 JF |
1411 | |
1412 | if (host_ != nil) | |
dfdb9ae0 | 1413 | authority_ = host_; |
7623f855 JF |
1414 | else |
1415 | authority_ = [url path]; | |
a3328c28 JF |
1416 | } |
1417 | ||
f9f6d9e8 | 1418 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
a3328c28 | 1419 | if ((self = [super init]) != nil) { |
f9f6d9e8 | 1420 | [self setMetaIndex:index inPool:pool]; |
36bb2ca2 | 1421 | } return self; |
2367a917 | 1422 | } |
dc5812ec | 1423 | |
7b0ce2da JF |
1424 | - (NSComparisonResult) compareByNameAndType:(Source *)source { |
1425 | NSDictionary *lhr = [self record]; | |
1426 | NSDictionary *rhr = [source record]; | |
1427 | ||
1428 | if (lhr != rhr) | |
1429 | return lhr == nil ? NSOrderedDescending : NSOrderedAscending; | |
1430 | ||
1431 | NSString *lhs = [self name]; | |
1432 | NSString *rhs = [source name]; | |
1433 | ||
1434 | if ([lhs length] != 0 && [rhs length] != 0) { | |
1435 | unichar lhc = [lhs characterAtIndex:0]; | |
1436 | unichar rhc = [rhs characterAtIndex:0]; | |
1437 | ||
1438 | if (isalpha(lhc) && !isalpha(rhc)) | |
1439 | return NSOrderedAscending; | |
1440 | else if (!isalpha(lhc) && isalpha(rhc)) | |
1441 | return NSOrderedDescending; | |
1442 | } | |
1443 | ||
68f1828e | 1444 | return [lhs compare:rhs options:LaxCompareOptions_]; |
7b0ce2da JF |
1445 | } |
1446 | ||
6204f56a | 1447 | - (NSString *) depictionForPackage:(NSString *)package { |
89b0ea4a | 1448 | return depiction_.empty() ? nil : [static_cast<id>(depiction_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
6204f56a JF |
1449 | } |
1450 | ||
dc63e78f | 1451 | - (NSString *) supportForPackage:(NSString *)package { |
89b0ea4a | 1452 | return support_.empty() ? nil : [static_cast<id>(support_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
dc63e78f JF |
1453 | } |
1454 | ||
7b0ce2da JF |
1455 | - (NSDictionary *) record { |
1456 | return record_; | |
1457 | } | |
1458 | ||
36bb2ca2 JF |
1459 | - (BOOL) trusted { |
1460 | return trusted_; | |
1461 | } | |
3178d79b | 1462 | |
36bb2ca2 JF |
1463 | - (NSString *) uri { |
1464 | return uri_; | |
1465 | } | |
ec97ef06 | 1466 | |
36bb2ca2 JF |
1467 | - (NSString *) distribution { |
1468 | return distribution_; | |
1469 | } | |
dc5812ec | 1470 | |
36bb2ca2 JF |
1471 | - (NSString *) type { |
1472 | return type_; | |
dc5812ec JF |
1473 | } |
1474 | ||
8252b666 JF |
1475 | - (NSString *) base { |
1476 | return base_; | |
1477 | } | |
1478 | ||
7b0ce2da | 1479 | - (NSString *) key { |
f9f6d9e8 | 1480 | return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_]; |
7b0ce2da JF |
1481 | } |
1482 | ||
1483 | - (NSString *) host { | |
f9f6d9e8 | 1484 | return host_; |
7b0ce2da JF |
1485 | } |
1486 | ||
1487 | - (NSString *) name { | |
c08c8943 | 1488 | return origin_.empty() ? (id) authority_ : origin_; |
7b0ce2da JF |
1489 | } |
1490 | ||
8d262908 | 1491 | - (NSString *) shortDescription { |
36bb2ca2 JF |
1492 | return description_; |
1493 | } | |
b4d89997 | 1494 | |
36bb2ca2 | 1495 | - (NSString *) label { |
c08c8943 | 1496 | return label_.empty() ? (id) authority_ : label_; |
36bb2ca2 | 1497 | } |
3178d79b | 1498 | |
36bb2ca2 JF |
1499 | - (NSString *) origin { |
1500 | return origin_; | |
1501 | } | |
3178d79b | 1502 | |
36bb2ca2 JF |
1503 | - (NSString *) version { |
1504 | return version_; | |
1505 | } | |
2367a917 | 1506 | |
36bb2ca2 JF |
1507 | - (NSString *) defaultIcon { |
1508 | return defaultIcon_; | |
1509 | } | |
2367a917 | 1510 | |
2388b078 JF |
1511 | @end |
1512 | /* }}} */ | |
83682c75 JF |
1513 | /* CydiaOperation Class {{{ */ |
1514 | @interface CydiaOperation : NSObject { | |
7b33d201 JF |
1515 | _H<NSString> operator_; |
1516 | _H<NSString> value_; | |
2388b078 JF |
1517 | } |
1518 | ||
83682c75 JF |
1519 | - (NSString *) operator; |
1520 | - (NSString *) value; | |
2388b078 JF |
1521 | |
1522 | @end | |
1523 | ||
83682c75 | 1524 | @implementation CydiaOperation |
2388b078 | 1525 | |
83682c75 JF |
1526 | - (id) initWithOperator:(const char *)_operator value:(const char *)value { |
1527 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
1528 | operator_ = [NSString stringWithUTF8String:_operator]; |
1529 | value_ = [NSString stringWithUTF8String:value]; | |
83682c75 JF |
1530 | } return self; |
1531 | } | |
1532 | ||
1533 | + (NSArray *) _attributeKeys { | |
1534 | return [NSArray arrayWithObjects: | |
1535 | @"operator", | |
1536 | @"value", | |
1537 | nil]; | |
2388b078 JF |
1538 | } |
1539 | ||
83682c75 JF |
1540 | - (NSArray *) attributeKeys { |
1541 | return [[self class] _attributeKeys]; | |
2388b078 JF |
1542 | } |
1543 | ||
83682c75 JF |
1544 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
1545 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1546 | } | |
1547 | ||
1548 | - (NSString *) operator { | |
1549 | return operator_; | |
1550 | } | |
1551 | ||
1552 | - (NSString *) value { | |
1553 | return value_; | |
1554 | } | |
1555 | ||
1556 | @end | |
1557 | /* }}} */ | |
810c9763 JF |
1558 | /* CydiaClause Class {{{ */ |
1559 | @interface CydiaClause : NSObject { | |
7b33d201 JF |
1560 | _H<NSString> package_; |
1561 | _H<CydiaOperation> version_; | |
83682c75 JF |
1562 | } |
1563 | ||
83682c75 JF |
1564 | - (NSString *) package; |
1565 | - (CydiaOperation *) version; | |
1566 | ||
1567 | @end | |
1568 | ||
810c9763 | 1569 | @implementation CydiaClause |
83682c75 | 1570 | |
83682c75 JF |
1571 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
1572 | if ((self = [super init]) != nil) { | |
7b33d201 | 1573 | package_ = [NSString stringWithUTF8String:dep.TargetPkg().Name()]; |
83682c75 JF |
1574 | |
1575 | if (const char *version = dep.TargetVer()) | |
7b33d201 | 1576 | version_ = [[[CydiaOperation alloc] initWithOperator:dep.CompType() value:version] autorelease]; |
83682c75 | 1577 | else |
7b33d201 | 1578 | version_ = (id) [NSNull null]; |
83682c75 JF |
1579 | } return self; |
1580 | } | |
1581 | ||
1582 | + (NSArray *) _attributeKeys { | |
1583 | return [NSArray arrayWithObjects: | |
1584 | @"package", | |
83682c75 JF |
1585 | @"version", |
1586 | nil]; | |
1587 | } | |
1588 | ||
1589 | - (NSArray *) attributeKeys { | |
1590 | return [[self class] _attributeKeys]; | |
1591 | } | |
1592 | ||
1593 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1594 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1595 | } | |
1596 | ||
83682c75 JF |
1597 | - (NSString *) package { |
1598 | return package_; | |
1599 | } | |
1600 | ||
1601 | - (CydiaOperation *) version { | |
1602 | return version_; | |
2388b078 JF |
1603 | } |
1604 | ||
810c9763 JF |
1605 | @end |
1606 | /* }}} */ | |
1607 | /* CydiaRelation Class {{{ */ | |
1608 | @interface CydiaRelation : NSObject { | |
7b33d201 JF |
1609 | _H<NSString> relationship_; |
1610 | _H<NSMutableArray> clauses_; | |
810c9763 JF |
1611 | } |
1612 | ||
1613 | - (NSString *) relationship; | |
1614 | - (NSArray *) clauses; | |
1615 | ||
1616 | @end | |
1617 | ||
1618 | @implementation CydiaRelation | |
1619 | ||
810c9763 JF |
1620 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
1621 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
1622 | relationship_ = [NSString stringWithUTF8String:dep.DepType()]; |
1623 | clauses_ = [NSMutableArray arrayWithCapacity:8]; | |
810c9763 JF |
1624 | |
1625 | pkgCache::DepIterator start; | |
1626 | pkgCache::DepIterator end; | |
1627 | dep.GlobOr(start, end); // ++dep | |
1628 | ||
1629 | _forever { | |
1630 | [clauses_ addObject:[[[CydiaClause alloc] initWithIterator:start] autorelease]]; | |
1631 | ||
1632 | // yes, seriously. (wtf?) | |
1633 | if (start == end) | |
1634 | break; | |
1635 | ++start; | |
1636 | } | |
1637 | } return self; | |
1638 | } | |
1639 | ||
1640 | + (NSArray *) _attributeKeys { | |
1641 | return [NSArray arrayWithObjects: | |
1642 | @"clauses", | |
1643 | @"relationship", | |
1644 | nil]; | |
1645 | } | |
1646 | ||
1647 | - (NSArray *) attributeKeys { | |
1648 | return [[self class] _attributeKeys]; | |
1649 | } | |
1650 | ||
1651 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1652 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1653 | } | |
1654 | ||
1655 | - (NSString *) relationship { | |
1656 | return relationship_; | |
1657 | } | |
1658 | ||
1659 | - (NSArray *) clauses { | |
1660 | return clauses_; | |
1661 | } | |
1662 | ||
1663 | - (void) addClause:(CydiaClause *)clause { | |
1664 | [clauses_ addObject:clause]; | |
1665 | } | |
1666 | ||
dc5812ec | 1667 | @end |
b4d89997 | 1668 | /* }}} */ |
36bb2ca2 | 1669 | /* Package Class {{{ */ |
12016ee5 JF |
1670 | struct ParsedPackage { |
1671 | CYString tagline_; | |
1672 | ||
1673 | CYString icon_; | |
1674 | ||
1675 | CYString depiction_; | |
1676 | CYString homepage_; | |
1677 | ||
1678 | CYString sponsor_; | |
1679 | CYString author_; | |
1680 | ||
1681 | CYString bugs_; | |
1682 | CYString support_; | |
1683 | }; | |
1684 | ||
36bb2ca2 | 1685 | @interface Package : NSObject { |
97a3d89e JF |
1686 | uint32_t era_ : 26; |
1687 | uint32_t role_ : 3; | |
aab28f8b JF |
1688 | uint32_t essential_ : 1; |
1689 | uint32_t obsolete_ : 1; | |
1690 | uint32_t ignored_ : 1; | |
1691 | ||
68d927e2 | 1692 | apr_pool_t *pool_; |
a1440b10 | 1693 | |
9c5737d5 JF |
1694 | uint32_t rank_; |
1695 | ||
aab28f8b JF |
1696 | _transient Database *database_; |
1697 | ||
7376b55c | 1698 | pkgCache::VerIterator version_; |
36bb2ca2 | 1699 | pkgCache::PkgIterator iterator_; |
36bb2ca2 | 1700 | pkgCache::VerFileIterator file_; |
bbb879fb | 1701 | |
aab28f8b JF |
1702 | CYString id_; |
1703 | CYString name_; | |
807ae6d7 | 1704 | |
4c0ed943 | 1705 | CYString latest_; |
6a155117 | 1706 | CYString installed_; |
dc5812ec | 1707 | |
fe33a23e | 1708 | const char *section_; |
aab28f8b | 1709 | _transient NSString *section$_; |
f79a4512 | 1710 | |
7b33d201 | 1711 | _H<Source> source_; |
2388b078 | 1712 | |
bb6bb6d6 | 1713 | PackageValue *metadata_; |
aab28f8b | 1714 | ParsedPackage *parsed_; |
94b0b3e5 | 1715 | |
7b33d201 | 1716 | _H<NSMutableArray> tags_; |
b4d89997 JF |
1717 | } |
1718 | ||
7376b55c | 1719 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
f79a4512 | 1720 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
b4d89997 | 1721 | |
2388b078 | 1722 | - (pkgCache::PkgIterator) iterator; |
68d927e2 | 1723 | - (void) parse; |
2388b078 | 1724 | |
36bb2ca2 | 1725 | - (NSString *) section; |
dec6029f JF |
1726 | - (NSString *) simpleSection; |
1727 | ||
f79a4512 JF |
1728 | - (NSString *) longSection; |
1729 | - (NSString *) shortSection; | |
1730 | ||
a3328c28 JF |
1731 | - (NSString *) uri; |
1732 | ||
7aa82ca2 | 1733 | - (MIMEAddress *) maintainer; |
36bb2ca2 | 1734 | - (size_t) size; |
eef4ccaf JF |
1735 | - (NSString *) longDescription; |
1736 | - (NSString *) shortDescription; | |
808c6eb6 | 1737 | - (unichar) index; |
dc5812ec | 1738 | |
94b0b3e5 | 1739 | - (PackageValue *) metadata; |
31bc18a7 | 1740 | - (time_t) seen; |
94b0b3e5 JF |
1741 | |
1742 | - (bool) subscribed; | |
1743 | - (bool) setSubscribed:(bool)subscribed; | |
1744 | ||
807ae6d7 | 1745 | - (BOOL) ignored; |
9e98e020 | 1746 | |
36bb2ca2 JF |
1747 | - (NSString *) latest; |
1748 | - (NSString *) installed; | |
6a155117 | 1749 | - (BOOL) uninstalled; |
5a09ae08 JF |
1750 | |
1751 | - (BOOL) valid; | |
31f3cfff | 1752 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
36bb2ca2 JF |
1753 | - (BOOL) essential; |
1754 | - (BOOL) broken; | |
7d2ac47f | 1755 | - (BOOL) unfiltered; |
6d9712c4 | 1756 | - (BOOL) visible; |
b4d89997 | 1757 | |
9bedffaa JF |
1758 | - (BOOL) half; |
1759 | - (BOOL) halfConfigured; | |
1760 | - (BOOL) halfInstalled; | |
1761 | - (BOOL) hasMode; | |
1762 | - (NSString *) mode; | |
1763 | ||
36bb2ca2 JF |
1764 | - (NSString *) id; |
1765 | - (NSString *) name; | |
770f2a8e | 1766 | - (UIImage *) icon; |
6f1a15d9 | 1767 | - (NSString *) homepage; |
25a2158d | 1768 | - (NSString *) depiction; |
7aa82ca2 | 1769 | - (MIMEAddress *) author; |
b4d89997 | 1770 | |
dc63e78f JF |
1771 | - (NSString *) support; |
1772 | ||
affeffc7 | 1773 | - (NSArray *) files; |
affeffc7 JF |
1774 | - (NSArray *) warnings; |
1775 | - (NSArray *) applications; | |
2388b078 | 1776 | |
36bb2ca2 | 1777 | - (Source *) source; |
b4d89997 | 1778 | |
9c5737d5 | 1779 | - (uint32_t) rank; |
d84597fe | 1780 | - (BOOL) matches:(NSArray *)query; |
b4d89997 | 1781 | |
7b0ce2da | 1782 | - (bool) hasSupportingRole; |
6d9712c4 | 1783 | - (BOOL) hasTag:(NSString *)tag; |
c390d3ab | 1784 | - (NSString *) primaryPurpose; |
770f2a8e | 1785 | - (NSArray *) purposes; |
3bd1c2a2 | 1786 | - (bool) isCommercial; |
6d9712c4 | 1787 | |
cd95e390 JF |
1788 | - (void) setIndex:(size_t)index; |
1789 | ||
df213583 JF |
1790 | - (CYString &) cyname; |
1791 | ||
f79a4512 | 1792 | - (uint32_t) compareBySection:(NSArray *)sections; |
807ae6d7 | 1793 | |
36bb2ca2 JF |
1794 | - (void) install; |
1795 | - (void) remove; | |
06aa974d | 1796 | |
d84597fe | 1797 | - (bool) isUnfilteredAndSearchedForBy:(NSArray *)query; |
01d93940 | 1798 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search; |
e4f3d6e9 | 1799 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number; |
670a0494 | 1800 | - (bool) isVisibleInSection:(NSString *)section; |
0a3b45ef | 1801 | - (bool) isVisibleInSource:(Source *)source; |
b4d89997 | 1802 | |
36bb2ca2 | 1803 | @end |
b4d89997 | 1804 | |
f79a4512 JF |
1805 | uint32_t PackageChangesRadix(Package *self, void *) { |
1806 | union { | |
1807 | uint32_t key; | |
1808 | ||
1809 | struct { | |
1810 | uint32_t timestamp : 30; | |
1811 | uint32_t ignored : 1; | |
1812 | uint32_t upgradable : 1; | |
1813 | } bits; | |
1814 | } value; | |
1815 | ||
1816 | bool upgradable([self upgradableAndEssential:YES]); | |
1817 | value.bits.upgradable = upgradable ? 1 : 0; | |
1818 | ||
1819 | if (upgradable) { | |
1820 | value.bits.timestamp = 0; | |
1821 | value.bits.ignored = [self ignored] ? 0 : 1; | |
1822 | value.bits.upgradable = 1; | |
1823 | } else { | |
36047c66 | 1824 | value.bits.timestamp = [self seen] >> 2; |
f79a4512 JF |
1825 | value.bits.ignored = 0; |
1826 | value.bits.upgradable = 0; | |
1827 | } | |
1828 | ||
1829 | return _not(uint32_t) - value.key; | |
1830 | } | |
1831 | ||
df213583 JF |
1832 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
1833 | size_t offset(reinterpret_cast<size_t>(context)); | |
1834 | CYString &name([self cyname]); | |
677b8415 | 1835 | |
df213583 JF |
1836 | size_t size(name.size()); |
1837 | if (size == 0) | |
1838 | return 0; | |
1839 | char *text(name.data()); | |
677b8415 | 1840 | |
df213583 JF |
1841 | size_t zeros; |
1842 | if (!isdigit(text[0])) | |
1843 | zeros = 0; | |
1844 | else { | |
1845 | size_t digits(1); | |
1846 | while (size != digits && isdigit(text[digits])) | |
1847 | if (++digits == 4) | |
1848 | break; | |
1849 | zeros = 4 - digits; | |
1850 | } | |
677b8415 | 1851 | |
df213583 JF |
1852 | uint8_t data[4]; |
1853 | ||
df213583 JF |
1854 | if (offset == 0 && zeros != 0) { |
1855 | memset(data, '0', zeros); | |
1856 | memcpy(data + zeros, text, 4 - zeros); | |
1857 | } else { | |
1858 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
1859 | if (size <= offset - zeros) | |
1860 | return 0; | |
1861 | ||
1862 | text += offset - zeros; | |
1863 | size -= offset - zeros; | |
1864 | ||
1865 | if (size >= 4) | |
1866 | memcpy(data, text, 4); | |
1867 | else { | |
1868 | memcpy(data, text, size); | |
1869 | memset(data + size, 0, 4 - size); | |
1870 | } | |
1871 | ||
1872 | for (size_t i(0); i != 4; ++i) | |
1873 | if (isalpha(data[i])) | |
a7a59ee1 | 1874 | data[i] |= 0x20; |
df213583 JF |
1875 | } |
1876 | ||
1877 | if (offset == 0) | |
a7a59ee1 JF |
1878 | if (data[0] == '@') |
1879 | data[0] = 0x7f; | |
1880 | else | |
1881 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
df213583 JF |
1882 | |
1883 | /* XXX: ntohl may be more honest */ | |
1884 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
1885 | } | |
1886 | ||
1887 | CYString &(*PackageName)(Package *self, SEL sel); | |
1888 | ||
1889 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { | |
1890 | _profile(PackageNameCompare) | |
1891 | CYString &lhi(PackageName(lhs, @selector(cyname))); | |
1892 | CYString &rhi(PackageName(rhs, @selector(cyname))); | |
1893 | CFStringRef lhn(lhi), rhn(rhi); | |
677b8415 | 1894 | |
5358f56f JF |
1895 | if (lhn == NULL) |
1896 | return rhn == NULL ? NSOrderedSame : NSOrderedAscending; | |
1897 | else if (rhn == NULL) | |
1898 | return NSOrderedDescending; | |
1899 | ||
677b8415 | 1900 | _profile(PackageNameCompare$NumbersLast) |
df213583 | 1901 | if (!lhi.empty() && !rhi.empty()) { |
677b8415 JF |
1902 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
1903 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
1904 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
1905 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
1906 | return lha ? NSOrderedAscending : NSOrderedDescending; | |
1907 | } | |
1908 | _end | |
1909 | ||
df213583 JF |
1910 | CFIndex length = CFStringGetLength(lhn); |
1911 | ||
677b8415 JF |
1912 | _profile(PackageNameCompare$Compare) |
1913 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, Locale_); | |
1914 | _end | |
1915 | _end | |
1916 | } | |
1917 | ||
eef4ccaf JF |
1918 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *context) { |
1919 | return PackageNameCompare(*lhs, *rhs, context); | |
677b8415 JF |
1920 | } |
1921 | ||
1922 | struct PackageNameOrdering : | |
1923 | std::binary_function<Package *, Package *, bool> | |
1924 | { | |
1925 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
1926 | return PackageNameCompare(lhs, rhs, NULL) == NSOrderedAscending; | |
1927 | } | |
1928 | }; | |
1929 | ||
36bb2ca2 | 1930 | @implementation Package |
b4d89997 | 1931 | |
df213583 JF |
1932 | - (NSString *) description { |
1933 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
1934 | } | |
1935 | ||
36bb2ca2 | 1936 | - (void) dealloc { |
12016ee5 JF |
1937 | if (parsed_ != NULL) |
1938 | delete parsed_; | |
36bb2ca2 | 1939 | [super dealloc]; |
b4d89997 JF |
1940 | } |
1941 | ||
3bd1c2a2 | 1942 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 1943 | if (false); |
10d63f9e JF |
1944 | else if (selector == @selector(clear)) |
1945 | return @"clear"; | |
2cd1afd9 JF |
1946 | else if (selector == @selector(getField:)) |
1947 | return @"getField"; | |
e58ff941 | 1948 | else if (selector == @selector(hasTag:)) |
3bd1c2a2 | 1949 | return @"hasTag"; |
10d63f9e JF |
1950 | else if (selector == @selector(install)) |
1951 | return @"install"; | |
1952 | else if (selector == @selector(remove)) | |
1953 | return @"remove"; | |
3bd1c2a2 JF |
1954 | else |
1955 | return nil; | |
1956 | } | |
1957 | ||
1958 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
1959 | return [self webScriptNameForSelector:selector] == nil; | |
1960 | } | |
1961 | ||
6f1a15d9 | 1962 | + (NSArray *) _attributeKeys { |
e58ff941 JF |
1963 | return [NSArray arrayWithObjects: |
1964 | @"applications", | |
1965 | @"author", | |
1966 | @"depiction", | |
1967 | @"essential", | |
1968 | @"homepage", | |
1969 | @"icon", | |
1970 | @"id", | |
1971 | @"installed", | |
1972 | @"latest", | |
1973 | @"longDescription", | |
1974 | @"longSection", | |
1975 | @"maintainer", | |
1976 | @"mode", | |
1977 | @"name", | |
1978 | @"purposes", | |
83682c75 | 1979 | @"relations", |
e58ff941 | 1980 | @"section", |
de1ace71 | 1981 | @"selection", |
e58ff941 JF |
1982 | @"shortDescription", |
1983 | @"shortSection", | |
1984 | @"simpleSection", | |
1985 | @"size", | |
1986 | @"source", | |
1987 | @"sponsor", | |
5959b596 | 1988 | @"state", |
e58ff941 | 1989 | @"support", |
82aa2434 | 1990 | @"tags", |
e58ff941 JF |
1991 | @"warnings", |
1992 | nil]; | |
6f1a15d9 JF |
1993 | } |
1994 | ||
1995 | - (NSArray *) attributeKeys { | |
1996 | return [[self class] _attributeKeys]; | |
1997 | } | |
1998 | ||
1999 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2000 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2001 | } | |
2002 | ||
83682c75 JF |
2003 | - (NSArray *) relations { |
2004 | @synchronized (database_) { | |
2005 | NSMutableArray *relations([NSMutableArray arrayWithCapacity:16]); | |
810c9763 JF |
2006 | for (pkgCache::DepIterator dep(version_.DependsList()); !dep.end(); ++dep) |
2007 | [relations addObject:[[[CydiaRelation alloc] initWithIterator:dep] autorelease]]; | |
83682c75 JF |
2008 | return relations; |
2009 | } } | |
2010 | ||
2cd1afd9 JF |
2011 | - (NSString *) getField:(NSString *)name { |
2012 | @synchronized (database_) { | |
2013 | if ([database_ era] != era_ || file_.end()) | |
2014 | return nil; | |
2015 | ||
2016 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2017 | ||
2018 | const char *start, *end; | |
2019 | if (!parser.Find([name UTF8String], start, end)) | |
2020 | return (NSString *) [NSNull null]; | |
2021 | ||
2022 | return [(NSString *) CYStringCreate(start, end - start) autorelease]; | |
2023 | } } | |
2024 | ||
68d927e2 | 2025 | - (void) parse { |
12016ee5 | 2026 | if (parsed_ != NULL) |
68d927e2 | 2027 | return; |
12016ee5 JF |
2028 | @synchronized (database_) { |
2029 | if ([database_ era] != era_ || file_.end()) | |
68d927e2 JF |
2030 | return; |
2031 | ||
12016ee5 JF |
2032 | ParsedPackage *parsed(new ParsedPackage); |
2033 | parsed_ = parsed; | |
2034 | ||
68d927e2 JF |
2035 | _profile(Package$parse) |
2036 | pkgRecords::Parser *parser; | |
2037 | ||
2038 | _profile(Package$parse$Lookup) | |
2039 | parser = &[database_ records]->Lookup(file_); | |
2040 | _end | |
2041 | ||
2042 | CYString website; | |
2043 | ||
2044 | _profile(Package$parse$Find) | |
2045 | struct { | |
2046 | const char *name_; | |
2047 | CYString *value_; | |
2048 | } names[] = { | |
12016ee5 JF |
2049 | {"icon", &parsed->icon_}, |
2050 | {"depiction", &parsed->depiction_}, | |
2051 | {"homepage", &parsed->homepage_}, | |
68d927e2 | 2052 | {"website", &website}, |
12016ee5 JF |
2053 | {"bugs", &parsed->bugs_}, |
2054 | {"support", &parsed->support_}, | |
2055 | {"sponsor", &parsed->sponsor_}, | |
2056 | {"author", &parsed->author_}, | |
68d927e2 JF |
2057 | }; |
2058 | ||
2059 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2060 | const char *start, *end; | |
2061 | ||
2062 | if (parser->Find(names[i].name_, start, end)) { | |
2063 | CYString &value(*names[i].value_); | |
2064 | _profile(Package$parse$Value) | |
2065 | value.set(pool_, start, end - start); | |
2066 | _end | |
2067 | } | |
2068 | } | |
2069 | _end | |
2070 | ||
2071 | _profile(Package$parse$Tagline) | |
2072 | const char *start, *end; | |
0dd0c302 | 2073 | if (parser->ShortDesc(start, end)) { |
68d927e2 JF |
2074 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2075 | if (stop == NULL) | |
2076 | stop = end; | |
2077 | while (stop != start && stop[-1] == '\r') | |
2078 | --stop; | |
12016ee5 | 2079 | parsed->tagline_.set(pool_, start, stop - start); |
68d927e2 JF |
2080 | } |
2081 | _end | |
2082 | ||
2083 | _profile(Package$parse$Retain) | |
12016ee5 JF |
2084 | if (parsed->homepage_.empty()) |
2085 | parsed->homepage_ = website; | |
2086 | if (parsed->homepage_ == parsed->depiction_) | |
2087 | parsed->homepage_.clear(); | |
68d927e2 JF |
2088 | _end |
2089 | _end | |
12016ee5 | 2090 | } } |
68d927e2 | 2091 | |
7376b55c | 2092 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
a1440b10 | 2093 | if ((self = [super init]) != nil) { |
7376b55c | 2094 | _profile(Package$initWithVersion) |
68d927e2 | 2095 | pool_ = pool; |
a1440b10 | 2096 | |
36bb2ca2 | 2097 | database_ = database; |
aab28f8b | 2098 | era_ = [database era]; |
b4d89997 | 2099 | |
aab28f8b | 2100 | version_ = version; |
2083b866 | 2101 | |
aab28f8b JF |
2102 | pkgCache::PkgIterator iterator(version.ParentPkg()); |
2103 | iterator_ = iterator; | |
06aa974d | 2104 | |
aab28f8b | 2105 | _profile(Package$initWithVersion$Version) |
68d927e2 JF |
2106 | if (!version_.end()) |
2107 | file_ = version_.FileList(); | |
2108 | else { | |
2109 | pkgCache &cache([database_ cache]); | |
2110 | file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); | |
2111 | } | |
2112 | _end | |
808c6eb6 | 2113 | |
aab28f8b | 2114 | _profile(Package$initWithVersion$Cache) |
aab28f8b JF |
2115 | name_.set(NULL, iterator.Display()); |
2116 | ||
2117 | latest_.set(NULL, StripVersion_(version_.VerStr())); | |
2118 | ||
2119 | pkgCache::VerIterator current(iterator.CurrentVer()); | |
2120 | if (!current.end()) | |
2121 | installed_.set(NULL, StripVersion_(current.VerStr())); | |
808c6eb6 JF |
2122 | _end |
2123 | ||
7376b55c | 2124 | _profile(Package$initWithVersion$Tags) |
aab28f8b | 2125 | pkgCache::TagIterator tag(iterator.TagList()); |
0dd0c302 | 2126 | if (!tag.end()) { |
7b33d201 | 2127 | tags_ = [NSMutableArray arrayWithCapacity:8]; |
0dd0c302 JF |
2128 | do { |
2129 | const char *name(tag.Name()); | |
3931b718 | 2130 | [tags_ addObject:[(NSString *)CYStringCreate(name) autorelease]]; |
0a377825 | 2131 | |
97a3d89e JF |
2132 | if (role_ == 0 && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) { |
2133 | if (strcmp(name + 6, "enduser") == 0) | |
2134 | role_ = 1; | |
2135 | else if (strcmp(name + 6, "hacker") == 0) | |
2136 | role_ = 2; | |
2137 | else if (strcmp(name + 6, "developer") == 0) | |
2138 | role_ = 3; | |
2139 | else if (strcmp(name + 6, "cydia") == 0) | |
2140 | role_ = 7; | |
2141 | else | |
2142 | role_ = 4; | |
2143 | } | |
0a377825 JF |
2144 | |
2145 | if (strncmp(name, "cydia::", 7) == 0) { | |
2146 | if (strcmp(name + 7, "essential") == 0) | |
2147 | essential_ = true; | |
2148 | else if (strcmp(name + 7, "obsolete") == 0) | |
2149 | obsolete_ = true; | |
2150 | } | |
2151 | ||
0dd0c302 JF |
2152 | ++tag; |
2153 | } while (!tag.end()); | |
2154 | } | |
808c6eb6 | 2155 | _end |
7b0ce2da | 2156 | |
7376b55c | 2157 | _profile(Package$initWithVersion$Metadata) |
029c8b74 JF |
2158 | const char *mixed(iterator.Name()); |
2159 | size_t size(strlen(mixed)); | |
2160 | char lower[size + 1]; | |
2161 | ||
2162 | for (size_t i(0); i != size; ++i) | |
2163 | lower[i] = mixed[i] | 0x20; | |
2164 | lower[size] = '\0'; | |
2165 | ||
2166 | PackageValue *metadata(PackageFind(lower, size)); | |
bb6bb6d6 | 2167 | metadata_ = metadata; |
677b8415 | 2168 | |
029c8b74 JF |
2169 | id_.set(NULL, metadata->name_, size); |
2170 | ||
94b0b3e5 JF |
2171 | const char *latest(version_.VerStr()); |
2172 | size_t length(strlen(latest)); | |
a4b0ec52 | 2173 | |
94b0b3e5 | 2174 | uint16_t vhash(hashlittle(latest, length)); |
677b8415 | 2175 | |
94b0b3e5 JF |
2176 | size_t capped(std::min<size_t>(8, length)); |
2177 | latest = latest + length - capped; | |
677b8415 | 2178 | |
94b0b3e5 JF |
2179 | if (metadata->first_ == 0) |
2180 | metadata->first_ = now_; | |
808c6eb6 | 2181 | |
94b0b3e5 | 2182 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
94b0b3e5 JF |
2183 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); |
2184 | metadata->vhash_ = vhash; | |
2f856365 JF |
2185 | metadata->last_ = now_; |
2186 | } else if (metadata->last_ == 0) | |
2187 | metadata->last_ = metadata->first_; | |
808c6eb6 | 2188 | _end |
a1440b10 | 2189 | |
7376b55c | 2190 | _profile(Package$initWithVersion$Section) |
fe33a23e | 2191 | section_ = iterator.Section(); |
f79a4512 | 2192 | _end |
a1440b10 | 2193 | |
aab28f8b JF |
2194 | _profile(Package$initWithVersion$Flags) |
2195 | essential_ |= ((iterator->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); | |
2196 | ignored_ = iterator->SelectedState == pkgCache::State::Hold; | |
4fa77608 | 2197 | _end |
3dd53516 | 2198 | _end } return self; |
dc5812ec JF |
2199 | } |
2200 | ||
f79a4512 | 2201 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
b1ce61ec JF |
2202 | pkgCache::VerIterator version; |
2203 | ||
2204 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2205 | version = [database policy]->GetCandidateVer(iterator); | |
2206 | _end | |
2207 | ||
7376b55c JF |
2208 | if (version.end()) |
2209 | return nil; | |
b1ce61ec | 2210 | |
8564efc1 JF |
2211 | Package *package; |
2212 | ||
2213 | _profile(Package$packageWithIterator$Allocate) | |
2214 | package = [Package allocWithZone:zone]; | |
2215 | _end | |
2216 | ||
2217 | _profile(Package$packageWithIterator$Initialize) | |
2218 | package = [package | |
2219 | initWithVersion:version | |
2220 | withZone:zone | |
2221 | inPool:pool | |
2222 | database:database | |
2223 | ]; | |
2224 | _end | |
2225 | ||
2226 | _profile(Package$packageWithIterator$Autorelease) | |
2227 | package = [package autorelease]; | |
2228 | _end | |
2229 | ||
2230 | return package; | |
3dd53516 | 2231 | } |
dc5812ec | 2232 | |
2388b078 JF |
2233 | - (pkgCache::PkgIterator) iterator { |
2234 | return iterator_; | |
2235 | } | |
2236 | ||
36bb2ca2 | 2237 | - (NSString *) section { |
f79a4512 | 2238 | if (section$_ == nil) { |
fe33a23e | 2239 | if (section_ == NULL) |
f79a4512 JF |
2240 | return nil; |
2241 | ||
fe33a23e JF |
2242 | _profile(Package$section$mappedSectionForPointer) |
2243 | section$_ = [database_ mappedSectionForPointer:section_]; | |
e4f3d6e9 | 2244 | _end |
f79a4512 | 2245 | } return section$_; |
dc5812ec JF |
2246 | } |
2247 | ||
dec6029f JF |
2248 | - (NSString *) simpleSection { |
2249 | if (NSString *section = [self section]) | |
2250 | return Simplify(section); | |
2251 | else | |
2252 | return nil; | |
a3328c28 | 2253 | } |
dec6029f | 2254 | |
f79a4512 | 2255 | - (NSString *) longSection { |
18873623 | 2256 | return LocalizeSection([self section]); |
f79a4512 JF |
2257 | } |
2258 | ||
2259 | - (NSString *) shortSection { | |
2260 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2261 | } | |
2262 | ||
a3328c28 JF |
2263 | - (NSString *) uri { |
2264 | return nil; | |
2265 | #if 0 | |
2266 | pkgIndexFile *index; | |
2267 | pkgCache::PkgFileIterator file(file_.File()); | |
2268 | if (![database_ list].FindIndex(file, index)) | |
2269 | return nil; | |
2270 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2271 | //return [NSString stringWithUTF8String:file.Site()]; | |
2272 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2273 | #endif | |
dec6029f JF |
2274 | } |
2275 | ||
7aa82ca2 | 2276 | - (MIMEAddress *) maintainer { |
884171d6 JF |
2277 | @synchronized (database_) { |
2278 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2279 | return nil; |
884171d6 | 2280 | |
36bb2ca2 | 2281 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
00e2109e | 2282 | const std::string &maintainer(parser->Maintainer()); |
7aa82ca2 | 2283 | return maintainer.empty() ? nil : [MIMEAddress addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; |
884171d6 | 2284 | } } |
8fe19fc1 | 2285 | |
36bb2ca2 | 2286 | - (size_t) size { |
884171d6 JF |
2287 | @synchronized (database_) { |
2288 | if ([database_ era] != era_ || version_.end()) | |
2289 | return 0; | |
2290 | ||
2291 | return version_->InstalledSize; | |
2292 | } } | |
dc5812ec | 2293 | |
eef4ccaf | 2294 | - (NSString *) longDescription { |
3dd53516 JF |
2295 | @synchronized (database_) { |
2296 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2297 | return nil; |
3dd53516 | 2298 | |
36bb2ca2 | 2299 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2388b078 | 2300 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
8fe19fc1 | 2301 | |
36bb2ca2 JF |
2302 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2303 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2304 | if ([lines count] < 2) | |
2305 | return nil; | |
3178d79b | 2306 | |
36bb2ca2 | 2307 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
c4dcf2c2 | 2308 | for (size_t i(1), e([lines count]); i != e; ++i) { |
36bb2ca2 JF |
2309 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2310 | [trimmed addObject:trim]; | |
2311 | } | |
3178d79b | 2312 | |
36bb2ca2 | 2313 | return [trimmed componentsJoinedByString:@"\n"]; |
3dd53516 | 2314 | } } |
dc5812ec | 2315 | |
eef4ccaf | 2316 | - (NSString *) shortDescription { |
12016ee5 | 2317 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->tagline_); |
eef4ccaf JF |
2318 | } |
2319 | ||
808c6eb6 JF |
2320 | - (unichar) index { |
2321 | _profile(Package$index) | |
677b8415 JF |
2322 | CFStringRef name((CFStringRef) [self name]); |
2323 | if (CFStringGetLength(name) == 0) | |
808c6eb6 | 2324 | return '#'; |
677b8415 JF |
2325 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2326 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
808c6eb6 | 2327 | return '#'; |
447db19d | 2328 | return toupper(character); |
808c6eb6 | 2329 | _end |
36bb2ca2 | 2330 | } |
3178d79b | 2331 | |
94b0b3e5 | 2332 | - (PackageValue *) metadata { |
bb6bb6d6 | 2333 | return metadata_; |
807ae6d7 JF |
2334 | } |
2335 | ||
31bc18a7 | 2336 | - (time_t) seen { |
94b0b3e5 JF |
2337 | PackageValue *metadata([self metadata]); |
2338 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
3178d79b JF |
2339 | } |
2340 | ||
94b0b3e5 JF |
2341 | - (bool) subscribed { |
2342 | return [self metadata]->subscribed_; | |
2343 | } | |
2344 | ||
2345 | - (bool) setSubscribed:(bool)subscribed { | |
2346 | PackageValue *metadata([self metadata]); | |
2347 | if (metadata->subscribed_ == subscribed) | |
2348 | return false; | |
2349 | metadata->subscribed_ = subscribed; | |
2350 | return true; | |
807ae6d7 JF |
2351 | } |
2352 | ||
2353 | - (BOOL) ignored { | |
1b18f026 | 2354 | return ignored_; |
807ae6d7 JF |
2355 | } |
2356 | ||
36bb2ca2 JF |
2357 | - (NSString *) latest { |
2358 | return latest_; | |
8fe19fc1 JF |
2359 | } |
2360 | ||
36bb2ca2 JF |
2361 | - (NSString *) installed { |
2362 | return installed_; | |
3178d79b JF |
2363 | } |
2364 | ||
6a155117 JF |
2365 | - (BOOL) uninstalled { |
2366 | return installed_.empty(); | |
2367 | } | |
2368 | ||
5a09ae08 JF |
2369 | - (BOOL) valid { |
2370 | return !version_.end(); | |
2371 | } | |
2372 | ||
31f3cfff | 2373 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
677b8415 JF |
2374 | _profile(Package$upgradableAndEssential) |
2375 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2376 | if (current.end()) | |
e4f3d6e9 | 2377 | return essential && essential_; |
677b8415 | 2378 | else |
e4f3d6e9 | 2379 | return !version_.end() && version_ != current; |
677b8415 | 2380 | _end |
36bb2ca2 | 2381 | } |
3178d79b | 2382 | |
36bb2ca2 | 2383 | - (BOOL) essential { |
a1440b10 | 2384 | return essential_; |
3178d79b JF |
2385 | } |
2386 | ||
36bb2ca2 | 2387 | - (BOOL) broken { |
9bedffaa JF |
2388 | return [database_ cache][iterator_].InstBroken(); |
2389 | } | |
2390 | ||
7d2ac47f | 2391 | - (BOOL) unfiltered { |
4fa77608 | 2392 | _profile(Package$unfiltered$obsolete) |
cf48f656 | 2393 | if (_unlikely(obsolete_)) |
4fa77608 JF |
2394 | return false; |
2395 | _end | |
2396 | ||
2397 | _profile(Package$unfiltered$hasSupportingRole) | |
cf48f656 | 2398 | if (_unlikely(![self hasSupportingRole])) |
4fa77608 JF |
2399 | return false; |
2400 | _end | |
2401 | ||
e4f3d6e9 JF |
2402 | return true; |
2403 | } | |
4fa77608 | 2404 | |
e4f3d6e9 JF |
2405 | - (BOOL) visible { |
2406 | if (![self unfiltered]) | |
2407 | return false; | |
2408 | ||
fe33a23e JF |
2409 | NSString *section; |
2410 | ||
2411 | _profile(Package$visible$section) | |
2412 | section = [self section]; | |
2413 | _end | |
4fa77608 | 2414 | |
e4f3d6e9 | 2415 | _profile(Package$visible$isSectionVisible) |
45447dc3 | 2416 | if (!isSectionVisible(section)) |
4fa77608 JF |
2417 | return false; |
2418 | _end | |
2419 | ||
2420 | return true; | |
7d2ac47f JF |
2421 | } |
2422 | ||
9bedffaa | 2423 | - (BOOL) half { |
677b8415 | 2424 | unsigned char current(iterator_->CurrentState); |
9bedffaa JF |
2425 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2426 | } | |
2427 | ||
2428 | - (BOOL) halfConfigured { | |
2429 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2430 | } | |
2431 | ||
2432 | - (BOOL) halfInstalled { | |
2433 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2434 | } | |
2435 | ||
2436 | - (BOOL) hasMode { | |
2437 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2438 | return state.Mode != pkgDepCache::ModeKeep; | |
2439 | } | |
2440 | ||
2441 | - (NSString *) mode { | |
2442 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2443 | ||
2444 | switch (state.Mode) { | |
2445 | case pkgDepCache::ModeDelete: | |
2446 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
f79a4512 | 2447 | return @"PURGE"; |
9bedffaa | 2448 | else |
f79a4512 | 2449 | return @"REMOVE"; |
9bedffaa | 2450 | case pkgDepCache::ModeKeep: |
dc63e78f | 2451 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2452 | return @"REINSTALL"; |
dc63e78f JF |
2453 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
2454 | return nil;*/ | |
9bedffaa JF |
2455 | else |
2456 | return nil; | |
9bedffaa | 2457 | case pkgDepCache::ModeInstall: |
dc63e78f | 2458 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2459 | return @"REINSTALL"; |
dc63e78f | 2460 | else*/ switch (state.Status) { |
9bedffaa | 2461 | case -1: |
f79a4512 | 2462 | return @"DOWNGRADE"; |
9bedffaa | 2463 | case 0: |
f79a4512 | 2464 | return @"INSTALL"; |
9bedffaa | 2465 | case 1: |
f79a4512 | 2466 | return @"UPGRADE"; |
9bedffaa | 2467 | case 2: |
f79a4512 | 2468 | return @"NEW_INSTALL"; |
670a0494 | 2469 | _nodefault |
9bedffaa | 2470 | } |
670a0494 | 2471 | _nodefault |
9bedffaa | 2472 | } |
8fe19fc1 JF |
2473 | } |
2474 | ||
36bb2ca2 JF |
2475 | - (NSString *) id { |
2476 | return id_; | |
8fe19fc1 JF |
2477 | } |
2478 | ||
36bb2ca2 | 2479 | - (NSString *) name { |
9fcbca29 | 2480 | return name_.empty() ? id_ : name_; |
36bb2ca2 | 2481 | } |
8fe19fc1 | 2482 | |
770f2a8e | 2483 | - (UIImage *) icon { |
dec6029f | 2484 | NSString *section = [self simpleSection]; |
770f2a8e JF |
2485 | |
2486 | UIImage *icon(nil); | |
12016ee5 JF |
2487 | if (parsed_ != NULL) |
2488 | if (NSString *href = parsed_->icon_) | |
2489 | if ([href hasPrefix:@"file:///"]) | |
2490 | // XXX: correct escaping | |
2491 | icon = [UIImage imageAtPath:[href substringFromIndex:7]]; | |
770f2a8e JF |
2492 | if (icon == nil) if (section != nil) |
2493 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]; | |
b9956841 | 2494 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
189a73d0 | 2495 | if ([dicon hasPrefix:@"file:///"]) |
ac308d3e | 2496 | // XXX: correct escaping |
189a73d0 | 2497 | icon = [UIImage imageAtPath:[dicon substringFromIndex:7]]; |
770f2a8e JF |
2498 | if (icon == nil) |
2499 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
2500 | return icon; | |
36bb2ca2 | 2501 | } |
8fe19fc1 | 2502 | |
6f1a15d9 | 2503 | - (NSString *) homepage { |
12016ee5 | 2504 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
8fe19fc1 JF |
2505 | } |
2506 | ||
25a2158d | 2507 | - (NSString *) depiction { |
12016ee5 | 2508 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
25a2158d JF |
2509 | } |
2510 | ||
7aa82ca2 JF |
2511 | - (MIMEAddress *) sponsor { |
2512 | return parsed_ == NULL || parsed_->sponsor_.empty() ? nil : [MIMEAddress addressWithString:parsed_->sponsor_]; | |
795d26fc JF |
2513 | } |
2514 | ||
7aa82ca2 JF |
2515 | - (MIMEAddress *) author { |
2516 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [MIMEAddress addressWithString:parsed_->author_]; | |
77fcccaf JF |
2517 | } |
2518 | ||
dc63e78f | 2519 | - (NSString *) support { |
12016ee5 | 2520 | return parsed_ != NULL && !parsed_->bugs_.empty() ? parsed_->bugs_ : [[self source] supportForPackage:id_]; |
dc63e78f JF |
2521 | } |
2522 | ||
affeffc7 | 2523 | - (NSArray *) files { |
9fcbca29 | 2524 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
affeffc7 JF |
2525 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
2526 | ||
2527 | std::ifstream fin; | |
2528 | fin.open([path UTF8String]); | |
2529 | if (!fin.is_open()) | |
2530 | return nil; | |
2531 | ||
2532 | std::string line; | |
2533 | while (std::getline(fin, line)) | |
2534 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
2535 | ||
2536 | return files; | |
2537 | } | |
2538 | ||
5959b596 JF |
2539 | - (NSString *) state { |
2540 | @synchronized (database_) { | |
2541 | if ([database_ era] != era_ || file_.end()) | |
2542 | return nil; | |
2543 | ||
2544 | switch (iterator_->CurrentState) { | |
2545 | case pkgCache::State::NotInstalled: | |
2546 | return @"NotInstalled"; | |
2547 | case pkgCache::State::UnPacked: | |
2548 | return @"UnPacked"; | |
2549 | case pkgCache::State::HalfConfigured: | |
2550 | return @"HalfConfigured"; | |
2551 | case pkgCache::State::HalfInstalled: | |
2552 | return @"HalfInstalled"; | |
2553 | case pkgCache::State::ConfigFiles: | |
2554 | return @"ConfigFiles"; | |
2555 | case pkgCache::State::Installed: | |
2556 | return @"Installed"; | |
2557 | case pkgCache::State::TriggersAwaited: | |
2558 | return @"TriggersAwaited"; | |
2559 | case pkgCache::State::TriggersPending: | |
2560 | return @"TriggersPending"; | |
2561 | } | |
2562 | ||
2563 | return (NSString *) [NSNull null]; | |
2564 | } } | |
2565 | ||
de1ace71 JF |
2566 | - (NSString *) selection { |
2567 | @synchronized (database_) { | |
2568 | if ([database_ era] != era_ || file_.end()) | |
2569 | return nil; | |
2570 | ||
2571 | switch (iterator_->SelectedState) { | |
2572 | case pkgCache::State::Unknown: | |
2573 | return @"Unknown"; | |
2574 | case pkgCache::State::Install: | |
2575 | return @"Install"; | |
2576 | case pkgCache::State::Hold: | |
2577 | return @"Hold"; | |
2578 | case pkgCache::State::DeInstall: | |
2579 | return @"DeInstall"; | |
2580 | case pkgCache::State::Purge: | |
2581 | return @"Purge"; | |
2582 | } | |
2583 | ||
2584 | return (NSString *) [NSNull null]; | |
2585 | } } | |
2586 | ||
affeffc7 JF |
2587 | - (NSArray *) warnings { |
2588 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); | |
2589 | const char *name(iterator_.Name()); | |
2590 | ||
2591 | size_t length(strlen(name)); | |
2592 | if (length < 2) invalid: | |
43f3d7f6 | 2593 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
affeffc7 JF |
2594 | else for (size_t i(0); i != length; ++i) |
2595 | if ( | |
9dd60d81 JF |
2596 | /* XXX: technically this is not allowed */ |
2597 | (name[i] < 'A' || name[i] > 'Z') && | |
affeffc7 JF |
2598 | (name[i] < 'a' || name[i] > 'z') && |
2599 | (name[i] < '0' || name[i] > '9') && | |
2600 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
2601 | ) goto invalid; | |
2602 | ||
2603 | if (strcmp(name, "cydia") != 0) { | |
2604 | bool cydia = false; | |
7623f855 | 2605 | bool user = false; |
9dd60d81 | 2606 | bool _private = false; |
affeffc7 JF |
2607 | bool stash = false; |
2608 | ||
9dd60d81 JF |
2609 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
2610 | ||
affeffc7 JF |
2611 | if (NSArray *files = [self files]) |
2612 | for (NSString *file in files) | |
2613 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
2614 | cydia = true; | |
7623f855 JF |
2615 | else if (!user && [file isEqualToString:@"/User"]) |
2616 | user = true; | |
9dd60d81 JF |
2617 | else if (!_private && [file isEqualToString:@"/private"]) |
2618 | _private = true; | |
affeffc7 JF |
2619 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
2620 | stash = true; | |
2621 | ||
9dd60d81 JF |
2622 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
2623 | if (cydia && !repository) | |
43f3d7f6 | 2624 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
7623f855 JF |
2625 | if (user) |
2626 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
9dd60d81 | 2627 | if (_private) |
43f3d7f6 | 2628 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
affeffc7 | 2629 | if (stash) |
43f3d7f6 | 2630 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
affeffc7 JF |
2631 | } |
2632 | ||
2633 | return [warnings count] == 0 ? nil : warnings; | |
2634 | } | |
2635 | ||
2636 | - (NSArray *) applications { | |
2637 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
2638 | ||
2639 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
2640 | ||
2641 | static Pcre application_r("^/Applications/(.*)\\.app/Info.plist$"); | |
2642 | if (NSArray *files = [self files]) | |
2643 | for (NSString *file in files) | |
2644 | if (application_r(file)) { | |
2645 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
2646 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); | |
2647 | if ([id isEqualToString:me]) | |
2648 | continue; | |
2649 | ||
2650 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
2651 | if (display == nil) | |
2652 | display = application_r[1]; | |
2653 | ||
2654 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
2655 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
2656 | if (icon == nil || [icon length] == 0) | |
2657 | icon = @"icon.png"; | |
2658 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
2659 | ||
2660 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
2661 | [applications addObject:application]; | |
2662 | ||
2663 | [application addObject:id]; | |
2664 | [application addObject:display]; | |
2665 | [application addObject:url]; | |
2666 | } | |
2667 | ||
2668 | return [applications count] == 0 ? nil : applications; | |
2669 | } | |
2670 | ||
36bb2ca2 | 2671 | - (Source *) source { |
5699667a | 2672 | if (source_ == nil) { |
a1440b10 JF |
2673 | @synchronized (database_) { |
2674 | if ([database_ era] != era_ || file_.end()) | |
5699667a JF |
2675 | source_ = (Source *) [NSNull null]; |
2676 | else | |
7b33d201 | 2677 | source_ = [database_ getSource:file_.File()] ?: (Source *) [NSNull null]; |
a1440b10 | 2678 | } |
bbb879fb JF |
2679 | } |
2680 | ||
5699667a | 2681 | return source_ == (Source *) [NSNull null] ? nil : source_; |
8fe19fc1 JF |
2682 | } |
2683 | ||
9c5737d5 JF |
2684 | - (uint32_t) rank { |
2685 | return rank_; | |
2686 | } | |
2687 | ||
d84597fe JF |
2688 | - (BOOL) matches:(NSArray *)query { |
2689 | if (query == nil || [query count] == 0) | |
36bb2ca2 | 2690 | return NO; |
8fe19fc1 | 2691 | |
9c5737d5 JF |
2692 | rank_ = 0; |
2693 | ||
2694 | NSString *string; | |
36bb2ca2 | 2695 | NSRange range; |
9c5737d5 | 2696 | NSUInteger length; |
8fe19fc1 | 2697 | |
d84597fe JF |
2698 | [self parse]; |
2699 | ||
9c5737d5 | 2700 | string = [self id]; |
d84597fe JF |
2701 | length = [string length]; |
2702 | ||
2703 | for (NSString *term in query) { | |
2704 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
2705 | if (range.location != NSNotFound) | |
2706 | rank_ -= 10 * 100000 / length; | |
2707 | } | |
8fe19fc1 | 2708 | |
9c5737d5 | 2709 | string = [self name]; |
8fe19fc1 | 2710 | |
d84597fe JF |
2711 | for (NSString *term in query) { |
2712 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
2713 | if (range.location != NSNotFound) | |
2714 | rank_ -= 6 * 100000 / length; | |
2715 | } | |
c4b530a7 | 2716 | |
9c5737d5 JF |
2717 | string = [self shortDescription]; |
2718 | length = [string length]; | |
d84597fe | 2719 | NSUInteger stop(std::min<NSUInteger>(length, 100)); |
df289c5a | 2720 | |
d84597fe JF |
2721 | for (NSString *term in query) { |
2722 | range = [string rangeOfString:term options:MatchCompareOptions_ range:NSMakeRange(0, stop)]; | |
2723 | if (range.location != NSNotFound) | |
2724 | rank_ -= 2 * 100000 / length; | |
2725 | } | |
8fe19fc1 | 2726 | |
9c5737d5 | 2727 | return rank_ != 0; |
36bb2ca2 | 2728 | } |
8fe19fc1 | 2729 | |
7b0ce2da | 2730 | - (bool) hasSupportingRole { |
97a3d89e | 2731 | if (role_ == 0) |
6d9712c4 | 2732 | return true; |
97a3d89e | 2733 | if (role_ == 1) |
7b0ce2da JF |
2734 | return true; |
2735 | if ([Role_ isEqualToString:@"User"]) | |
2736 | return false; | |
97a3d89e | 2737 | if (role_ == 2) |
7b0ce2da JF |
2738 | return true; |
2739 | if ([Role_ isEqualToString:@"Hacker"]) | |
2740 | return false; | |
97a3d89e | 2741 | if (role_ == 3) |
7b0ce2da JF |
2742 | return true; |
2743 | if ([Role_ isEqualToString:@"Developer"]) | |
2744 | return false; | |
2745 | _assert(false); | |
6d9712c4 JF |
2746 | } |
2747 | ||
82aa2434 JF |
2748 | - (NSArray *) tags { |
2749 | return tags_; | |
2750 | } | |
2751 | ||
6d9712c4 JF |
2752 | - (BOOL) hasTag:(NSString *)tag { |
2753 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
2754 | } | |
2755 | ||
c390d3ab | 2756 | - (NSString *) primaryPurpose { |
7b33d201 | 2757 | for (NSString *tag in (NSArray *) tags_) |
c390d3ab JF |
2758 | if ([tag hasPrefix:@"purpose::"]) |
2759 | return [tag substringFromIndex:9]; | |
2760 | return nil; | |
2761 | } | |
2762 | ||
770f2a8e JF |
2763 | - (NSArray *) purposes { |
2764 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
7b33d201 | 2765 | for (NSString *tag in (NSArray *) tags_) |
770f2a8e JF |
2766 | if ([tag hasPrefix:@"purpose::"]) |
2767 | [purposes addObject:[tag substringFromIndex:9]]; | |
2768 | return [purposes count] == 0 ? nil : purposes; | |
2769 | } | |
2770 | ||
3bd1c2a2 JF |
2771 | - (bool) isCommercial { |
2772 | return [self hasTag:@"cydia::commercial"]; | |
2773 | } | |
2774 | ||
cd95e390 JF |
2775 | - (void) setIndex:(size_t)index { |
2776 | if (metadata_->index_ != index) | |
2777 | metadata_->index_ = index; | |
2778 | } | |
2779 | ||
df213583 JF |
2780 | - (CYString &) cyname { |
2781 | return name_.empty() ? id_ : name_; | |
f79a4512 JF |
2782 | } |
2783 | ||
f79a4512 JF |
2784 | - (uint32_t) compareBySection:(NSArray *)sections { |
2785 | NSString *section([self section]); | |
2786 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
2787 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
2788 | return i; | |
36bb2ca2 | 2789 | } |
3178d79b | 2790 | |
f79a4512 | 2791 | return _not(uint32_t); |
36bb2ca2 | 2792 | } |
3178d79b | 2793 | |
dc63e78f | 2794 | - (void) clear { |
c6ca67ba | 2795 | @synchronized (database_) { |
dc63e78f JF |
2796 | pkgProblemResolver *resolver = [database_ resolver]; |
2797 | resolver->Clear(iterator_); | |
c6ca67ba JF |
2798 | |
2799 | pkgCacheFile &cache([database_ cache]); | |
2800 | cache->SetReInstall(iterator_, false); | |
2801 | cache->MarkKeep(iterator_, false); | |
2802 | } } | |
dc63e78f | 2803 | |
36bb2ca2 | 2804 | - (void) install { |
c6ca67ba | 2805 | @synchronized (database_) { |
36bb2ca2 JF |
2806 | pkgProblemResolver *resolver = [database_ resolver]; |
2807 | resolver->Clear(iterator_); | |
2808 | resolver->Protect(iterator_); | |
c6ca67ba | 2809 | |
36bb2ca2 | 2810 | pkgCacheFile &cache([database_ cache]); |
c6ca67ba | 2811 | cache->SetReInstall(iterator_, false); |
36bb2ca2 | 2812 | cache->MarkInstall(iterator_, false); |
c6ca67ba | 2813 | |
36bb2ca2 JF |
2814 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
2815 | if (!state.Install()) | |
2816 | cache->SetReInstall(iterator_, true); | |
c6ca67ba | 2817 | } } |
68a238ec | 2818 | |
36bb2ca2 | 2819 | - (void) remove { |
c6ca67ba | 2820 | @synchronized (database_) { |
36bb2ca2 JF |
2821 | pkgProblemResolver *resolver = [database_ resolver]; |
2822 | resolver->Clear(iterator_); | |
36bb2ca2 | 2823 | resolver->Remove(iterator_); |
c6ca67ba JF |
2824 | resolver->Protect(iterator_); |
2825 | ||
2826 | pkgCacheFile &cache([database_ cache]); | |
2827 | cache->SetReInstall(iterator_, false); | |
2828 | cache->MarkDelete(iterator_, true); | |
2829 | } } | |
8fe19fc1 | 2830 | |
d84597fe | 2831 | - (bool) isUnfilteredAndSearchedForBy:(NSArray *)query { |
808c6eb6 JF |
2832 | _profile(Package$isUnfilteredAndSearchedForBy) |
2833 | bool value(true); | |
2834 | ||
2835 | _profile(Package$isUnfilteredAndSearchedForBy$Unfiltered) | |
2836 | value &= [self unfiltered]; | |
2837 | _end | |
2838 | ||
2839 | _profile(Package$isUnfilteredAndSearchedForBy$Match) | |
d84597fe | 2840 | value &= [self matches:query]; |
808c6eb6 JF |
2841 | _end |
2842 | ||
0a3b45ef | 2843 | return value; |
808c6eb6 | 2844 | _end |
36bb2ca2 | 2845 | } |
8fe19fc1 | 2846 | |
01d93940 | 2847 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search { |
ef055c6c JF |
2848 | if ([search length] == 0) |
2849 | return false; | |
2850 | ||
01d93940 JF |
2851 | _profile(Package$isUnfilteredAndSelectedForBy) |
2852 | bool value(true); | |
2853 | ||
2854 | _profile(Package$isUnfilteredAndSelectedForBy$Unfiltered) | |
2855 | value &= [self unfiltered]; | |
2856 | _end | |
2857 | ||
2858 | _profile(Package$isUnfilteredAndSelectedForBy$Match) | |
2859 | value &= [[self name] compare:search options:MatchCompareOptions_ range:NSMakeRange(0, [search length])] == NSOrderedSame; | |
2860 | _end | |
2861 | ||
2862 | return value; | |
2863 | _end | |
2864 | } | |
2865 | ||
e4f3d6e9 | 2866 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number { |
97a3d89e | 2867 | return ![self uninstalled] && (![number boolValue] && role_ != 7 || [self unfiltered]); |
36bb2ca2 | 2868 | } |
8fe19fc1 | 2869 | |
670a0494 | 2870 | - (bool) isVisibleInSection:(NSString *)name { |
e4f3d6e9 | 2871 | NSString *section([self section]); |
5a09ae08 | 2872 | |
e4f3d6e9 JF |
2873 | return ( |
2874 | name == nil || | |
2875 | section == nil && [name length] == 0 || | |
2876 | [name isEqualToString:section] | |
2877 | ) && [self visible]; | |
7b0ce2da JF |
2878 | } |
2879 | ||
0a3b45ef JF |
2880 | - (bool) isVisibleInSource:(Source *)source { |
2881 | return [self source] == source && [self visible]; | |
36bb2ca2 | 2882 | } |
8fe19fc1 | 2883 | |
36bb2ca2 JF |
2884 | @end |
2885 | /* }}} */ | |
2886 | /* Section Class {{{ */ | |
2887 | @interface Section : NSObject { | |
7b33d201 | 2888 | _H<NSString> name_; |
808c6eb6 | 2889 | unichar index_; |
36bb2ca2 JF |
2890 | size_t row_; |
2891 | size_t count_; | |
7b33d201 | 2892 | _H<NSString> localized_; |
36bb2ca2 | 2893 | } |
3178d79b | 2894 | |
677b8415 | 2895 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9fcbca29 JF |
2896 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
2897 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
2898 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
808c6eb6 | 2899 | - (Section *) initWithIndex:(unichar)index row:(size_t)row; |
36bb2ca2 | 2900 | - (NSString *) name; |
808c6eb6 | 2901 | - (unichar) index; |
f79a4512 | 2902 | |
36bb2ca2 JF |
2903 | - (size_t) row; |
2904 | - (size_t) count; | |
f79a4512 JF |
2905 | |
2906 | - (void) addToRow; | |
36bb2ca2 | 2907 | - (void) addToCount; |
b19871dd | 2908 | |
f79a4512 | 2909 | - (void) setCount:(size_t)count; |
677b8415 | 2910 | - (NSString *) localized; |
f79a4512 | 2911 | |
36bb2ca2 | 2912 | @end |
b19871dd | 2913 | |
36bb2ca2 | 2914 | @implementation Section |
b19871dd | 2915 | |
677b8415 | 2916 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9fcbca29 JF |
2917 | NSString *lhs(localized_); |
2918 | NSString *rhs([section localized]); | |
6d9712c4 | 2919 | |
9fcbca29 | 2920 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
6d9712c4 JF |
2921 | unichar lhc = [lhs characterAtIndex:0]; |
2922 | unichar rhc = [rhs characterAtIndex:0]; | |
2923 | ||
2924 | if (isalpha(lhc) && !isalpha(rhc)) | |
2925 | return NSOrderedAscending; | |
2926 | else if (!isalpha(lhc) && isalpha(rhc)) | |
2927 | return NSOrderedDescending; | |
9fcbca29 | 2928 | }*/ |
6d9712c4 | 2929 | |
68f1828e | 2930 | return [lhs compare:rhs options:LaxCompareOptions_]; |
6d9712c4 JF |
2931 | } |
2932 | ||
9fcbca29 JF |
2933 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
2934 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
2935 | if (localized != nil) | |
7b33d201 | 2936 | localized_ = localized; |
9fcbca29 JF |
2937 | } return self; |
2938 | } | |
2939 | ||
2940 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
2941 | return [self initWithName:name row:0 localize:localize]; | |
6d9712c4 JF |
2942 | } |
2943 | ||
9fcbca29 | 2944 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
36bb2ca2 | 2945 | if ((self = [super init]) != nil) { |
7b33d201 | 2946 | name_ = name; |
808c6eb6 JF |
2947 | index_ = '\0'; |
2948 | row_ = row; | |
9fcbca29 | 2949 | if (localize) |
7b33d201 | 2950 | localized_ = LocalizeSection(name_); |
808c6eb6 JF |
2951 | } return self; |
2952 | } | |
2953 | ||
f79a4512 | 2954 | /* XXX: localize the index thingees */ |
808c6eb6 JF |
2955 | - (Section *) initWithIndex:(unichar)index row:(size_t)row { |
2956 | if ((self = [super init]) != nil) { | |
7b33d201 | 2957 | name_ = [NSString stringWithCharacters:&index length:1]; |
808c6eb6 | 2958 | index_ = index; |
36bb2ca2 | 2959 | row_ = row; |
b19871dd JF |
2960 | } return self; |
2961 | } | |
2962 | ||
36bb2ca2 JF |
2963 | - (NSString *) name { |
2964 | return name_; | |
2965 | } | |
2966 | ||
808c6eb6 JF |
2967 | - (unichar) index { |
2968 | return index_; | |
2969 | } | |
2970 | ||
36bb2ca2 JF |
2971 | - (size_t) row { |
2972 | return row_; | |
2973 | } | |
2974 | ||
2975 | - (size_t) count { | |
2976 | return count_; | |
2977 | } | |
2978 | ||
f79a4512 JF |
2979 | - (void) addToRow { |
2980 | ++row_; | |
2981 | } | |
2982 | ||
36bb2ca2 JF |
2983 | - (void) addToCount { |
2984 | ++count_; | |
2985 | } | |
2986 | ||
f79a4512 JF |
2987 | - (void) setCount:(size_t)count { |
2988 | count_ = count; | |
2989 | } | |
2990 | ||
2991 | - (NSString *) localized { | |
2992 | return localized_; | |
2993 | } | |
2994 | ||
b19871dd JF |
2995 | @end |
2996 | /* }}} */ | |
2997 | ||
670a0494 | 2998 | static NSString *Colon_; |
70750ab3 | 2999 | NSString *Elision_; |
670a0494 JF |
3000 | static NSString *Error_; |
3001 | static NSString *Warning_; | |
3002 | ||
9f9ae81c JF |
3003 | class CydiaLogCleaner : |
3004 | public pkgArchiveCleaner | |
3005 | { | |
3006 | protected: | |
3007 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
3008 | unlink(File); | |
3009 | } | |
3010 | }; | |
3011 | ||
36bb2ca2 JF |
3012 | /* Database Implementation {{{ */ |
3013 | @implementation Database | |
ec97ef06 | 3014 | |
770f2a8e | 3015 | + (Database *) sharedInstance { |
7b33d201 | 3016 | static _H<Database> instance; |
770f2a8e | 3017 | if (instance == nil) |
7b33d201 | 3018 | instance = [[[Database alloc] init] autorelease]; |
770f2a8e JF |
3019 | return instance; |
3020 | } | |
3021 | ||
a1440b10 JF |
3022 | - (unsigned) era { |
3023 | return era_; | |
3024 | } | |
3025 | ||
0944377b JF |
3026 | - (void) releasePackages { |
3027 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3028 | CFArrayRemoveAllValues(packages_); | |
3029 | } | |
3030 | ||
36bb2ca2 | 3031 | - (void) dealloc { |
3931b718 | 3032 | // XXX: actually implement this thing |
36bb2ca2 | 3033 | _assert(false); |
0944377b | 3034 | [self releasePackages]; |
f79a4512 | 3035 | apr_pool_destroy(pool_); |
0944377b | 3036 | NSRecycleZone(zone_); |
36bb2ca2 JF |
3037 | [super dealloc]; |
3038 | } | |
ec97ef06 | 3039 | |
d13edf44 | 3040 | - (void) _readCydia:(NSNumber *)fd { |
77fcccaf JF |
3041 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3042 | std::istream is(&ib); | |
3043 | std::string line; | |
3044 | ||
bc8cd583 JF |
3045 | static Pcre finish_r("^finish:([^:]*)$"); |
3046 | ||
77fcccaf | 3047 | while (std::getline(is, line)) { |
d13edf44 JF |
3048 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3049 | ||
77fcccaf | 3050 | const char *data(line.c_str()); |
bc8cd583 | 3051 | size_t size = line.size(); |
c390d3ab | 3052 | lprintf("C:%s\n", data); |
bc8cd583 JF |
3053 | |
3054 | if (finish_r(data, size)) { | |
3055 | NSString *finish = finish_r[1]; | |
3056 | int index = [Finishes_ indexOfObject:finish]; | |
3057 | if (index != INT_MAX && index > Finish_) | |
3058 | Finish_ = index; | |
3059 | } | |
d13edf44 JF |
3060 | |
3061 | [pool release]; | |
77fcccaf JF |
3062 | } |
3063 | ||
670a0494 | 3064 | _assume(false); |
77fcccaf JF |
3065 | } |
3066 | ||
d13edf44 | 3067 | - (void) _readStatus:(NSNumber *)fd { |
36bb2ca2 JF |
3068 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3069 | std::istream is(&ib); | |
3070 | std::string line; | |
ec97ef06 | 3071 | |
7b0ce2da JF |
3072 | static Pcre conffile_r("^status: [^ ]* : conffile-prompt : (.*?) *$"); |
3073 | static Pcre pmstatus_r("^([^:]*):([^:]*):([^:]*):(.*)$"); | |
ec97ef06 | 3074 | |
36bb2ca2 | 3075 | while (std::getline(is, line)) { |
d13edf44 JF |
3076 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3077 | ||
36bb2ca2 | 3078 | const char *data(line.c_str()); |
670a0494 | 3079 | size_t size(line.size()); |
c390d3ab | 3080 | lprintf("S:%s\n", data); |
ec97ef06 | 3081 | |
a08145a8 JF |
3082 | if (conffile_r(data, size)) { |
3083 | // status: /fail : conffile-prompt : '/fail' '/fail.dpkg-new' 1 1 | |
4187453f | 3084 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:conffile_r[1] waitUntilDone:YES]; |
a08145a8 JF |
3085 | } else if (strncmp(data, "status: ", 8) == 0) { |
3086 | // status: <package>: {unpacked,half-configured,installed} | |
389133be | 3087 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 8)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 JF |
3088 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3089 | } else if (strncmp(data, "processing: ", 12) == 0) { | |
3090 | // processing: configure: config-test | |
389133be | 3091 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 12)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 | 3092 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
6915b806 | 3093 | } else if (pmstatus_r(data, size)) { |
31f3cfff | 3094 | std::string type([pmstatus_r[1] UTF8String]); |
4a2dc82e | 3095 | |
6915b806 | 3096 | NSString *package = pmstatus_r[2]; |
4a2dc82e JF |
3097 | if ([package isEqualToString:@"dpkg-exec"]) |
3098 | package = nil; | |
31f3cfff | 3099 | |
5a09ae08 | 3100 | float percent([pmstatus_r[3] floatValue]); |
6915b806 | 3101 | [progress_ performSelectorOnMainThread:@selector(setProgressPercent:) withObject:[NSNumber numberWithFloat:(percent / 100)] waitUntilDone:YES]; |
5a09ae08 JF |
3102 | |
3103 | NSString *string = pmstatus_r[4]; | |
5a09ae08 | 3104 | |
6915b806 | 3105 | if (type == "pmerror") { |
389133be | 3106 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeError forPackage:package]); |
6915b806 JF |
3107 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3108 | } else if (type == "pmstatus") { | |
389133be | 3109 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeStatus forPackage:package]); |
6915b806 JF |
3110 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3111 | } else if (type == "pmconffile") | |
4187453f | 3112 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:string waitUntilDone:YES]; |
670a0494 JF |
3113 | else |
3114 | lprintf("E:unknown pmstatus\n"); | |
3115 | } else | |
3116 | lprintf("E:unknown status\n"); | |
d13edf44 JF |
3117 | |
3118 | [pool release]; | |
36bb2ca2 | 3119 | } |
ec97ef06 | 3120 | |
670a0494 | 3121 | _assume(false); |
36bb2ca2 | 3122 | } |
ec97ef06 | 3123 | |
d13edf44 | 3124 | - (void) _readOutput:(NSNumber *)fd { |
36bb2ca2 JF |
3125 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3126 | std::istream is(&ib); | |
3127 | std::string line; | |
3128 | ||
5a09ae08 | 3129 | while (std::getline(is, line)) { |
d13edf44 JF |
3130 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3131 | ||
c390d3ab | 3132 | lprintf("O:%s\n", line.c_str()); |
27024935 | 3133 | |
389133be | 3134 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:line.c_str()] ofType:kCydiaProgressEventTypeInformation]); |
6915b806 | 3135 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
d13edf44 JF |
3136 | |
3137 | [pool release]; | |
5a09ae08 | 3138 | } |
36bb2ca2 | 3139 | |
670a0494 | 3140 | _assume(false); |
ec97ef06 JF |
3141 | } |
3142 | ||
8b29f8e6 JF |
3143 | - (FILE *) input { |
3144 | return input_; | |
3145 | } | |
3146 | ||
36bb2ca2 | 3147 | - (Package *) packageWithName:(NSString *)name { |
3dd53516 | 3148 | @synchronized (self) { |
5a09ae08 JF |
3149 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3150 | return nil; | |
36bb2ca2 | 3151 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
f79a4512 | 3152 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:pool_ database:self]; |
28ce8704 | 3153 | } } |
36bb2ca2 | 3154 | |
eb30da80 | 3155 | - (id) init { |
ec97ef06 | 3156 | if ((self = [super init]) != nil) { |
5a09ae08 | 3157 | policy_ = NULL; |
36bb2ca2 JF |
3158 | records_ = NULL; |
3159 | resolver_ = NULL; | |
3160 | fetcher_ = NULL; | |
3161 | lock_ = NULL; | |
ec97ef06 | 3162 | |
f79a4512 JF |
3163 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
3164 | apr_pool_create(&pool_, NULL); | |
3165 | ||
9f357d11 JF |
3166 | size_t capacity(MetaFile_->active_); |
3167 | if (capacity == 0) | |
3168 | capacity = 16384; | |
3169 | else | |
3170 | capacity += 1024; | |
3171 | ||
3172 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); | |
7b33d201 | 3173 | sourceList_ = [NSMutableArray arrayWithCapacity:16]; |
ec97ef06 | 3174 | |
36bb2ca2 | 3175 | int fds[2]; |
ec97ef06 | 3176 | |
77fcccaf JF |
3177 | _assert(pipe(fds) != -1); |
3178 | cydiafd_ = fds[1]; | |
3179 | ||
3180 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
bc8cd583 | 3181 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
77fcccaf JF |
3182 | |
3183 | [NSThread | |
3184 | detachNewThreadSelector:@selector(_readCydia:) | |
3185 | toTarget:self | |
3931b718 | 3186 | withObject:[NSNumber numberWithInt:fds[0]] |
77fcccaf JF |
3187 | ]; |
3188 | ||
36bb2ca2 JF |
3189 | _assert(pipe(fds) != -1); |
3190 | statusfd_ = fds[1]; | |
ec97ef06 | 3191 | |
36bb2ca2 JF |
3192 | [NSThread |
3193 | detachNewThreadSelector:@selector(_readStatus:) | |
3194 | toTarget:self | |
3931b718 | 3195 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3196 | ]; |
ec97ef06 | 3197 | |
8b29f8e6 JF |
3198 | _assert(pipe(fds) != -1); |
3199 | _assert(dup2(fds[0], 0) != -1); | |
3200 | _assert(close(fds[0]) != -1); | |
3201 | ||
3202 | input_ = fdopen(fds[1], "a"); | |
3203 | ||
36bb2ca2 JF |
3204 | _assert(pipe(fds) != -1); |
3205 | _assert(dup2(fds[1], 1) != -1); | |
3206 | _assert(close(fds[1]) != -1); | |
3207 | ||
3208 | [NSThread | |
3209 | detachNewThreadSelector:@selector(_readOutput:) | |
3210 | toTarget:self | |
3931b718 | 3211 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3212 | ]; |
ec97ef06 JF |
3213 | } return self; |
3214 | } | |
3215 | ||
36bb2ca2 JF |
3216 | - (pkgCacheFile &) cache { |
3217 | return cache_; | |
ec97ef06 JF |
3218 | } |
3219 | ||
5a09ae08 JF |
3220 | - (pkgDepCache::Policy *) policy { |
3221 | return policy_; | |
3222 | } | |
3223 | ||
36bb2ca2 JF |
3224 | - (pkgRecords *) records { |
3225 | return records_; | |
ec97ef06 JF |
3226 | } |
3227 | ||
36bb2ca2 JF |
3228 | - (pkgProblemResolver *) resolver { |
3229 | return resolver_; | |
ec97ef06 JF |
3230 | } |
3231 | ||
36bb2ca2 JF |
3232 | - (pkgAcquire &) fetcher { |
3233 | return *fetcher_; | |
ec97ef06 JF |
3234 | } |
3235 | ||
a3328c28 JF |
3236 | - (pkgSourceList &) list { |
3237 | return *list_; | |
3238 | } | |
3239 | ||
36bb2ca2 | 3240 | - (NSArray *) packages { |
077e9d90 | 3241 | return (NSArray *) packages_; |
ec97ef06 JF |
3242 | } |
3243 | ||
7b0ce2da | 3244 | - (NSArray *) sources { |
34f70f5d | 3245 | return sourceList_; |
7b0ce2da JF |
3246 | } |
3247 | ||
d669236d GP |
3248 | - (Source *) sourceWithKey:(NSString *)key { |
3249 | for (Source *source in [self sources]) { | |
3250 | if ([[source key] isEqualToString:key]) | |
3251 | return source; | |
3252 | } return nil; | |
3253 | } | |
3254 | ||
670a0494 JF |
3255 | - (bool) popErrorWithTitle:(NSString *)title { |
3256 | bool fatal(false); | |
670a0494 JF |
3257 | |
3258 | while (!_error->empty()) { | |
3259 | std::string error; | |
3260 | bool warning(!_error->PopMessage(error)); | |
3261 | if (!warning) | |
3262 | fatal = true; | |
cb6e2ccf | 3263 | |
670a0494 JF |
3264 | for (;;) { |
3265 | size_t size(error.size()); | |
3266 | if (size == 0 || error[size - 1] != '\n') | |
3267 | break; | |
3268 | error.resize(size - 1); | |
3269 | } | |
cb6e2ccf | 3270 | |
670a0494 JF |
3271 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); |
3272 | ||
389133be | 3273 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
670a0494 JF |
3274 | } |
3275 | ||
670a0494 JF |
3276 | return fatal; |
3277 | } | |
3278 | ||
3279 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3280 | return [self popErrorWithTitle:title] || !success; | |
3281 | } | |
3282 | ||
d13edf44 | 3283 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
3dd53516 JF |
3284 | @synchronized (self) { |
3285 | ++era_; | |
a1440b10 | 3286 | |
0944377b | 3287 | [self releasePackages]; |
ab3f6a01 | 3288 | |
34f70f5d JF |
3289 | sourceMap_.clear(); |
3290 | [sourceList_ removeAllObjects]; | |
843e75b8 | 3291 | |
36bb2ca2 | 3292 | _error->Discard(); |
5a09ae08 | 3293 | |
36bb2ca2 | 3294 | delete list_; |
5a09ae08 | 3295 | list_ = NULL; |
36bb2ca2 JF |
3296 | manager_ = NULL; |
3297 | delete lock_; | |
5a09ae08 | 3298 | lock_ = NULL; |
36bb2ca2 | 3299 | delete fetcher_; |
5a09ae08 | 3300 | fetcher_ = NULL; |
36bb2ca2 | 3301 | delete resolver_; |
5a09ae08 | 3302 | resolver_ = NULL; |
36bb2ca2 | 3303 | delete records_; |
5a09ae08 JF |
3304 | records_ = NULL; |
3305 | delete policy_; | |
3306 | policy_ = NULL; | |
36bb2ca2 | 3307 | |
5a09ae08 | 3308 | cache_.Close(); |
8b29f8e6 | 3309 | |
f79a4512 | 3310 | apr_pool_clear(pool_); |
a020a50e | 3311 | |
f79a4512 | 3312 | NSRecycleZone(zone_); |
a020a50e | 3313 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3314 | |
7376b55c JF |
3315 | int chk(creat("/tmp/cydia.chk", 0644)); |
3316 | if (chk != -1) | |
3317 | close(chk); | |
3318 | ||
4ba8f30a JF |
3319 | if (invocation != nil) |
3320 | [invocation invoke]; | |
3321 | ||
670a0494 JF |
3322 | NSString *title(UCLocalize("DATABASE")); |
3323 | ||
9487f027 | 3324 | _trace(); |
124cea03 JF |
3325 | OpProgress progress; |
3326 | while (!cache_.Open(progress, true)) { pop: | |
8b29f8e6 | 3327 | std::string error; |
670a0494 | 3328 | bool warning(!_error->PopMessage(error)); |
c390d3ab | 3329 | lprintf("cache_.Open():[%s]\n", error.c_str()); |
5a09ae08 JF |
3330 | |
3331 | if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3332 | [delegate_ repairWithSelector:@selector(configure)]; | |
3333 | else if (error == "The package lists or status file could not be parsed or opened.") | |
3334 | [delegate_ repairWithSelector:@selector(update)]; | |
37bf1e1b | 3335 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") |
f549c55a JF |
3336 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") |
3337 | // else if (error == "Malformed Status line") | |
9ea8d159 | 3338 | // else if (error == "The list of sources could not be read.") |
efa53fa9 | 3339 | else { |
389133be | 3340 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
efa53fa9 GP |
3341 | return; |
3342 | } | |
5a09ae08 | 3343 | |
670a0494 JF |
3344 | if (warning) |
3345 | goto pop; | |
3346 | _error->Discard(); | |
36bb2ca2 | 3347 | } |
9487f027 | 3348 | _trace(); |
36bb2ca2 | 3349 | |
7376b55c JF |
3350 | unlink("/tmp/cydia.chk"); |
3351 | ||
31bc18a7 | 3352 | now_ = [[NSDate date] timeIntervalSince1970]; |
36bb2ca2 | 3353 | |
5a09ae08 | 3354 | policy_ = new pkgDepCache::Policy(); |
36bb2ca2 JF |
3355 | records_ = new pkgRecords(cache_); |
3356 | resolver_ = new pkgProblemResolver(cache_); | |
3357 | fetcher_ = new pkgAcquire(&status_); | |
3358 | lock_ = NULL; | |
3359 | ||
3360 | list_ = new pkgSourceList(); | |
670a0494 JF |
3361 | if ([self popErrorWithTitle:title forOperation:list_->ReadMainList()]) |
3362 | return; | |
3363 | ||
3364 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { | |
389133be | 3365 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("COUNTS_NONZERO_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3366 | return; |
3367 | } | |
36bb2ca2 | 3368 | |
670a0494 JF |
3369 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3370 | return; | |
9bedffaa JF |
3371 | |
3372 | if (cache_->BrokenCount() != 0) { | |
670a0494 JF |
3373 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3374 | return; | |
3375 | ||
3376 | if (cache_->BrokenCount() != 0) { | |
389133be | 3377 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("STILL_BROKEN_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3378 | return; |
3379 | } | |
3380 | ||
3381 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) | |
3382 | return; | |
9bedffaa JF |
3383 | } |
3384 | ||
68d927e2 | 3385 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
34f70f5d JF |
3386 | Source *object([[[Source alloc] initWithMetaIndex:*source inPool:pool_] autorelease]); |
3387 | [sourceList_ addObject:object]; | |
3388 | ||
68d927e2 JF |
3389 | std::vector<pkgIndexFile *> *indices = (*source)->GetIndexFiles(); |
3390 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) | |
3391 | // XXX: this could be more intelligent | |
3392 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3393 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
34f70f5d JF |
3394 | if (!cached.end()) |
3395 | sourceMap_[cached->ID] = object; | |
68d927e2 | 3396 | } |
36bb2ca2 | 3397 | } |
68d927e2 | 3398 | |
677b8415 | 3399 | { |
9fcbca29 | 3400 | /*std::vector<Package *> packages; |
677b8415 | 3401 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
9fcbca29 JF |
3402 | packages_ = nil;*/ |
3403 | ||
677b8415 JF |
3404 | _trace(); |
3405 | ||
3406 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) | |
3407 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:pool_ database:self]) | |
9fcbca29 | 3408 | //packages.push_back(package); |
7b33d201 | 3409 | CFArrayAppendValue(packages_, CFRetain(package)); |
677b8415 JF |
3410 | |
3411 | _trace(); | |
3412 | ||
9fcbca29 | 3413 | /*if (packages.empty()) |
677b8415 JF |
3414 | packages_ = [[NSArray alloc] init]; |
3415 | else | |
3416 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9fcbca29 JF |
3417 | _trace();*/ |
3418 | ||
de42680b JF |
3419 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(16)]; |
3420 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; | |
3421 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
9fcbca29 JF |
3422 | |
3423 | /*_trace(); | |
3424 | PrintTimes(); | |
3425 | _trace();*/ | |
3426 | ||
3427 | _trace(); | |
3428 | ||
3429 | /*if (!packages.empty()) | |
3430 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL);*/ | |
3431 | //std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3432 | ||
eef4ccaf JF |
3433 | //CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
3434 | ||
077e9d90 | 3435 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
9fcbca29 JF |
3436 | |
3437 | //[packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
677b8415 JF |
3438 | |
3439 | _trace(); | |
cd95e390 JF |
3440 | |
3441 | size_t count(CFArrayGetCount(packages_)); | |
9f357d11 JF |
3442 | MetaFile_->active_ = count; |
3443 | ||
cd95e390 JF |
3444 | for (size_t index(0); index != count; ++index) |
3445 | [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; | |
3446 | ||
3447 | _trace(); | |
677b8415 | 3448 | } |
d13edf44 | 3449 | } } |
ec97ef06 | 3450 | |
c6ca67ba JF |
3451 | - (void) clear { |
3452 | @synchronized (self) { | |
3453 | delete resolver_; | |
3454 | resolver_ = new pkgProblemResolver(cache_); | |
3455 | ||
50c1653e JF |
3456 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) |
3457 | if (!cache_[iterator].Keep()) | |
c6ca67ba | 3458 | cache_->MarkKeep(iterator, false); |
50c1653e | 3459 | else if ((cache_[iterator].iFlags & pkgDepCache::ReInstall) != 0) |
c6ca67ba | 3460 | cache_->SetReInstall(iterator, false); |
c6ca67ba JF |
3461 | } } |
3462 | ||
5a09ae08 JF |
3463 | - (void) configure { |
3464 | NSString *dpkg = [NSString stringWithFormat:@"dpkg --configure -a --status-fd %u", statusfd_]; | |
985d2dff | 3465 | _trace(); |
5a09ae08 | 3466 | system([dpkg UTF8String]); |
985d2dff | 3467 | _trace(); |
5a09ae08 JF |
3468 | } |
3469 | ||
670a0494 JF |
3470 | - (bool) clean { |
3471 | // XXX: I don't remember this condition | |
77fcccaf | 3472 | if (lock_ != NULL) |
670a0494 | 3473 | return false; |
77fcccaf JF |
3474 | |
3475 | FileFd Lock; | |
3476 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
3477 | |
3478 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
3479 | ||
3480 | if ([self popErrorWithTitle:title]) | |
3481 | return false; | |
77fcccaf JF |
3482 | |
3483 | pkgAcquire fetcher; | |
3484 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
3485 | ||
9f9ae81c | 3486 | CydiaLogCleaner cleaner; |
670a0494 JF |
3487 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
3488 | return false; | |
3489 | ||
3490 | return true; | |
77fcccaf JF |
3491 | } |
3492 | ||
670a0494 | 3493 | - (bool) prepare { |
744f398e JF |
3494 | fetcher_->Shutdown(); |
3495 | ||
36bb2ca2 JF |
3496 | pkgRecords records(cache_); |
3497 | ||
3498 | lock_ = new FileFd(); | |
3499 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
3500 | |
3501 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
3502 | ||
3503 | if ([self popErrorWithTitle:title]) | |
3504 | return false; | |
36bb2ca2 JF |
3505 | |
3506 | pkgSourceList list; | |
670a0494 JF |
3507 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3508 | return false; | |
36bb2ca2 JF |
3509 | |
3510 | manager_ = (_system->CreatePM(cache_)); | |
670a0494 JF |
3511 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
3512 | return false; | |
3513 | ||
3514 | return true; | |
ec97ef06 JF |
3515 | } |
3516 | ||
36bb2ca2 | 3517 | - (void) perform { |
be860cc8 JF |
3518 | bool substrate(RestartSubstrate_); |
3519 | RestartSubstrate_ = false; | |
3520 | ||
670a0494 JF |
3521 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
3522 | ||
a72074b2 JF |
3523 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
3524 | pkgSourceList list; | |
670a0494 JF |
3525 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3526 | return; | |
a72074b2 JF |
3527 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3528 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3529 | } | |
a0be02eb | 3530 | |
dcaecde2 | 3531 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
7ffd70fd | 3532 | |
eeb9b112 JF |
3533 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
3534 | _trace(); | |
6e90508f | 3535 | [self popErrorWithTitle:title]; |
36bb2ca2 | 3536 | return; |
eeb9b112 JF |
3537 | } |
3538 | ||
3539 | bool failed = false; | |
3540 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
3541 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
3542 | continue; | |
6204f56a JF |
3543 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
3544 | continue; | |
eeb9b112 | 3545 | |
eeb9b112 | 3546 | failed = true; |
eeb9b112 JF |
3547 | } |
3548 | ||
dcaecde2 | 3549 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
54043703 | 3550 | |
eeb9b112 JF |
3551 | if (failed) { |
3552 | _trace(); | |
3553 | return; | |
3554 | } | |
36bb2ca2 | 3555 | |
be860cc8 JF |
3556 | if (substrate) |
3557 | RestartSubstrate_ = true; | |
3558 | ||
36bb2ca2 JF |
3559 | _system->UnLock(); |
3560 | pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_); | |
3561 | ||
eeb9b112 JF |
3562 | if (_error->PendingError()) { |
3563 | _trace(); | |
36bb2ca2 | 3564 | return; |
eeb9b112 JF |
3565 | } |
3566 | ||
3567 | if (result == pkgPackageManager::Failed) { | |
3568 | _trace(); | |
36bb2ca2 | 3569 | return; |
eeb9b112 JF |
3570 | } |
3571 | ||
3572 | if (result != pkgPackageManager::Completed) { | |
3573 | _trace(); | |
36bb2ca2 | 3574 | return; |
eeb9b112 | 3575 | } |
a0be02eb | 3576 | |
a72074b2 JF |
3577 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
3578 | pkgSourceList list; | |
670a0494 JF |
3579 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3580 | return; | |
a72074b2 JF |
3581 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3582 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3583 | } | |
3584 | ||
3585 | if (![before isEqualToArray:after]) | |
3586 | [self update]; | |
ec97ef06 JF |
3587 | } |
3588 | ||
670a0494 JF |
3589 | - (bool) upgrade { |
3590 | NSString *title(UCLocalize("UPGRADE")); | |
3591 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
3592 | return false; | |
3593 | return true; | |
c7c6384e JF |
3594 | } |
3595 | ||
36bb2ca2 JF |
3596 | - (void) update { |
3597 | [self updateWithStatus:status_]; | |
3598 | } | |
b4d89997 | 3599 | |
670a0494 | 3600 | - (void) updateWithStatus:(Status &)status { |
670a0494 JF |
3601 | NSString *title(UCLocalize("REFRESHING_DATA")); |
3602 | ||
36bb2ca2 | 3603 | pkgSourceList list; |
53ca7fdd JF |
3604 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3605 | return; | |
b4d89997 | 3606 | |
36bb2ca2 JF |
3607 | FileFd lock; |
3608 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
670a0494 JF |
3609 | if ([self popErrorWithTitle:title]) |
3610 | return; | |
18873623 | 3611 | |
aaae308d JF |
3612 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
3613 | ||
fc470c15 JF |
3614 | bool success(ListUpdate(status, list, PulseInterval_)); |
3615 | if (status.WasCancelled()) | |
3616 | _error->Discard(); | |
aa42c612 | 3617 | else { |
fc470c15 | 3618 | [self popErrorWithTitle:title forOperation:success]; |
aa42c612 JF |
3619 | [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"]; |
3620 | Changed_ = true; | |
3621 | } | |
36bb2ca2 | 3622 | |
aaae308d | 3623 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
b4d89997 JF |
3624 | } |
3625 | ||
6915b806 | 3626 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate { |
36bb2ca2 | 3627 | delegate_ = delegate; |
6915b806 JF |
3628 | } |
3629 | ||
3630 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate { | |
3631 | progress_ = delegate; | |
36bb2ca2 | 3632 | status_.setDelegate(delegate); |
36bb2ca2 | 3633 | } |
b4d89997 | 3634 | |
6915b806 JF |
3635 | - (NSObject<ProgressDelegate> *) progressDelegate { |
3636 | return progress_; | |
3637 | } | |
3638 | ||
7376b55c | 3639 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
34f70f5d JF |
3640 | SourceMap::const_iterator i(sourceMap_.find(file->ID)); |
3641 | return i == sourceMap_.end() ? nil : i->second; | |
b19871dd JF |
3642 | } |
3643 | ||
fe33a23e | 3644 | - (NSString *) mappedSectionForPointer:(const char *)section { |
4905df00 | 3645 | _H<NSString> *mapped; |
fe33a23e | 3646 | |
4905df00 JF |
3647 | _profile(Database$mappedSectionForPointer$Cache) |
3648 | mapped = §ions_[section]; | |
3649 | _end | |
3650 | ||
3651 | if (*mapped == NULL) { | |
fe33a23e JF |
3652 | size_t length(strlen(section)); |
3653 | char spaced[length + 1]; | |
3654 | ||
3655 | _profile(Database$mappedSectionForPointer$Replace) | |
3656 | for (size_t index(0); index != length; ++index) | |
3657 | spaced[index] = section[index] == '_' ? ' ' : section[index]; | |
3658 | spaced[length] = '\0'; | |
3659 | _end | |
3660 | ||
3661 | NSString *string; | |
3662 | ||
3663 | _profile(Database$mappedSectionForPointer$stringWithUTF8String) | |
3664 | string = [NSString stringWithUTF8String:spaced]; | |
3665 | _end | |
3666 | ||
3667 | _profile(Database$mappedSectionForPointer$Map) | |
4905df00 | 3668 | string = [SectionMap_ objectForKey:string] ?: string; |
fe33a23e | 3669 | _end |
4905df00 JF |
3670 | |
3671 | *mapped = string; | |
3672 | } return *mapped; | |
fe33a23e JF |
3673 | } |
3674 | ||
36bb2ca2 JF |
3675 | @end |
3676 | /* }}} */ | |
b4d89997 | 3677 | |
7b33d201 | 3678 | static _H<NSMutableSet> Diversions_; |
7256476b | 3679 | |
775deead JF |
3680 | @interface Diversion : NSObject { |
3681 | Pcre pattern_; | |
3682 | _H<NSString> key_; | |
3683 | _H<NSString> format_; | |
3684 | } | |
3685 | ||
3686 | @end | |
3687 | ||
3688 | @implementation Diversion | |
3689 | ||
3690 | - (id) initWithFrom:(NSString *)from to:(NSString *)to { | |
3691 | if ((self = [super init]) != nil) { | |
3692 | pattern_ = [from UTF8String]; | |
3693 | key_ = from; | |
3694 | format_ = to; | |
3695 | } return self; | |
3696 | } | |
3697 | ||
3698 | - (NSString *) divert:(NSString *)url { | |
8ea598c0 | 3699 | return !pattern_(url) ? nil : pattern_->*format_; |
775deead JF |
3700 | } |
3701 | ||
7256476b JF |
3702 | + (NSURL *) divertURL:(NSURL *)url { |
3703 | divert: | |
3704 | NSString *href([url absoluteString]); | |
3705 | ||
7b33d201 | 3706 | for (Diversion *diversion in (id) Diversions_) |
7256476b | 3707 | if (NSString *diverted = [diversion divert:href]) { |
85d5d452 JF |
3708 | #if !ForRelease |
3709 | NSLog(@"div: %@", diverted); | |
3710 | #endif | |
7256476b JF |
3711 | url = [NSURL URLWithString:diverted]; |
3712 | goto divert; | |
3713 | } | |
3714 | ||
3715 | return url; | |
3716 | } | |
3717 | ||
775deead JF |
3718 | - (NSString *) key { |
3719 | return key_; | |
3720 | } | |
3721 | ||
3722 | - (NSUInteger) hash { | |
3723 | return [key_ hash]; | |
3724 | } | |
3725 | ||
3726 | - (BOOL) isEqual:(Diversion *)object { | |
3727 | return self == object || [self class] == [object class] && [key_ isEqual:[object key]]; | |
3728 | } | |
3729 | ||
3730 | @end | |
3731 | ||
43f3d7f6 | 3732 | @interface CydiaObject : NSObject { |
7b33d201 | 3733 | _H<IndirectDelegate> indirect_; |
3931b718 | 3734 | _transient id delegate_; |
43f3d7f6 | 3735 | } |
c390d3ab | 3736 | |
43f3d7f6 | 3737 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
6840bff3 | 3738 | |
c390d3ab JF |
3739 | @end |
3740 | ||
09e89a8a | 3741 | @interface CydiaWebViewController : CyteWebViewController { |
7b33d201 | 3742 | _H<CydiaObject> cydia_; |
775deead JF |
3743 | } |
3744 | ||
3745 | + (void) addDiversion:(Diversion *)diversion; | |
3746 | ||
3747 | @end | |
3748 | ||
775deead | 3749 | /* Web Scripting {{{ */ |
43f3d7f6 | 3750 | @implementation CydiaObject |
c390d3ab | 3751 | |
43f3d7f6 JF |
3752 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
3753 | if ((self = [super init]) != nil) { | |
7b33d201 | 3754 | indirect_ = indirect; |
43f3d7f6 JF |
3755 | } return self; |
3756 | } | |
3757 | ||
77801ff1 JF |
3758 | - (void) setDelegate:(id)delegate { |
3759 | delegate_ = delegate; | |
3760 | } | |
3761 | ||
43f3d7f6 | 3762 | + (NSArray *) _attributeKeys { |
e58ff941 | 3763 | return [NSArray arrayWithObjects: |
6ffdaae3 | 3764 | @"bbsnum", |
e58ff941 | 3765 | @"device", |
56296da0 | 3766 | @"ecid", |
93d6d318 JF |
3767 | @"firmware", |
3768 | @"hostname", | |
3769 | @"idiom", | |
56296da0 JF |
3770 | @"model", |
3771 | @"plmn", | |
3772 | @"role", | |
e58ff941 | 3773 | @"serial", |
3ea82d91 | 3774 | @"token", |
bf1d5e69 | 3775 | @"version", |
e58ff941 | 3776 | nil]; |
43f3d7f6 JF |
3777 | } |
3778 | ||
3779 | - (NSArray *) attributeKeys { | |
3780 | return [[self class] _attributeKeys]; | |
c390d3ab JF |
3781 | } |
3782 | ||
43f3d7f6 JF |
3783 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
3784 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
c390d3ab | 3785 | } |
43f3d7f6 | 3786 | |
bf1d5e69 JF |
3787 | - (NSString *) version { |
3788 | return @ Cydia_; | |
3789 | } | |
3790 | ||
43f3d7f6 JF |
3791 | - (NSString *) device { |
3792 | return [[UIDevice currentDevice] uniqueIdentifier]; | |
c390d3ab JF |
3793 | } |
3794 | ||
93d6d318 JF |
3795 | - (NSString *) firmware { |
3796 | return [[UIDevice currentDevice] systemVersion]; | |
3797 | } | |
3798 | ||
3799 | - (NSString *) hostname { | |
3800 | return [[UIDevice currentDevice] name]; | |
3801 | } | |
3802 | ||
3803 | - (NSString *) idiom { | |
c138614d | 3804 | return (id) Idiom_ ?: [NSNull null]; |
93d6d318 JF |
3805 | } |
3806 | ||
56296da0 | 3807 | - (NSString *) plmn { |
849cd6bf | 3808 | return (id) PLMN_ ?: [NSNull null]; |
56296da0 JF |
3809 | } |
3810 | ||
6ffdaae3 JF |
3811 | - (NSString *) bbsnum { |
3812 | return (id) BBSNum_ ?: [NSNull null]; | |
3813 | } | |
3814 | ||
56296da0 | 3815 | - (NSString *) ecid { |
849cd6bf | 3816 | return (id) ChipID_ ?: [NSNull null]; |
affeffc7 JF |
3817 | } |
3818 | ||
43f3d7f6 | 3819 | - (NSString *) serial { |
56296da0 | 3820 | return SerialNumber_; |
43f3d7f6 | 3821 | } |
86316a91 | 3822 | |
56296da0 | 3823 | - (NSString *) role { |
849cd6bf | 3824 | return (id) Role_ ?: [NSNull null]; |
affeffc7 JF |
3825 | } |
3826 | ||
56296da0 JF |
3827 | - (NSString *) model { |
3828 | return [NSString stringWithUTF8String:Machine_]; | |
43f3d7f6 | 3829 | } |
43f3d7f6 | 3830 | |
3ea82d91 JF |
3831 | - (NSString *) token { |
3832 | return (id) Token_ ?: [NSNull null]; | |
3833 | } | |
3834 | ||
43f3d7f6 | 3835 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 3836 | if (false); |
cfc7b442 JF |
3837 | else if (selector == @selector(addBridgedHost:)) |
3838 | return @"addBridgedHost"; | |
3f428e4e JF |
3839 | else if (selector == @selector(addInternalRedirect::)) |
3840 | return @"addInternalRedirect"; | |
e4b48f2f | 3841 | else if (selector == @selector(addPipelinedHost:scheme:)) |
834c18a4 | 3842 | return @"addPipelinedHost"; |
b088c0cd JF |
3843 | else if (selector == @selector(addTrivialSource:)) |
3844 | return @"addTrivialSource"; | |
e58ff941 | 3845 | else if (selector == @selector(close)) |
43f3d7f6 | 3846 | return @"close"; |
e58ff941 JF |
3847 | else if (selector == @selector(du:)) |
3848 | return @"du"; | |
3849 | else if (selector == @selector(stringWithFormat:arguments:)) | |
3850 | return @"format"; | |
5c32f89e JF |
3851 | else if (selector == @selector(getAllSources)) |
3852 | return @"getAllSourcs"; | |
5bc1277a JF |
3853 | else if (selector == @selector(getKernelNumber:)) |
3854 | return @"getKernelNumber"; | |
3855 | else if (selector == @selector(getKernelString:)) | |
3856 | return @"getKernelString"; | |
8cc8eb1c JF |
3857 | else if (selector == @selector(getInstalledPackages)) |
3858 | return @"getInstalledPackages"; | |
c31d7cdc JF |
3859 | else if (selector == @selector(getIORegistryEntry::)) |
3860 | return @"getIORegistryEntry"; | |
948db680 JF |
3861 | else if (selector == @selector(getLocaleIdentifier)) |
3862 | return @"getLocaleIdentifier"; | |
3863 | else if (selector == @selector(getPreferredLanguages)) | |
3864 | return @"getPreferredLanguages"; | |
43f3d7f6 JF |
3865 | else if (selector == @selector(getPackageById:)) |
3866 | return @"getPackageById"; | |
ef974f52 JF |
3867 | else if (selector == @selector(getSessionValue:)) |
3868 | return @"getSessionValue"; | |
77801ff1 JF |
3869 | else if (selector == @selector(installPackages:)) |
3870 | return @"installPackages"; | |
e58ff941 JF |
3871 | else if (selector == @selector(localizedStringForKey:value:table:)) |
3872 | return @"localize"; | |
8d497e2a JF |
3873 | else if (selector == @selector(popViewController:)) |
3874 | return @"popViewController"; | |
b088c0cd JF |
3875 | else if (selector == @selector(refreshSources)) |
3876 | return @"refreshSources"; | |
ed5566c7 JF |
3877 | else if (selector == @selector(removeButton)) |
3878 | return @"removeButton"; | |
ef974f52 JF |
3879 | else if (selector == @selector(setSessionValue::)) |
3880 | return @"setSessionValue"; | |
8a126074 JF |
3881 | else if (selector == @selector(substitutePackageNames:)) |
3882 | return @"substitutePackageNames"; | |
8e3b68d4 JF |
3883 | else if (selector == @selector(scrollToBottom:)) |
3884 | return @"scrollToBottom"; | |
8366df5e JF |
3885 | else if (selector == @selector(setAllowsNavigationAction:)) |
3886 | return @"setAllowsNavigationAction"; | |
c31c825d JF |
3887 | else if (selector == @selector(setBadgeValue:)) |
3888 | return @"setBadgeValue"; | |
43f3d7f6 JF |
3889 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
3890 | return @"setButtonImage"; | |
3891 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
3892 | return @"setButtonTitle"; | |
b8a5d89d JF |
3893 | else if (selector == @selector(setHidesBackButton:)) |
3894 | return @"setHidesBackButton"; | |
5cdfcd6f JF |
3895 | else if (selector == @selector(setHidesNavigationBar:)) |
3896 | return @"setHidesNavigationBar"; | |
82406217 JF |
3897 | else if (selector == @selector(setNavigationBarStyle:)) |
3898 | return @"setNavigationBarStyle"; | |
00984204 JF |
3899 | else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:)) |
3900 | return @"setNavigationBarTintColor"; | |
36a20e14 JF |
3901 | else if (selector == @selector(setPasteboardString:)) |
3902 | return @"setPasteboardString"; | |
3903 | else if (selector == @selector(setPasteboardURL:)) | |
3904 | return @"setPasteboardURL"; | |
ef055c6c JF |
3905 | else if (selector == @selector(setToken:)) |
3906 | return @"setToken"; | |
43f3d7f6 JF |
3907 | else if (selector == @selector(setViewportWidth:)) |
3908 | return @"setViewportWidth"; | |
43f3d7f6 JF |
3909 | else if (selector == @selector(statfs:)) |
3910 | return @"statfs"; | |
e58ff941 JF |
3911 | else if (selector == @selector(supports:)) |
3912 | return @"supports"; | |
7c218781 JF |
3913 | else if (selector == @selector(unload)) |
3914 | return @"unload"; | |
c390d3ab | 3915 | else |
43f3d7f6 JF |
3916 | return nil; |
3917 | } | |
3918 | ||
3919 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
3920 | return [self webScriptNameForSelector:selector] == nil; | |
c390d3ab JF |
3921 | } |
3922 | ||
43f3d7f6 JF |
3923 | - (BOOL) supports:(NSString *)feature { |
3924 | return [feature isEqualToString:@"window.open"]; | |
3925 | } | |
c390d3ab | 3926 | |
7c218781 JF |
3927 | - (void) unload { |
3928 | [delegate_ performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; | |
3929 | } | |
3930 | ||
3f428e4e | 3931 | - (void) addInternalRedirect:(NSString *)from :(NSString *)to { |
a576488f | 3932 | [CydiaWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO]; |
775deead JF |
3933 | } |
3934 | ||
5bc1277a JF |
3935 | - (NSNumber *) getKernelNumber:(NSString *)name { |
3936 | const char *string([name UTF8String]); | |
3937 | ||
3938 | size_t size; | |
3939 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
3940 | return (id) [NSNull null]; | |
3941 | ||
3942 | if (size != sizeof(int)) | |
3943 | return (id) [NSNull null]; | |
3944 | ||
3945 | int value; | |
3946 | if (sysctlbyname(string, &value, &size, NULL, 0) == -1) | |
3947 | return (id) [NSNull null]; | |
3948 | ||
3949 | return [NSNumber numberWithInt:value]; | |
3950 | } | |
3951 | ||
3952 | - (NSString *) getKernelString:(NSString *)name { | |
3953 | const char *string([name UTF8String]); | |
3954 | ||
3955 | size_t size; | |
3956 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
3957 | return (id) [NSNull null]; | |
3958 | ||
3959 | char value[size + 1]; | |
3960 | if (sysctlbyname(string, value, &size, NULL, 0) == -1) | |
3961 | return (id) [NSNull null]; | |
3962 | ||
3963 | // XXX: just in case you request something ludicrous | |
3964 | value[size] = '\0'; | |
3965 | ||
3966 | return [NSString stringWithCString:value]; | |
3967 | } | |
3968 | ||
c31d7cdc JF |
3969 | - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry { |
3970 | NSObject *value(CYIOGetValue([path UTF8String], entry)); | |
3971 | ||
3972 | if (value != nil) | |
3973 | if ([value isKindOfClass:[NSData class]]) | |
3974 | value = CYHex((NSData *) value); | |
3975 | ||
3976 | return value; | |
3977 | } | |
3978 | ||
ef974f52 JF |
3979 | - (id) getSessionValue:(NSString *)key { |
3980 | @synchronized (SessionData_) { | |
3981 | return [SessionData_ objectForKey:key]; | |
3982 | } } | |
3983 | ||
3984 | - (void) setSessionValue:(NSString *)key :(NSString *)value { | |
3985 | @synchronized (SessionData_) { | |
3986 | if (value == (id) [WebUndefined undefined]) | |
3987 | [SessionData_ removeObjectForKey:key]; | |
3988 | else | |
3989 | [SessionData_ setObject:value forKey:key]; | |
3990 | } } | |
3991 | ||
cfc7b442 | 3992 | - (void) addBridgedHost:(NSString *)host { |
48f1762f JF |
3993 | @synchronized (HostConfig_) { |
3994 | [BridgedHosts_ addObject:host]; | |
3995 | } } | |
5df7ecfb | 3996 | |
e4b48f2f | 3997 | - (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme { |
48f1762f | 3998 | @synchronized (HostConfig_) { |
e4b48f2f JF |
3999 | if (scheme != (id) [WebUndefined undefined]) |
4000 | host = [NSString stringWithFormat:@"%@:%@", [scheme lowercaseString], host]; | |
4001 | ||
48f1762f JF |
4002 | [PipelinedHosts_ addObject:host]; |
4003 | } } | |
834c18a4 | 4004 | |
8d497e2a JF |
4005 | - (void) popViewController:(NSNumber *)value { |
4006 | if (value == (id) [WebUndefined undefined]) | |
4007 | value = [NSNumber numberWithBool:YES]; | |
4008 | [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO]; | |
4009 | } | |
4010 | ||
b088c0cd | 4011 | - (void) addTrivialSource:(NSString *)href { |
a1d85d42 | 4012 | [delegate_ performSelectorOnMainThread:@selector(addTrivialSource:) withObject:href waitUntilDone:NO]; |
b088c0cd JF |
4013 | } |
4014 | ||
4015 | - (void) refreshSources { | |
4016 | [delegate_ performSelectorOnMainThread:@selector(syncData) withObject:nil waitUntilDone:NO]; | |
4017 | } | |
4018 | ||
5c32f89e JF |
4019 | - (NSArray *) getAllSources { |
4020 | return [[Database sharedInstance] sources]; | |
4021 | } | |
4022 | ||
8cc8eb1c | 4023 | - (NSArray *) getInstalledPackages { |
26e6829b JF |
4024 | Database *database([Database sharedInstance]); |
4025 | @synchronized (database) { | |
4026 | NSArray *packages([database packages]); | |
f4db946e | 4027 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
77801ff1 | 4028 | for (Package *package in packages) |
26e6829b | 4029 | if (![package uninstalled]) |
8cc8eb1c JF |
4030 | [installed addObject:package]; |
4031 | return installed; | |
26e6829b | 4032 | } } |
8cc8eb1c | 4033 | |
43f3d7f6 | 4034 | - (Package *) getPackageById:(NSString *)id { |
62cab237 JF |
4035 | if (Package *package = [[Database sharedInstance] packageWithName:id]) { |
4036 | [package parse]; | |
4037 | return package; | |
4038 | } else | |
4039 | return (Package *) [NSNull null]; | |
43f3d7f6 JF |
4040 | } |
4041 | ||
948db680 JF |
4042 | - (NSString *) getLocaleIdentifier { |
4043 | return Locale_ == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(Locale_); | |
4044 | } | |
4045 | ||
4046 | - (NSArray *) getPreferredLanguages { | |
4047 | return Languages_; | |
4048 | } | |
4049 | ||
43f3d7f6 JF |
4050 | - (NSArray *) statfs:(NSString *)path { |
4051 | struct statfs stat; | |
4052 | ||
4053 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
4054 | return nil; | |
4055 | ||
4056 | return [NSArray arrayWithObjects: | |
4057 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
4058 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
4059 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
4060 | nil]; | |
4061 | } | |
4062 | ||
4063 | - (NSNumber *) du:(NSString *)path { | |
4064 | NSNumber *value(nil); | |
4065 | ||
4066 | int fds[2]; | |
4067 | _assert(pipe(fds) != -1); | |
4068 | ||
4069 | pid_t pid(ExecFork()); | |
4070 | if (pid == 0) { | |
4071 | _assert(dup2(fds[1], 1) != -1); | |
4072 | _assert(close(fds[0]) != -1); | |
4073 | _assert(close(fds[1]) != -1); | |
4074 | /* XXX: this should probably not use du */ | |
4075 | execl("/usr/libexec/cydia/du", "du", "-s", [path UTF8String], NULL); | |
4076 | exit(1); | |
4077 | _assert(false); | |
4078 | } | |
4079 | ||
4080 | _assert(close(fds[1]) != -1); | |
4081 | ||
4082 | if (FILE *du = fdopen(fds[0], "r")) { | |
4083 | char line[1024]; | |
4084 | while (fgets(line, sizeof(line), du) != NULL) { | |
4085 | size_t length(strlen(line)); | |
4086 | while (length != 0 && line[length - 1] == '\n') | |
4087 | line[--length] = '\0'; | |
4088 | if (char *tab = strchr(line, '\t')) { | |
4089 | *tab = '\0'; | |
4090 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
4091 | } | |
4092 | } | |
4093 | ||
4094 | fclose(du); | |
4095 | } else _assert(close(fds[0])); | |
4096 | ||
4097 | int status; | |
4098 | wait: | |
4099 | if (waitpid(pid, &status, 0) == -1) | |
4100 | if (errno == EINTR) | |
4101 | goto wait; | |
4102 | else _assert(false); | |
4103 | ||
4104 | return value; | |
4105 | } | |
4106 | ||
4107 | - (void) close { | |
e6417cea | 4108 | [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO]; |
43f3d7f6 JF |
4109 | } |
4110 | ||
77801ff1 JF |
4111 | - (void) installPackages:(NSArray *)packages { |
4112 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
4113 | } | |
4114 | ||
8a126074 JF |
4115 | - (NSString *) substitutePackageNames:(NSString *)message { |
4116 | NSMutableArray *words([[message componentsSeparatedByString:@" "] mutableCopy]); | |
4117 | for (size_t i(0), e([words count]); i != e; ++i) { | |
4118 | NSString *word([words objectAtIndex:i]); | |
4119 | if (Package *package = [[Database sharedInstance] packageWithName:word]) | |
4120 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4121 | } | |
4122 | ||
4123 | return [words componentsJoinedByString:@" "]; | |
4124 | } | |
4125 | ||
ed5566c7 JF |
4126 | - (void) removeButton { |
4127 | [indirect_ removeButton]; | |
4128 | } | |
4129 | ||
43f3d7f6 JF |
4130 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
4131 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
4132 | } | |
4133 | ||
4134 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
4135 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
4136 | } | |
4137 | ||
c31c825d JF |
4138 | - (void) setBadgeValue:(id)value { |
4139 | [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO]; | |
4140 | } | |
4141 | ||
8366df5e JF |
4142 | - (void) setAllowsNavigationAction:(NSString *)value { |
4143 | [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO]; | |
4144 | } | |
4145 | ||
b8a5d89d JF |
4146 | - (void) setHidesBackButton:(NSString *)value { |
4147 | [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO]; | |
4148 | } | |
4149 | ||
5cdfcd6f JF |
4150 | - (void) setHidesNavigationBar:(NSString *)value { |
4151 | [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO]; | |
4152 | } | |
4153 | ||
82406217 JF |
4154 | - (void) setNavigationBarStyle:(NSString *)value { |
4155 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO]; | |
4156 | } | |
4157 | ||
00984204 JF |
4158 | - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha { |
4159 | float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]); | |
4160 | UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]); | |
4161 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO]; | |
4162 | } | |
4163 | ||
36a20e14 JF |
4164 | - (void) setPasteboardString:(NSString *)value { |
4165 | [[objc_getClass("UIPasteboard") generalPasteboard] setString:value]; | |
4166 | } | |
4167 | ||
4168 | - (void) setPasteboardURL:(NSString *)value { | |
4169 | [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]]; | |
4170 | } | |
4171 | ||
673a6e1a | 4172 | - (void) _setToken:(NSString *)token { |
9737d93e JF |
4173 | Token_ = token; |
4174 | ||
4175 | if (token == nil) | |
4176 | [Metadata_ removeObjectForKey:@"Token"]; | |
4177 | else | |
4178 | [Metadata_ setObject:Token_ forKey:@"Token"]; | |
ef055c6c | 4179 | |
ef055c6c JF |
4180 | Changed_ = true; |
4181 | } | |
4182 | ||
673a6e1a JF |
4183 | - (void) setToken:(NSString *)token { |
4184 | [self performSelectorOnMainThread:@selector(_setToken:) withObject:token waitUntilDone:NO]; | |
4185 | } | |
4186 | ||
8e3b68d4 JF |
4187 | - (void) scrollToBottom:(NSNumber *)animated { |
4188 | [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO]; | |
4189 | } | |
4190 | ||
43f3d7f6 | 4191 | - (void) setViewportWidth:(float)width { |
8dbdaafa | 4192 | [indirect_ setViewportWidthOnMainThread:width]; |
43f3d7f6 JF |
4193 | } |
4194 | ||
4195 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
4196 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
4197 | unsigned count([arguments count]); | |
4198 | id values[count]; | |
4199 | for (unsigned i(0); i != count; ++i) | |
4200 | values[i] = [arguments objectAtIndex:i]; | |
673e8fa3 | 4201 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; |
43f3d7f6 JF |
4202 | } |
4203 | ||
4204 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
a95e0405 JF |
4205 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
4206 | value = nil; | |
43f3d7f6 JF |
4207 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
4208 | table = nil; | |
4209 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
c390d3ab JF |
4210 | } |
4211 | ||
4212 | @end | |
4213 | /* }}} */ | |
f79a4512 | 4214 | |
dd5f8161 | 4215 | |
80319240 | 4216 | /* Cydia Browser Controller {{{ */ |
a576488f | 4217 | @implementation CydiaWebViewController |
43f3d7f6 | 4218 | |
fe8e721f | 4219 | - (NSURL *) navigationURL { |
d323285e | 4220 | return request_ == nil ? nil : [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request_ URL] absoluteString]]]; |
fe8e721f GP |
4221 | } |
4222 | ||
775deead | 4223 | + (void) initialize { |
7b33d201 | 4224 | Diversions_ = [NSMutableSet setWithCapacity:0]; |
775deead JF |
4225 | } |
4226 | ||
4227 | + (void) addDiversion:(Diversion *)diversion { | |
4228 | [Diversions_ addObject:diversion]; | |
4229 | } | |
4230 | ||
2634b249 JF |
4231 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4232 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
01d93940 JF |
4233 | |
4234 | WebDataSource *source([frame dataSource]); | |
4235 | NSURLResponse *response([source response]); | |
4236 | NSURL *url([response URL]); | |
b8f1a18a JF |
4237 | NSString *scheme([[url scheme] lowercaseString]); |
4238 | ||
4239 | bool bridged(false); | |
8804004f | 4240 | |
48f1762f | 4241 | @synchronized (HostConfig_) { |
b8f1a18a JF |
4242 | if ([scheme isEqualToString:@"file"]) |
4243 | bridged = true; | |
4244 | else if ([scheme isEqualToString:@"https"]) | |
48f1762f | 4245 | if ([BridgedHosts_ containsObject:[url host]]) |
b8f1a18a | 4246 | bridged = true; |
48f1762f | 4247 | } |
b8f1a18a JF |
4248 | |
4249 | if (bridged) | |
4250 | [window setValue:cydia_ forKey:@"cydia"]; | |
43f3d7f6 JF |
4251 | } |
4252 | ||
22485d93 JF |
4253 | - (void) _setupMail:(MFMailComposeViewController *)controller { |
4254 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/cydia.log"] mimeType:@"text/plain" fileName:@"cydia.log"]; | |
4255 | ||
4256 | system("/usr/bin/dpkg -l >/tmp/dpkgl.log"); | |
4257 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/dpkgl.log"] mimeType:@"text/plain" fileName:@"dpkgl.log"]; | |
4258 | } | |
4259 | ||
f9b36dae JF |
4260 | - (NSURL *) URLWithURL:(NSURL *)url { |
4261 | return [Diversion divertURL:url]; | |
4262 | } | |
4263 | ||
9d1bf666 JF |
4264 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
4265 | NSMutableURLRequest *copy([[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source] mutableCopy]); | |
4266 | ||
bfc87a4d | 4267 | if (System_ != NULL) |
9d1bf666 | 4268 | [copy setValue:System_ forHTTPHeaderField:@"X-System"]; |
43f3d7f6 | 4269 | if (Machine_ != NULL) |
9d1bf666 | 4270 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
01d93940 | 4271 | if (Token_ != nil) |
9d1bf666 | 4272 | [copy setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"]; |
43f3d7f6 JF |
4273 | |
4274 | return copy; | |
a9a0661e JF |
4275 | } |
4276 | ||
77801ff1 JF |
4277 | - (void) setDelegate:(id)delegate { |
4278 | [super setDelegate:delegate]; | |
4279 | [cydia_ setDelegate:delegate]; | |
4280 | } | |
4281 | ||
b5e7eebb | 4282 | - (id) init { |
a576488f | 4283 | if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) { |
7b33d201 | 4284 | cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease]; |
43f3d7f6 | 4285 | |
2634b249 | 4286 | WebView *webview([[webview_ _documentView] webView]); |
43f3d7f6 | 4287 | |
44c1771c | 4288 | NSString *application([NSString stringWithFormat:@"Cydia/%@", @ Cydia_]); |
43f3d7f6 | 4289 | |
43f3d7f6 | 4290 | if (Safari_ != nil) |
9ef18597 | 4291 | application = [NSString stringWithFormat:@"Safari/%@ %@", Safari_, application]; |
35bf217f | 4292 | if (Build_ != nil) |
9ef18597 | 4293 | application = [NSString stringWithFormat:@"Mobile/%@ %@", Build_, application]; |
35bf217f | 4294 | if (Product_ != nil) |
9ef18597 | 4295 | application = [NSString stringWithFormat:@"Version/%@ %@", Product_, application]; |
43f3d7f6 JF |
4296 | |
4297 | [webview setApplicationNameForUserAgent:application]; | |
4298 | } return self; | |
4299 | } | |
4300 | ||
4301 | @end | |
80319240 | 4302 | /* }}} */ |
43f3d7f6 | 4303 | |
b1ca831d JF |
4304 | // CydiaScript {{{ |
4305 | @interface NSObject (CydiaScript) | |
4306 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; | |
4307 | @end | |
4308 | ||
4309 | @implementation NSObject (CydiaScript) | |
4310 | ||
4311 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
4312 | return self; | |
4313 | } | |
4314 | ||
4315 | @end | |
4316 | ||
4317 | @implementation NSArray (CydiaScript) | |
4318 | ||
4319 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
4320 | WebScriptObject *object([context evaluateWebScript:@"[]"]); | |
4321 | for (size_t i(0), e([self count]); i != e; ++i) | |
4322 | [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; | |
4323 | return object; | |
4324 | } | |
4325 | ||
4326 | @end | |
4327 | ||
4328 | @implementation NSDictionary (CydiaScript) | |
4329 | ||
4330 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
4331 | WebScriptObject *object([context evaluateWebScript:@"({})"]); | |
4332 | for (id i in self) | |
4333 | [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; | |
4334 | return object; | |
4335 | } | |
4336 | ||
4337 | @end | |
4338 | // }}} | |
4339 | ||
5829aea2 GP |
4340 | /* Confirmation Controller {{{ */ |
4341 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { | |
4342 | if (!iterator.end()) | |
4343 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
4344 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
4345 | continue; | |
4346 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
4347 | if (package.end()) | |
4348 | continue; | |
4349 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
4350 | return true; | |
4351 | } | |
4352 | ||
4353 | return false; | |
4354 | } | |
4355 | ||
adb61bda | 4356 | @protocol ConfirmationControllerDelegate |
674dce72 | 4357 | - (void) cancelAndClear:(bool)clear; |
b5e7eebb | 4358 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
dc63e78f | 4359 | - (void) queue; |
36bb2ca2 | 4360 | @end |
2367a917 | 4361 | |
a576488f | 4362 | @interface ConfirmationController : CydiaWebViewController { |
770f2a8e | 4363 | _transient Database *database_; |
6ceb0959 | 4364 | |
7b33d201 | 4365 | _H<UIAlertView> essential_; |
6ceb0959 | 4366 | |
7b33d201 JF |
4367 | _H<NSDictionary> changes_; |
4368 | _H<NSMutableArray> issues_; | |
4369 | _H<NSDictionary> sizes_; | |
6ceb0959 | 4370 | |
a9a0661e | 4371 | BOOL substrate_; |
36bb2ca2 | 4372 | } |
dc5812ec | 4373 | |
b5e7eebb | 4374 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 4375 | |
dc5812ec JF |
4376 | @end |
4377 | ||
adb61bda | 4378 | @implementation ConfirmationController |
dc5812ec | 4379 | |
ab2cfc1e JF |
4380 | - (void) complete { |
4381 | if (substrate_) | |
be860cc8 | 4382 | RestartSubstrate_ = true; |
ab2cfc1e JF |
4383 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
4384 | } | |
4385 | ||
b5e7eebb | 4386 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3272e699 | 4387 | NSString *context([alert context]); |
9bedffaa | 4388 | |
1cedb821 | 4389 | if ([context isEqualToString:@"remove"]) { |
ab2cfc1e | 4390 | if (button == [alert cancelButtonIndex]) |
b5e7eebb | 4391 | [self dismissModalViewControllerAnimated:YES]; |
ab2cfc1e JF |
4392 | else if (button == [alert firstOtherButtonIndex]) { |
4393 | [self complete]; | |
9bedffaa | 4394 | } |
9bedffaa | 4395 | |
3272e699 | 4396 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 4397 | } else if ([context isEqualToString:@"unable"]) { |
b5e7eebb | 4398 | [self dismissModalViewControllerAnimated:YES]; |
3272e699 GP |
4399 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
4400 | } else { | |
b5e7eebb GP |
4401 | [super alertView:alert clickedButtonAtIndex:button]; |
4402 | } | |
36bb2ca2 JF |
4403 | } |
4404 | ||
46c46f4f | 4405 | - (void) _doContinue { |
21ea11a4 GP |
4406 | [self dismissModalViewControllerAnimated:YES]; |
4407 | [delegate_ cancelAndClear:NO]; | |
46c46f4f | 4408 | } |
bc11cf5b | 4409 | |
46c46f4f JF |
4410 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
4411 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
21ea11a4 GP |
4412 | return nil; |
4413 | } | |
4414 | ||
2634b249 JF |
4415 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4416 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
6ceb0959 | 4417 | |
781001d7 | 4418 | [window setValue:[[NSDictionary dictionaryWithObjectsAndKeys: |
7b33d201 JF |
4419 | (id) changes_, @"changes", |
4420 | (id) issues_, @"issues", | |
4421 | (id) sizes_, @"sizes", | |
781001d7 JF |
4422 | self, @"queue", |
4423 | nil] Cydia$webScriptObjectInContext:window] forKey:@"cydiaConfirm"]; | |
36bb2ca2 JF |
4424 | } |
4425 | ||
b5e7eebb GP |
4426 | - (id) initWithDatabase:(Database *)database { |
4427 | if ((self = [super init]) != nil) { | |
770f2a8e JF |
4428 | database_ = database; |
4429 | ||
6ceb0959 JF |
4430 | NSMutableArray *installs([NSMutableArray arrayWithCapacity:16]); |
4431 | NSMutableArray *reinstalls([NSMutableArray arrayWithCapacity:16]); | |
4432 | NSMutableArray *upgrades([NSMutableArray arrayWithCapacity:16]); | |
4433 | NSMutableArray *downgrades([NSMutableArray arrayWithCapacity:16]); | |
4434 | NSMutableArray *removes([NSMutableArray arrayWithCapacity:16]); | |
dc5812ec | 4435 | |
36bb2ca2 | 4436 | bool remove(false); |
dc5812ec | 4437 | |
6ceb0959 JF |
4438 | pkgCacheFile &cache([database_ cache]); |
4439 | NSArray *packages([database_ packages]); | |
a9a0661e JF |
4440 | pkgDepCache::Policy *policy([database_ policy]); |
4441 | ||
7b33d201 | 4442 | issues_ = [NSMutableArray arrayWithCapacity:4]; |
6ceb0959 | 4443 | |
c4dcf2c2 | 4444 | for (Package *package in packages) { |
6ceb0959 JF |
4445 | pkgCache::PkgIterator iterator([package iterator]); |
4446 | NSString *name([package id]); | |
4447 | ||
4448 | if ([package broken]) { | |
4449 | NSMutableArray *reasons([NSMutableArray arrayWithCapacity:4]); | |
4450 | ||
4451 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
4452 | name, @"package", | |
4453 | reasons, @"reasons", | |
4454 | nil]]; | |
4455 | ||
4456 | pkgCache::VerIterator ver(cache[iterator].InstVerIter(cache)); | |
4457 | if (ver.end()) | |
4458 | continue; | |
4459 | ||
4460 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
4461 | pkgCache::DepIterator start; | |
4462 | pkgCache::DepIterator end; | |
4463 | dep.GlobOr(start, end); // ++dep | |
4464 | ||
4465 | if (!cache->IsImportantDep(end)) | |
4466 | continue; | |
4467 | if ((cache[end] & pkgDepCache::DepGInstall) != 0) | |
4468 | continue; | |
4469 | ||
810c9763 JF |
4470 | NSMutableArray *clauses([NSMutableArray arrayWithCapacity:4]); |
4471 | ||
4472 | [reasons addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
4473 | [NSString stringWithUTF8String:start.DepType()], @"relationship", | |
4474 | clauses, @"clauses", | |
4475 | nil]]; | |
4476 | ||
6ceb0959 JF |
4477 | _forever { |
4478 | NSString *reason, *installed((NSString *) [WebUndefined undefined]); | |
4479 | ||
4480 | pkgCache::PkgIterator target(start.TargetPkg()); | |
4481 | if (target->ProvidesList != 0) | |
4482 | reason = @"missing"; | |
4483 | else { | |
4484 | pkgCache::VerIterator ver(cache[target].InstVerIter(cache)); | |
4485 | if (!ver.end()) { | |
4486 | reason = @"installed"; | |
4487 | installed = [NSString stringWithUTF8String:ver.VerStr()]; | |
4488 | } else if (!cache[target].CandidateVerIter(cache).end()) | |
4489 | reason = @"uninstalled"; | |
4490 | else if (target->ProvidesList == 0) | |
4491 | reason = @"uninstallable"; | |
4492 | else | |
4493 | reason = @"virtual"; | |
4494 | } | |
4495 | ||
4496 | NSDictionary *version(start.TargetVer() == 0 ? [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: | |
4497 | [NSString stringWithUTF8String:start.CompType()], @"operator", | |
4498 | [NSString stringWithUTF8String:start.TargetVer()], @"value", | |
4499 | nil]); | |
4500 | ||
810c9763 | 4501 | [clauses addObject:[NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
4502 | [NSString stringWithUTF8String:start.TargetPkg().Name()], @"package", |
4503 | version, @"version", | |
4504 | reason, @"reason", | |
4505 | installed, @"installed", | |
4506 | nil]]; | |
4507 | ||
4508 | // yes, seriously. (wtf?) | |
4509 | if (start == end) | |
4510 | break; | |
4511 | ++start; | |
4512 | } | |
4513 | } | |
4514 | } | |
4515 | ||
36bb2ca2 | 4516 | pkgDepCache::StateCache &state(cache[iterator]); |
dc5812ec | 4517 | |
6ceb0959 | 4518 | static Pcre special_r("^(firmware$|gsc\\.|cy\\+)"); |
2388b078 | 4519 | |
36bb2ca2 | 4520 | if (state.NewInstall()) |
6ceb0959 | 4521 | [installs addObject:name]; |
f8d15be2 | 4522 | // XXX: else if (state.Install()) |
36bb2ca2 | 4523 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) |
6ceb0959 | 4524 | [reinstalls addObject:name]; |
f8d15be2 | 4525 | // XXX: move before previous if |
36bb2ca2 | 4526 | else if (state.Upgrade()) |
6ceb0959 | 4527 | [upgrades addObject:name]; |
36bb2ca2 | 4528 | else if (state.Downgrade()) |
6ceb0959 JF |
4529 | [downgrades addObject:name]; |
4530 | else if (!state.Delete()) | |
f8d15be2 | 4531 | // XXX: _assert(state.Keep()); |
6ceb0959 | 4532 | continue; |
1916f316 JF |
4533 | else if (special_r(name)) |
4534 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
4535 | [NSNull null], @"package", | |
4536 | [NSArray arrayWithObjects: | |
4537 | [NSDictionary dictionaryWithObjectsAndKeys: | |
810c9763 JF |
4538 | @"Conflicts", @"relationship", |
4539 | [NSArray arrayWithObjects: | |
4540 | [NSDictionary dictionaryWithObjectsAndKeys: | |
4541 | name, @"package", | |
4542 | [NSNull null], @"version", | |
4543 | @"installed", @"reason", | |
4544 | nil], | |
4545 | nil], @"clauses", | |
1916f316 JF |
4546 | nil], |
4547 | nil], @"reasons", | |
4548 | nil]]; | |
4549 | else { | |
2388b078 | 4550 | if ([package essential]) |
36bb2ca2 | 4551 | remove = true; |
6ceb0959 JF |
4552 | [removes addObject:name]; |
4553 | } | |
a9a0661e JF |
4554 | |
4555 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); | |
4556 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
36bb2ca2 | 4557 | } |
ec97ef06 | 4558 | |
36bb2ca2 JF |
4559 | if (!remove) |
4560 | essential_ = nil; | |
d791dce4 | 4561 | else if (Advanced_) { |
43f3d7f6 | 4562 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
f79a4512 | 4563 | |
7b33d201 | 4564 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 4565 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
b5e7eebb GP |
4566 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
4567 | delegate:self | |
4568 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
1aa29546 JF |
4569 | otherButtonTitles: |
4570 | [NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], | |
4571 | nil | |
7b33d201 | 4572 | ] autorelease]; |
04fe1349 | 4573 | |
3272e699 | 4574 | [essential_ setContext:@"remove"]; |
9bedffaa | 4575 | } else { |
7b33d201 | 4576 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 4577 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
b5e7eebb GP |
4578 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
4579 | delegate:self | |
4580 | cancelButtonTitle:UCLocalize("OKAY") | |
4581 | otherButtonTitles:nil | |
7b33d201 | 4582 | ] autorelease]; |
ec97ef06 | 4583 | |
b5e7eebb | 4584 | [essential_ setContext:@"unable"]; |
36bb2ca2 | 4585 | } |
ec97ef06 | 4586 | |
7b33d201 | 4587 | changes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
4588 | installs, @"installs", |
4589 | reinstalls, @"reinstalls", | |
4590 | upgrades, @"upgrades", | |
4591 | downgrades, @"downgrades", | |
4592 | removes, @"removes", | |
affeffc7 | 4593 | nil]; |
dc5812ec | 4594 | |
7b33d201 | 4595 | sizes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
4596 | [NSNumber numberWithInteger:[database_ fetcher].FetchNeeded()], @"downloading", |
4597 | [NSNumber numberWithInteger:[database_ fetcher].PartialPresent()], @"resuming", | |
affeffc7 | 4598 | nil]; |
dc5812ec | 4599 | |
90351d93 | 4600 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/confirm/", UI_]]]; |
36bb2ca2 | 4601 | } return self; |
b4d89997 | 4602 | } |
8da60fb7 | 4603 | |
e6124cb6 JF |
4604 | - (UIBarButtonItem *) leftButton { |
4605 | return [[[UIBarButtonItem alloc] | |
4606 | initWithTitle:UCLocalize("CANCEL") | |
4607 | style:UIBarButtonItemStylePlain | |
4608 | target:self | |
4609 | action:@selector(cancelButtonClicked) | |
4610 | ] autorelease]; | |
4611 | } | |
4612 | ||
614cd4f1 | 4613 | #if !AlwaysReload |
bcde1e70 | 4614 | - (void) applyRightButton { |
6ceb0959 | 4615 | if ([issues_ count] == 0 && ![self isLoading]) |
dd9de556 JF |
4616 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
4617 | initWithTitle:UCLocalize("CONFIRM") | |
2761d574 | 4618 | style:UIBarButtonItemStyleDone |
dd9de556 JF |
4619 | target:self |
4620 | action:@selector(confirmButtonClicked) | |
4621 | ] autorelease]]; | |
4622 | else | |
bcde1e70 | 4623 | [[self navigationItem] setRightBarButtonItem:nil]; |
affeffc7 | 4624 | } |
bcde1e70 | 4625 | #endif |
affeffc7 | 4626 | |
b5e7eebb | 4627 | - (void) cancelButtonClicked { |
21ea11a4 GP |
4628 | [self dismissModalViewControllerAnimated:YES]; |
4629 | [delegate_ cancelAndClear:YES]; | |
affeffc7 JF |
4630 | } |
4631 | ||
9487f027 | 4632 | #if !AlwaysReload |
b5e7eebb | 4633 | - (void) confirmButtonClicked { |
affeffc7 | 4634 | if (essential_ != nil) |
b5e7eebb | 4635 | [essential_ show]; |
ab2cfc1e JF |
4636 | else |
4637 | [self complete]; | |
affeffc7 | 4638 | } |
9487f027 | 4639 | #endif |
affeffc7 | 4640 | |
36bb2ca2 JF |
4641 | @end |
4642 | /* }}} */ | |
8da60fb7 | 4643 | |
aaae308d JF |
4644 | /* Progress Data {{{ */ |
4645 | @interface CydiaProgressData : NSObject { | |
4646 | _transient id delegate_; | |
4647 | ||
4648 | bool running_; | |
b0b11d99 | 4649 | float percent_; |
aaae308d | 4650 | |
bcbac8f7 JF |
4651 | float current_; |
4652 | float total_; | |
4653 | float speed_; | |
4654 | ||
aaae308d JF |
4655 | _H<NSMutableArray> events_; |
4656 | _H<NSString> title_; | |
4657 | ||
4658 | _H<NSString> status_; | |
4659 | _H<NSString> finish_; | |
4660 | } | |
4661 | ||
4662 | @end | |
4663 | ||
4664 | @implementation CydiaProgressData | |
4665 | ||
4666 | + (NSArray *) _attributeKeys { | |
4667 | return [NSArray arrayWithObjects: | |
bcbac8f7 | 4668 | @"current", |
aaae308d JF |
4669 | @"events", |
4670 | @"finish", | |
b0b11d99 | 4671 | @"percent", |
aaae308d | 4672 | @"running", |
bcbac8f7 | 4673 | @"speed", |
aaae308d | 4674 | @"title", |
bcbac8f7 | 4675 | @"total", |
aaae308d JF |
4676 | nil]; |
4677 | } | |
4678 | ||
4679 | - (NSArray *) attributeKeys { | |
4680 | return [[self class] _attributeKeys]; | |
4681 | } | |
4682 | ||
4683 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
4684 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
4685 | } | |
4686 | ||
4687 | - (id) init { | |
4688 | if ((self = [super init]) != nil) { | |
4689 | events_ = [NSMutableArray arrayWithCapacity:32]; | |
4690 | } return self; | |
4691 | } | |
4692 | ||
4693 | - (void) setDelegate:(id)delegate { | |
4694 | delegate_ = delegate; | |
4695 | } | |
4696 | ||
b0b11d99 JF |
4697 | - (void) setPercent:(float)value { |
4698 | percent_ = value; | |
aaae308d JF |
4699 | } |
4700 | ||
b0b11d99 JF |
4701 | - (NSNumber *) percent { |
4702 | return [NSNumber numberWithFloat:percent_]; | |
aaae308d JF |
4703 | } |
4704 | ||
bcbac8f7 JF |
4705 | - (void) setCurrent:(float)value { |
4706 | current_ = value; | |
4707 | } | |
4708 | ||
4709 | - (NSNumber *) current { | |
4710 | return [NSNumber numberWithFloat:current_]; | |
4711 | } | |
4712 | ||
4713 | - (void) setTotal:(float)value { | |
4714 | total_ = value; | |
4715 | } | |
4716 | ||
4717 | - (NSNumber *) total { | |
4718 | return [NSNumber numberWithFloat:total_]; | |
4719 | } | |
4720 | ||
4721 | - (void) setSpeed:(float)value { | |
4722 | speed_ = value; | |
4723 | } | |
4724 | ||
4725 | - (NSNumber *) speed { | |
4726 | return [NSNumber numberWithFloat:speed_]; | |
4727 | } | |
4728 | ||
aaae308d JF |
4729 | - (NSArray *) events { |
4730 | return events_; | |
4731 | } | |
4732 | ||
4733 | - (void) removeAllEvents { | |
4734 | [events_ removeAllObjects]; | |
4735 | } | |
4736 | ||
4737 | - (void) addEvent:(CydiaProgressEvent *)event { | |
4738 | [events_ addObject:event]; | |
4739 | } | |
4740 | ||
4741 | - (void) setTitle:(NSString *)text { | |
4742 | title_ = text; | |
4743 | } | |
4744 | ||
4745 | - (NSString *) title { | |
4746 | return title_; | |
4747 | } | |
4748 | ||
4749 | - (void) setFinish:(NSString *)text { | |
4750 | finish_ = text; | |
4751 | } | |
4752 | ||
4753 | - (NSString *) finish { | |
4754 | return (id) finish_ ?: [NSNull null]; | |
4755 | } | |
4756 | ||
4757 | - (void) setRunning:(bool)running { | |
4758 | running_ = running; | |
4759 | } | |
4760 | ||
4761 | - (NSNumber *) running { | |
4762 | return running_ ? (NSNumber *) kCFBooleanTrue : (NSNumber *) kCFBooleanFalse; | |
4763 | } | |
4764 | ||
4765 | @end | |
4766 | /* }}} */ | |
adb61bda | 4767 | /* Progress Controller {{{ */ |
a576488f | 4768 | @interface ProgressController : CydiaWebViewController < |
36bb2ca2 JF |
4769 | ProgressDelegate |
4770 | > { | |
8b29f8e6 | 4771 | _transient Database *database_; |
bf7c998c | 4772 | _H<CydiaProgressData, 1> progress_; |
aaae308d | 4773 | unsigned cancel_; |
2367a917 JF |
4774 | } |
4775 | ||
b5e7eebb | 4776 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
2367a917 | 4777 | |
6915b806 | 4778 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title; |
2367a917 | 4779 | |
6915b806 | 4780 | - (void) setTitle:(NSString *)title; |
aaae308d | 4781 | - (void) setCancellable:(bool)cancellable; |
bd150f54 | 4782 | |
36bb2ca2 JF |
4783 | @end |
4784 | ||
adb61bda | 4785 | @implementation ProgressController |
2367a917 JF |
4786 | |
4787 | - (void) dealloc { | |
6915b806 | 4788 | [database_ setProgressDelegate:nil]; |
2367a917 JF |
4789 | [super dealloc]; |
4790 | } | |
4791 | ||
63ae52be JF |
4792 | - (UIBarButtonItem *) leftButton { |
4793 | return cancel_ == 1 ? [[[UIBarButtonItem alloc] | |
3c62d654 JF |
4794 | initWithTitle:UCLocalize("CANCEL") |
4795 | style:UIBarButtonItemStylePlain | |
4796 | target:self | |
4797 | action:@selector(cancel) | |
63ae52be JF |
4798 | ] autorelease] : nil; |
4799 | } | |
4800 | ||
4801 | - (void) updateCancel { | |
4802 | [super applyLeftButton]; | |
3c62d654 JF |
4803 | } |
4804 | ||
b5e7eebb GP |
4805 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
4806 | if ((self = [super init]) != nil) { | |
8b29f8e6 | 4807 | database_ = database; |
36bb2ca2 | 4808 | delegate_ = delegate; |
ec97ef06 | 4809 | |
6915b806 JF |
4810 | [database_ setProgressDelegate:self]; |
4811 | ||
aaae308d JF |
4812 | progress_ = [[[CydiaProgressData alloc] init] autorelease]; |
4813 | [progress_ setDelegate:self]; | |
3c62d654 | 4814 | |
90351d93 | 4815 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/progress/", UI_]]]; |
3c62d654 JF |
4816 | |
4817 | [scroller_ setBackgroundColor:[UIColor blackColor]]; | |
4818 | ||
4819 | [[self navigationItem] setHidesBackButton:YES]; | |
4820 | ||
4821 | [self updateCancel]; | |
36bb2ca2 | 4822 | } return self; |
2367a917 JF |
4823 | } |
4824 | ||
aaae308d JF |
4825 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4826 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
4827 | [window setValue:progress_ forKey:@"cydiaProgress"]; | |
4828 | } | |
bc11cf5b | 4829 | |
aaae308d JF |
4830 | - (void) updateProgress { |
4831 | [self dispatchEvent:@"CydiaProgressUpdate"]; | |
4832 | } | |
b5e7eebb | 4833 | |
b5e7eebb | 4834 | - (void) viewWillAppear:(BOOL)animated { |
14e4ff09 | 4835 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
3c62d654 | 4836 | [super viewWillAppear:animated]; |
2367a917 JF |
4837 | } |
4838 | ||
aaae308d | 4839 | - (void) close { |
ef494bd8 RP |
4840 | UpdateExternalStatus(0); |
4841 | ||
670a0494 JF |
4842 | switch (Finish_) { |
4843 | case 0: | |
670a0494 JF |
4844 | break; |
4845 | ||
4846 | case 1: | |
c4899376 JF |
4847 | [delegate_ terminateWithSuccess]; |
4848 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
4849 | [delegate_ suspendWithAnimation:YES]; | |
4850 | else | |
4851 | [delegate_ suspend];*/ | |
670a0494 JF |
4852 | break; |
4853 | ||
4854 | case 2: | |
985d2dff | 4855 | _trace(); |
ef812071 | 4856 | goto reload; |
670a0494 JF |
4857 | |
4858 | case 3: | |
985d2dff | 4859 | _trace(); |
ef812071 JF |
4860 | goto reload; |
4861 | ||
4862 | reload: | |
4863 | system("/usr/bin/sbreload"); | |
985d2dff | 4864 | _trace(); |
670a0494 JF |
4865 | break; |
4866 | ||
4867 | case 4: | |
985d2dff | 4868 | _trace(); |
0e371502 JF |
4869 | if (void (*SBReboot)(mach_port_t) = reinterpret_cast<void (*)(mach_port_t)>(dlsym(RTLD_DEFAULT, "SBReboot"))) |
4870 | SBReboot(SBSSpringBoardServerPort()); | |
4871 | else | |
bb0fe3c9 | 4872 | reboot2(RB_AUTOBOOT); |
670a0494 | 4873 | break; |
bc8cd583 | 4874 | } |
aaae308d JF |
4875 | |
4876 | [super close]; | |
670a0494 | 4877 | } |
bd150f54 | 4878 | |
6915b806 | 4879 | - (void) setTitle:(NSString *)title { |
aaae308d JF |
4880 | [progress_ setTitle:title]; |
4881 | [self updateProgress]; | |
4882 | } | |
4883 | ||
4884 | - (UIBarButtonItem *) rightButton { | |
d53628b6 | 4885 | return [[progress_ running] boolValue] ? [super rightButton] : [[[UIBarButtonItem alloc] |
aaae308d JF |
4886 | initWithTitle:UCLocalize("CLOSE") |
4887 | style:UIBarButtonItemStylePlain | |
4888 | target:self | |
4889 | action:@selector(close) | |
4890 | ] autorelease]; | |
6915b806 JF |
4891 | } |
4892 | ||
4893 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title { | |
4894 | UpdateExternalStatus(1); | |
4895 | ||
aaae308d | 4896 | [progress_ setRunning:true]; |
6915b806 | 4897 | [self setTitle:title]; |
aaae308d | 4898 | // implicit updateProgress |
6915b806 | 4899 | |
140710ba | 4900 | SHA1SumValue notifyconf; { |
6915b806 JF |
4901 | FileFd file; |
4902 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4903 | _error->Discard(); | |
4904 | else { | |
4905 | MMap mmap(file, MMap::ReadOnly); | |
4906 | SHA1Summation sha1; | |
4907 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4908 | notifyconf = sha1.Result(); |
6915b806 JF |
4909 | } |
4910 | } | |
4911 | ||
140710ba | 4912 | SHA1SumValue springlist; { |
6915b806 JF |
4913 | FileFd file; |
4914 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4915 | _error->Discard(); | |
4916 | else { | |
4917 | MMap mmap(file, MMap::ReadOnly); | |
4918 | SHA1Summation sha1; | |
4919 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4920 | springlist = sha1.Result(); |
6915b806 JF |
4921 | } |
4922 | } | |
4923 | ||
4924 | if (invocation != nil) { | |
4925 | [invocation yieldToSelector:@selector(invoke)]; | |
aaae308d | 4926 | [self setTitle:@"COMPLETE"]; |
6915b806 | 4927 | } |
670a0494 | 4928 | |
22f8bed9 | 4929 | if (Finish_ < 4) { |
70d45c1e JF |
4930 | FileFd file; |
4931 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4932 | _error->Discard(); | |
4933 | else { | |
4934 | MMap mmap(file, MMap::ReadOnly); | |
4935 | SHA1Summation sha1; | |
4936 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4937 | if (!(notifyconf == sha1.Result())) |
70d45c1e JF |
4938 | Finish_ = 4; |
4939 | } | |
22f8bed9 JF |
4940 | } |
4941 | ||
affeffc7 | 4942 | if (Finish_ < 3) { |
70d45c1e JF |
4943 | FileFd file; |
4944 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4945 | _error->Discard(); | |
4946 | else { | |
4947 | MMap mmap(file, MMap::ReadOnly); | |
4948 | SHA1Summation sha1; | |
4949 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4950 | if (!(springlist == sha1.Result())) |
70d45c1e JF |
4951 | Finish_ = 3; |
4952 | } | |
dddbc481 JF |
4953 | } |
4954 | ||
be860cc8 JF |
4955 | if (Finish_ < 2) { |
4956 | if (RestartSubstrate_) | |
4957 | Finish_ = 2; | |
4958 | } | |
4959 | ||
4960 | RestartSubstrate_ = false; | |
4961 | ||
bc8cd583 | 4962 | switch (Finish_) { |
aaae308d JF |
4963 | case 0: [progress_ setFinish:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
4964 | case 1: [progress_ setFinish:UCLocalize("CLOSE_CYDIA")]; break; | |
4965 | case 2: [progress_ setFinish:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
4966 | case 3: [progress_ setFinish:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
4967 | case 4: [progress_ setFinish:UCLocalize("REBOOT_DEVICE")]; break; | |
bc8cd583 JF |
4968 | } |
4969 | ||
985d2dff | 4970 | _trace(); |
58241d4c | 4971 | system("su -c /usr/bin/uicache mobile"); |
985d2dff | 4972 | _trace(); |
c4ce98df | 4973 | |
eb403f34 | 4974 | UpdateExternalStatus(Finish_ == 0 ? 0 : 2); |
ef494bd8 | 4975 | |
aaae308d JF |
4976 | [progress_ setRunning:false]; |
4977 | [self updateProgress]; | |
4978 | ||
4979 | [self applyRightButton]; | |
31f3cfff JF |
4980 | } |
4981 | ||
6915b806 | 4982 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
aaae308d JF |
4983 | [progress_ addEvent:event]; |
4984 | [self updateProgress]; | |
baf80942 JF |
4985 | } |
4986 | ||
ff2d5dcd | 4987 | - (bool) isProgressCancelled { |
aaae308d JF |
4988 | return cancel_ == 2; |
4989 | } | |
4990 | ||
4991 | - (void) cancel { | |
4992 | cancel_ = 2; | |
4993 | [self updateCancel]; | |
4994 | } | |
4995 | ||
4996 | - (void) setCancellable:(bool)cancellable { | |
4997 | unsigned cancel(cancel_); | |
4998 | ||
4999 | if (!cancellable) | |
5000 | cancel_ = 0; | |
5001 | else if (cancel_ == 0) | |
5002 | cancel_ = 1; | |
5003 | ||
5004 | if (cancel != cancel_) | |
5005 | [self updateCancel]; | |
5006 | } | |
5007 | ||
5008 | - (void) setProgressCancellable:(NSNumber *)cancellable { | |
5009 | [self setCancellable:[cancellable boolValue]]; | |
baf80942 JF |
5010 | } |
5011 | ||
d885343d | 5012 | - (void) setProgressPercent:(NSNumber *)percent { |
b0b11d99 | 5013 | [progress_ setPercent:[percent floatValue]]; |
aaae308d | 5014 | [self updateProgress]; |
49048579 JF |
5015 | } |
5016 | ||
bcbac8f7 JF |
5017 | - (void) setProgressStatus:(NSDictionary *)status { |
5018 | if (status == nil) { | |
5019 | [progress_ setCurrent:0]; | |
5020 | [progress_ setTotal:0]; | |
5021 | [progress_ setSpeed:0]; | |
5022 | } else { | |
5023 | [progress_ setPercent:[[status objectForKey:@"Percent"] floatValue]]; | |
5024 | ||
5025 | [progress_ setCurrent:[[status objectForKey:@"Current"] floatValue]]; | |
5026 | [progress_ setTotal:[[status objectForKey:@"Total"] floatValue]]; | |
5027 | [progress_ setSpeed:[[status objectForKey:@"Speed"] floatValue]]; | |
5028 | } | |
5029 | ||
5030 | [self updateProgress]; | |
5031 | } | |
5032 | ||
36bb2ca2 JF |
5033 | @end |
5034 | /* }}} */ | |
dc088e63 | 5035 | |
46aa9775 | 5036 | /* Package Cell {{{ */ |
a9311516 | 5037 | @interface PackageCell : CyteTableViewCell < |
b97fcfc6 | 5038 | CyteTableViewCellDelegate |
c21004b9 | 5039 | > { |
7b33d201 JF |
5040 | _H<UIImage> icon_; |
5041 | _H<NSString> name_; | |
5042 | _H<NSString> description_; | |
3bd1c2a2 | 5043 | bool commercial_; |
7b33d201 JF |
5044 | _H<NSString> source_; |
5045 | _H<UIImage> badge_; | |
5046 | _H<Package> package_; | |
5047 | _H<UIImage> placard_; | |
59f3d290 | 5048 | bool summarized_; |
36bb2ca2 | 5049 | } |
723a0072 | 5050 | |
36bb2ca2 | 5051 | - (PackageCell *) init; |
59f3d290 | 5052 | - (void) setPackage:(Package *)package asSummary:(bool)summary; |
ec97ef06 | 5053 | |
327624b6 JF |
5054 | - (void) drawContentRect:(CGRect)rect; |
5055 | ||
5056 | @end | |
5057 | ||
36bb2ca2 JF |
5058 | @implementation PackageCell |
5059 | ||
36bb2ca2 | 5060 | - (PackageCell *) init { |
327624b6 JF |
5061 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
5062 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
5063 | UIView *content([self contentView]); | |
5064 | CGRect bounds([content bounds]); | |
04fe1349 | 5065 | |
b97fcfc6 | 5066 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
04fe1349 JF |
5067 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5068 | [content addSubview:content_]; | |
5069 | ||
327624b6 | 5070 | [content_ setDelegate:self]; |
327624b6 | 5071 | [content_ setOpaque:YES]; |
36bb2ca2 | 5072 | } return self; |
b4d89997 | 5073 | } |
b19871dd | 5074 | |
003fc610 | 5075 | - (NSString *) accessibilityLabel { |
7b33d201 | 5076 | return [NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), (id) name_, (id) description_]; |
003fc610 GP |
5077 | } |
5078 | ||
59f3d290 JF |
5079 | - (void) setPackage:(Package *)package asSummary:(bool)summary { |
5080 | summarized_ = summary; | |
5081 | ||
7b33d201 JF |
5082 | icon_ = nil; |
5083 | name_ = nil; | |
5084 | description_ = nil; | |
5085 | source_ = nil; | |
5086 | badge_ = nil; | |
5087 | placard_ = nil; | |
5088 | package_ = nil; | |
5089 | ||
68d927e2 | 5090 | [package parse]; |
31f3cfff | 5091 | |
36bb2ca2 | 5092 | Source *source = [package source]; |
b19871dd | 5093 | |
7b33d201 | 5094 | icon_ = [package icon]; |
5aebc4ff | 5095 | name_ = [NSString stringWithString:[package name]]; |
ef055c6c | 5096 | |
5aebc4ff JF |
5097 | NSString *description(nil); |
5098 | if (description == nil && IsWildcat_) | |
5099 | description = [package longDescription]; | |
5100 | if (description == nil) | |
5101 | description = [package shortDescription]; | |
5102 | ||
5103 | description_ = [NSString stringWithString:description]; | |
ef055c6c | 5104 | |
3bd1c2a2 | 5105 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5106 | |
7b33d201 | 5107 | package_ = package; |
dc63e78f | 5108 | |
a54b1c10 JF |
5109 | NSString *label = nil; |
5110 | bool trusted = false; | |
b19871dd | 5111 | |
36bb2ca2 JF |
5112 | if (source != nil) { |
5113 | label = [source label]; | |
5114 | trusted = [source trusted]; | |
a54b1c10 | 5115 | } else if ([[package id] isEqualToString:@"firmware"]) |
43f3d7f6 | 5116 | label = UCLocalize("APPLE"); |
7b0ce2da | 5117 | else |
43f3d7f6 | 5118 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; |
b19871dd | 5119 | |
f79a4512 | 5120 | NSString *from(label); |
a54b1c10 | 5121 | |
f79a4512 JF |
5122 | NSString *section = [package simpleSection]; |
5123 | if (section != nil && ![section isEqualToString:label]) { | |
5124 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
43f3d7f6 | 5125 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; |
f79a4512 | 5126 | } |
a54b1c10 | 5127 | |
7b33d201 | 5128 | source_ = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
36bb2ca2 | 5129 | |
c390d3ab | 5130 | if (NSString *purpose = [package primaryPurpose]) |
7b33d201 | 5131 | badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]; |
c390d3ab | 5132 | |
d832908d JF |
5133 | UIColor *color; |
5134 | NSString *placard; | |
5135 | ||
5136 | if (NSString *mode = [package_ mode]) { | |
5137 | if ([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]) { | |
5138 | color = RemovingColor_; | |
5139 | //placard = @"removing"; | |
5140 | } else { | |
5141 | color = InstallingColor_; | |
5142 | //placard = @"installing"; | |
5143 | } | |
5144 | ||
5145 | // XXX: the removing/installing placards are not @2x | |
5146 | placard = nil; | |
5147 | } else { | |
5148 | color = [UIColor whiteColor]; | |
5149 | ||
5150 | if ([package installed] != nil) | |
5151 | placard = @"installed"; | |
5152 | else | |
5153 | placard = nil; | |
5154 | } | |
5155 | ||
5156 | [content_ setBackgroundColor:color]; | |
5157 | ||
5158 | if (placard != nil) | |
7b33d201 | 5159 | placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/%@.png", App_, placard]]; |
670a0494 | 5160 | |
d832908d | 5161 | [self setNeedsDisplay]; |
327624b6 | 5162 | [content_ setNeedsDisplay]; |
3bd1c2a2 JF |
5163 | } |
5164 | ||
59f3d290 | 5165 | - (void) drawSummaryContentRect:(CGRect)rect { |
555aaf71 | 5166 | bool highlighted(highlighted_); |
ef055c6c | 5167 | float width([self bounds].size.width); |
dc63e78f | 5168 | |
59f3d290 JF |
5169 | if (icon_ != nil) { |
5170 | CGRect rect; | |
5171 | rect.size = [(UIImage *) icon_ size]; | |
5172 | ||
5173 | rect.size.width /= 4; | |
5174 | rect.size.height /= 4; | |
5175 | ||
5176 | rect.origin.x = 14 - rect.size.width / 4; | |
5177 | rect.origin.y = 14 - rect.size.height / 4; | |
5178 | ||
5179 | [icon_ drawInRect:rect]; | |
5180 | } | |
5181 | ||
5182 | if (badge_ != nil) { | |
5183 | CGRect rect; | |
5184 | rect.size = [(UIImage *) badge_ size]; | |
5185 | ||
5186 | rect.size.width /= 4; | |
5187 | rect.size.height /= 4; | |
5188 | ||
5189 | rect.origin.x = 20 - rect.size.width / 4; | |
5190 | rect.origin.y = 20 - rect.size.height / 4; | |
5191 | ||
5192 | [badge_ drawInRect:rect]; | |
5193 | } | |
5194 | ||
5195 | if (highlighted) | |
5196 | UISetColor(White_); | |
5197 | ||
5198 | if (!highlighted) | |
5199 | UISetColor(commercial_ ? Purple_ : Black_); | |
5200 | [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; | |
5201 | ||
5202 | if (placard_ != nil) | |
5203 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; | |
5204 | } | |
5205 | ||
5206 | - (void) drawNormalContentRect:(CGRect)rect { | |
5207 | bool highlighted(highlighted_); | |
5208 | float width([self bounds].size.width); | |
b19871dd | 5209 | |
baf80942 JF |
5210 | if (icon_ != nil) { |
5211 | CGRect rect; | |
7b33d201 | 5212 | rect.size = [(UIImage *) icon_ size]; |
baf80942 JF |
5213 | |
5214 | rect.size.width /= 2; | |
5215 | rect.size.height /= 2; | |
5216 | ||
5217 | rect.origin.x = 25 - rect.size.width / 2; | |
5218 | rect.origin.y = 25 - rect.size.height / 2; | |
5219 | ||
5220 | [icon_ drawInRect:rect]; | |
5221 | } | |
b19871dd | 5222 | |
c390d3ab | 5223 | if (badge_ != nil) { |
f79c810d | 5224 | CGRect rect; |
7b33d201 | 5225 | rect.size = [(UIImage *) badge_ size]; |
f79c810d JF |
5226 | |
5227 | rect.size.width /= 2; | |
5228 | rect.size.height /= 2; | |
5229 | ||
5230 | rect.origin.x = 36 - rect.size.width / 2; | |
5231 | rect.origin.y = 36 - rect.size.height / 2; | |
c390d3ab | 5232 | |
f79c810d | 5233 | [badge_ drawInRect:rect]; |
c390d3ab JF |
5234 | } |
5235 | ||
555aaf71 | 5236 | if (highlighted) |
f641a0e5 | 5237 | UISetColor(White_); |
b19871dd | 5238 | |
555aaf71 | 5239 | if (!highlighted) |
3bd1c2a2 | 5240 | UISetColor(commercial_ ? Purple_ : Black_); |
54b844a0 DH |
5241 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
5242 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; | |
b19871dd | 5243 | |
555aaf71 | 5244 | if (!highlighted) |
3bd1c2a2 | 5245 | UISetColor(commercial_ ? Purplish_ : Gray_); |
54b844a0 | 5246 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:UILineBreakModeTailTruncation]; |
670a0494 JF |
5247 | |
5248 | if (placard_ != nil) | |
01d93940 | 5249 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
ec97ef06 JF |
5250 | } |
5251 | ||
59f3d290 JF |
5252 | - (void) drawContentRect:(CGRect)rect { |
5253 | if (summarized_) | |
5254 | [self drawSummaryContentRect:rect]; | |
5255 | else | |
5256 | [self drawNormalContentRect:rect]; | |
5257 | } | |
5258 | ||
8da60fb7 JF |
5259 | @end |
5260 | /* }}} */ | |
36bb2ca2 | 5261 | /* Section Cell {{{ */ |
a9311516 | 5262 | @interface SectionCell : CyteTableViewCell < |
b97fcfc6 | 5263 | CyteTableViewCellDelegate |
c21004b9 | 5264 | > { |
7b33d201 JF |
5265 | _H<NSString> basic_; |
5266 | _H<NSString> section_; | |
5267 | _H<NSString> name_; | |
5268 | _H<NSString> count_; | |
5269 | _H<UIImage> icon_; | |
5270 | _H<UISwitch> switch_; | |
6d9712c4 | 5271 | BOOL editing_; |
b4d89997 | 5272 | } |
b19871dd | 5273 | |
6d9712c4 | 5274 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
36bb2ca2 | 5275 | |
8da60fb7 JF |
5276 | @end |
5277 | ||
36bb2ca2 | 5278 | @implementation SectionCell |
8da60fb7 | 5279 | |
46aa9775 GP |
5280 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
5281 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
7b33d201 JF |
5282 | icon_ = [UIImage applicationImageNamed:@"folder.png"]; |
5283 | switch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)] autorelease]; | |
46aa9775 GP |
5284 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
5285 | ||
5286 | UIView *content([self contentView]); | |
5287 | CGRect bounds([content bounds]); | |
5288 | ||
b97fcfc6 | 5289 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
46aa9775 GP |
5290 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5291 | [content addSubview:content_]; | |
5292 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5293 | ||
5294 | [content_ setDelegate:self]; | |
b4d89997 | 5295 | } return self; |
b19871dd JF |
5296 | } |
5297 | ||
6d9712c4 | 5298 | - (void) onSwitch:(id)sender { |
a171abd4 | 5299 | NSMutableDictionary *metadata([Sections_ objectForKey:basic_]); |
6d9712c4 JF |
5300 | if (metadata == nil) { |
5301 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
0010fa77 | 5302 | [Sections_ setObject:metadata forKey:basic_]; |
6d9712c4 JF |
5303 | } |
5304 | ||
46aa9775 | 5305 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
a171abd4 | 5306 | Changed_ = true; |
6d9712c4 JF |
5307 | } |
5308 | ||
5309 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
5310 | if (editing != editing_) { | |
5311 | if (editing_) | |
5312 | [switch_ removeFromSuperview]; | |
5313 | else | |
5314 | [self addSubview:switch_]; | |
5315 | editing_ = editing; | |
5316 | } | |
5317 | ||
7b33d201 JF |
5318 | basic_ = nil; |
5319 | section_ = nil; | |
5320 | name_ = nil; | |
5321 | count_ = nil; | |
6d9712c4 | 5322 | |
36bb2ca2 | 5323 | if (section == nil) { |
7b33d201 | 5324 | name_ = UCLocalize("ALL_PACKAGES"); |
f641a0e5 | 5325 | count_ = nil; |
36bb2ca2 | 5326 | } else { |
e59669fd | 5327 | basic_ = [section name]; |
677b8415 | 5328 | section_ = [section localized]; |
0010fa77 | 5329 | |
7b33d201 JF |
5330 | name_ = section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : (NSString *) section_; |
5331 | count_ = [NSString stringWithFormat:@"%d", [section count]]; | |
6d9712c4 JF |
5332 | |
5333 | if (editing_) | |
46aa9775 | 5334 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
36bb2ca2 | 5335 | } |
46aa9775 | 5336 | |
c21004b9 | 5337 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
0abb648c JF |
5338 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
5339 | ||
46aa9775 | 5340 | [content_ setNeedsDisplay]; |
f641a0e5 JF |
5341 | } |
5342 | ||
77801ff1 JF |
5343 | - (void) setFrame:(CGRect)frame { |
5344 | [super setFrame:frame]; | |
46aa9775 | 5345 | |
77801ff1 JF |
5346 | CGRect rect([switch_ frame]); |
5347 | [switch_ setFrame:CGRectMake(frame.size.width - 102, 9, rect.size.width, rect.size.height)]; | |
5348 | } | |
5349 | ||
003fc610 GP |
5350 | - (NSString *) accessibilityLabel { |
5351 | return name_; | |
5352 | } | |
5353 | ||
46aa9775 | 5354 | - (void) drawContentRect:(CGRect)rect { |
8cfba088 | 5355 | bool highlighted(highlighted_ && !editing_); |
bc11cf5b | 5356 | |
f641a0e5 JF |
5357 | [icon_ drawInRect:CGRectMake(8, 7, 32, 32)]; |
5358 | ||
555aaf71 | 5359 | if (highlighted) |
f641a0e5 JF |
5360 | UISetColor(White_); |
5361 | ||
46aa9775 | 5362 | float width(rect.size.width); |
58241d4c | 5363 | if (editing_) |
46aa9775 | 5364 | width -= 87; |
58241d4c | 5365 | |
555aaf71 JF |
5366 | if (!highlighted) |
5367 | UISetColor(Black_); | |
54b844a0 | 5368 | [name_ drawAtPoint:CGPointMake(48, 9) forWidth:(width - 70) withFont:Font22Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
6d9712c4 | 5369 | |
f641a0e5 JF |
5370 | CGSize size = [count_ sizeWithFont:Font14_]; |
5371 | ||
5372 | UISetColor(White_); | |
5373 | if (count_ != nil) | |
c390d3ab | 5374 | [count_ drawAtPoint:CGPointMake(13 + (29 - size.width) / 2, 16) withFont:Font12Bold_]; |
b19871dd JF |
5375 | } |
5376 | ||
36bb2ca2 JF |
5377 | @end |
5378 | /* }}} */ | |
b19871dd | 5379 | |
ab398adf | 5380 | /* File Table {{{ */ |
cd79e8cf | 5381 | @interface FileTable : CyteViewController < |
c21004b9 JF |
5382 | UITableViewDataSource, |
5383 | UITableViewDelegate | |
5384 | > { | |
36bb2ca2 | 5385 | _transient Database *database_; |
7b33d201 JF |
5386 | _H<Package> package_; |
5387 | _H<NSString> name_; | |
5388 | _H<NSMutableArray> files_; | |
bf7c998c | 5389 | _H<UITableView, 2> list_; |
ab398adf | 5390 | } |
b19871dd | 5391 | |
b5e7eebb | 5392 | - (id) initWithDatabase:(Database *)database; |
ab398adf JF |
5393 | - (void) setPackage:(Package *)package; |
5394 | ||
5395 | @end | |
5396 | ||
5397 | @implementation FileTable | |
5398 | ||
eb30da80 | 5399 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
ab398adf JF |
5400 | return files_ == nil ? 0 : [files_ count]; |
5401 | } | |
5402 | ||
21ea11a4 GP |
5403 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
5404 | return 24.0f; | |
5405 | }*/ | |
ab398adf | 5406 | |
46aa9775 | 5407 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
b5e7eebb GP |
5408 | static NSString *reuseIdentifier = @"Cell"; |
5409 | ||
46aa9775 GP |
5410 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5411 | if (cell == nil) { | |
5412 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
5413 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
ab398adf | 5414 | } |
46aa9775 | 5415 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
c21004b9 | 5416 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
b5e7eebb | 5417 | |
46aa9775 | 5418 | return cell; |
ab398adf | 5419 | } |
b19871dd | 5420 | |
fe8e721f GP |
5421 | - (NSURL *) navigationURL { |
5422 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/files", [package_ id]]]; | |
5423 | } | |
5424 | ||
5425 | - (void) loadView { | |
5426 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
5427 | ||
7b33d201 | 5428 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds]] autorelease]; |
fe8e721f GP |
5429 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5430 | [list_ setRowHeight:24.0f]; | |
7b33d201 | 5431 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
5432 | [list_ setDelegate:self]; |
5433 | [[self view] addSubview:list_]; | |
5434 | } | |
5435 | ||
5436 | - (void) viewDidLoad { | |
7d887d0b JF |
5437 | [super viewDidLoad]; |
5438 | ||
fe8e721f GP |
5439 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
5440 | } | |
5441 | ||
5442 | - (void) releaseSubviews { | |
fe8e721f GP |
5443 | list_ = nil; |
5444 | } | |
5445 | ||
b5e7eebb GP |
5446 | - (id) initWithDatabase:(Database *)database { |
5447 | if ((self = [super init]) != nil) { | |
ab398adf | 5448 | database_ = database; |
b19871dd | 5449 | |
7b33d201 | 5450 | files_ = [NSMutableArray arrayWithCapacity:32]; |
ab398adf JF |
5451 | } return self; |
5452 | } | |
5453 | ||
5454 | - (void) setPackage:(Package *)package { | |
7b33d201 JF |
5455 | package_ = nil; |
5456 | name_ = nil; | |
ab398adf JF |
5457 | |
5458 | [files_ removeAllObjects]; | |
5459 | ||
5460 | if (package != nil) { | |
7b33d201 JF |
5461 | package_ = package; |
5462 | name_ = [package id]; | |
ab398adf | 5463 | |
affeffc7 JF |
5464 | if (NSArray *files = [package files]) |
5465 | [files_ addObjectsFromArray:files]; | |
ab398adf | 5466 | |
9ea8d159 JF |
5467 | if ([files_ count] != 0) { |
5468 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
5469 | [files_ removeObjectAtIndex:0]; | |
a54b1c10 | 5470 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2388b078 JF |
5471 | |
5472 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
5473 | [stack addObject:@"/"]; | |
5474 | ||
5475 | for (int i(0), e([files_ count]); i != e; ++i) { | |
5476 | NSString *file = [files_ objectAtIndex:i]; | |
5477 | while (![file hasPrefix:[stack lastObject]]) | |
5478 | [stack removeLastObject]; | |
5479 | NSString *directory = [stack lastObject]; | |
5480 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
5481 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
a54b1c10 | 5482 | ([stack count] - 2) * 3, "", |
2388b078 JF |
5483 | [file substringFromIndex:[directory length]] |
5484 | ]]; | |
5485 | } | |
ab398adf JF |
5486 | } |
5487 | } | |
5488 | ||
5489 | [list_ reloadData]; | |
5490 | } | |
5491 | ||
ab398adf | 5492 | - (void) reloadData { |
fe8e721f GP |
5493 | [super reloadData]; |
5494 | ||
ab398adf | 5495 | [self setPackage:[database_ packageWithName:name_]]; |
ab398adf | 5496 | } |
b4d89997 | 5497 | |
b4d89997 | 5498 | @end |
36bb2ca2 | 5499 | /* }}} */ |
adb61bda | 5500 | /* Package Controller {{{ */ |
a576488f | 5501 | @interface CYPackageController : CydiaWebViewController < |
9daa7f25 DH |
5502 | UIActionSheetDelegate |
5503 | > { | |
770f2a8e | 5504 | _transient Database *database_; |
5d79f7bf JF |
5505 | _H<Package> package_; |
5506 | _H<NSString> name_; | |
3bd1c2a2 | 5507 | bool commercial_; |
5d79f7bf JF |
5508 | _H<NSMutableArray> buttons_; |
5509 | _H<UIBarButtonItem> button_; | |
b31b87cc JF |
5510 | } |
5511 | ||
57e8b225 | 5512 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name; |
b4d89997 | 5513 | |
36bb2ca2 | 5514 | @end |
b4d89997 | 5515 | |
f6e13561 | 5516 | @implementation CYPackageController |
b4d89997 | 5517 | |
fe8e721f | 5518 | - (NSURL *) navigationURL { |
5d79f7bf | 5519 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@", (id) name_]]; |
fe8e721f GP |
5520 | } |
5521 | ||
f79a4512 | 5522 | /* XXX: this is not safe at all... localization of /fail/ */ |
5a09ae08 | 5523 | - (void) _clickButtonWithName:(NSString *)name { |
43f3d7f6 | 5524 | if ([name isEqualToString:UCLocalize("CLEAR")]) |
dc63e78f | 5525 | [delegate_ clearPackage:package_]; |
43f3d7f6 | 5526 | else if ([name isEqualToString:UCLocalize("INSTALL")]) |
5a09ae08 | 5527 | [delegate_ installPackage:package_]; |
43f3d7f6 | 5528 | else if ([name isEqualToString:UCLocalize("REINSTALL")]) |
5a09ae08 | 5529 | [delegate_ installPackage:package_]; |
43f3d7f6 | 5530 | else if ([name isEqualToString:UCLocalize("REMOVE")]) |
5a09ae08 | 5531 | [delegate_ removePackage:package_]; |
43f3d7f6 | 5532 | else if ([name isEqualToString:UCLocalize("UPGRADE")]) |
5a09ae08 JF |
5533 | [delegate_ installPackage:package_]; |
5534 | else _assert(false); | |
5535 | } | |
5536 | ||
674dce72 | 5537 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
1cedb821 | 5538 | NSString *context([sheet context]); |
5a09ae08 | 5539 | |
1cedb821 | 5540 | if ([context isEqualToString:@"modify"]) { |
674dce72 GP |
5541 | if (button != [sheet cancelButtonIndex]) { |
5542 | NSString *buttonName = [buttons_ objectAtIndex:button]; | |
5543 | [self _clickButtonWithName:buttonName]; | |
5544 | } | |
bc11cf5b | 5545 | |
674dce72 | 5546 | [sheet dismissWithClickedButtonIndex:-1 animated:YES]; |
674dce72 | 5547 | } |
b4d89997 | 5548 | } |
2367a917 | 5549 | |
3e9c9e85 | 5550 | - (bool) _allowJavaScriptPanel { |
3bd1c2a2 | 5551 | return commercial_; |
3e9c9e85 JF |
5552 | } |
5553 | ||
9487f027 | 5554 | #if !AlwaysReload |
9daa7f25 | 5555 | - (void) _customButtonClicked { |
670a0494 JF |
5556 | int count([buttons_ count]); |
5557 | if (count == 0) | |
5558 | return; | |
2367a917 | 5559 | |
5a09ae08 JF |
5560 | if (count == 1) |
5561 | [self _clickButtonWithName:[buttons_ objectAtIndex:0]]; | |
5562 | else { | |
674dce72 | 5563 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
5a09ae08 | 5564 | [buttons addObjectsFromArray:buttons_]; |
36bb2ca2 | 5565 | |
674dce72 | 5566 | UIActionSheet *sheet = [[[UIActionSheet alloc] |
9ea8d159 | 5567 | initWithTitle:nil |
36bb2ca2 | 5568 | delegate:self |
674dce72 GP |
5569 | cancelButtonTitle:nil |
5570 | destructiveButtonTitle:nil | |
5571 | otherButtonTitles:nil | |
5572 | ] autorelease]; | |
5573 | ||
5574 | for (NSString *button in buttons) [sheet addButtonWithTitle:button]; | |
5575 | if (!IsWildcat_) { | |
5576 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
5577 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
5578 | } | |
5579 | [sheet setContext:@"modify"]; | |
bc11cf5b | 5580 | |
b5e7eebb | 5581 | [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; |
36bb2ca2 | 5582 | } |
b4d89997 | 5583 | } |
12b59862 | 5584 | |
a5938ea5 DH |
5585 | // We don't want to allow non-commercial packages to do custom things to the install button, |
5586 | // so it must call customButtonClicked with a custom commercial_ == 1 fallthrough. | |
9daa7f25 | 5587 | - (void) customButtonClicked { |
a5938ea5 DH |
5588 | if (commercial_) |
5589 | [super customButtonClicked]; | |
5590 | else | |
9daa7f25 | 5591 | [self _customButtonClicked]; |
12b59862 | 5592 | } |
2e6c1426 GP |
5593 | |
5594 | - (void) reloadButtonClicked { | |
6a8591be GP |
5595 | // Don't reload a commerical package by tapping the loading button, |
5596 | // but if it's not an Install button, we should forward it on. | |
5597 | if (![package_ uninstalled]) | |
5598 | [self _customButtonClicked]; | |
2fad210a GP |
5599 | } |
5600 | ||
5601 | - (void) applyLoadingTitle { | |
5602 | // Don't show "Loading" as the title. Ever. | |
2e6c1426 | 5603 | } |
a5938ea5 DH |
5604 | |
5605 | - (UIBarButtonItem *) rightButton { | |
2634b249 | 5606 | return button_; |
a5938ea5 | 5607 | } |
9487f027 | 5608 | #endif |
2367a917 | 5609 | |
57e8b225 | 5610 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name { |
b5e7eebb | 5611 | if ((self = [super init]) != nil) { |
36bb2ca2 | 5612 | database_ = database; |
5d79f7bf JF |
5613 | buttons_ = [NSMutableArray arrayWithCapacity:4]; |
5614 | name_ = [NSString stringWithString:name]; | |
5615 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]]]; | |
36bb2ca2 | 5616 | } return self; |
dc5812ec JF |
5617 | } |
5618 | ||
57e8b225 | 5619 | - (void) reloadData { |
9dac415b JF |
5620 | [super reloadData]; |
5621 | ||
57e8b225 | 5622 | package_ = [database_ packageWithName:name_]; |
36bb2ca2 | 5623 | |
5a09ae08 JF |
5624 | [buttons_ removeAllObjects]; |
5625 | ||
57e8b225 | 5626 | if (package_ != nil) { |
5d79f7bf | 5627 | [(Package *) package_ parse]; |
68d927e2 | 5628 | |
57e8b225 | 5629 | commercial_ = [package_ isCommercial]; |
36bb2ca2 | 5630 | |
dc63e78f | 5631 | if ([package_ mode] != nil) |
43f3d7f6 | 5632 | [buttons_ addObject:UCLocalize("CLEAR")]; |
5a09ae08 | 5633 | if ([package_ source] == nil); |
31f3cfff | 5634 | else if ([package_ upgradableAndEssential:NO]) |
43f3d7f6 | 5635 | [buttons_ addObject:UCLocalize("UPGRADE")]; |
6a155117 | 5636 | else if ([package_ uninstalled]) |
43f3d7f6 | 5637 | [buttons_ addObject:UCLocalize("INSTALL")]; |
5a09ae08 | 5638 | else |
43f3d7f6 | 5639 | [buttons_ addObject:UCLocalize("REINSTALL")]; |
6a155117 | 5640 | if (![package_ uninstalled]) |
43f3d7f6 | 5641 | [buttons_ addObject:UCLocalize("REMOVE")]; |
2634b249 | 5642 | } |
f79a4512 | 5643 | |
2634b249 JF |
5644 | NSString *title; |
5645 | switch ([buttons_ count]) { | |
5646 | case 0: title = nil; break; | |
5647 | case 1: title = [buttons_ objectAtIndex:0]; break; | |
5648 | default: title = UCLocalize("MODIFY"); break; | |
36bb2ca2 | 5649 | } |
2634b249 | 5650 | |
5d79f7bf | 5651 | button_ = [[[UIBarButtonItem alloc] |
2634b249 JF |
5652 | initWithTitle:title |
5653 | style:UIBarButtonItemStylePlain | |
5654 | target:self | |
5655 | action:@selector(customButtonClicked) | |
5d79f7bf | 5656 | ] autorelease]; |
b5e7eebb | 5657 | } |
f79a4512 | 5658 | |
3bd1c2a2 JF |
5659 | - (bool) isLoading { |
5660 | return commercial_ ? [super isLoading] : false; | |
9fe5e5f8 JF |
5661 | } |
5662 | ||
b4d89997 JF |
5663 | @end |
5664 | /* }}} */ | |
5829aea2 | 5665 | |
f50860ee | 5666 | /* Package List Controller {{{ */ |
cd79e8cf | 5667 | @interface PackageListController : CyteViewController < |
c21004b9 JF |
5668 | UITableViewDataSource, |
5669 | UITableViewDelegate | |
5670 | > { | |
36bb2ca2 | 5671 | _transient Database *database_; |
0175295c | 5672 | unsigned era_; |
56bf1e78 | 5673 | _H<NSArray> packages_; |
7b33d201 | 5674 | _H<NSMutableArray> sections_; |
bf7c998c | 5675 | _H<UITableView, 2> list_; |
7b33d201 JF |
5676 | _H<NSMutableArray> index_; |
5677 | _H<NSMutableDictionary> indices_; | |
5678 | _H<NSString> title_; | |
56bf1e78 | 5679 | unsigned reloading_; |
dc5812ec JF |
5680 | } |
5681 | ||
f50860ee | 5682 | - (id) initWithDatabase:(Database *)database title:(NSString *)title; |
b4d89997 | 5683 | - (void) setDelegate:(id)delegate; |
a0be02eb | 5684 | - (void) resetCursor; |
59f3d290 | 5685 | - (void) clearData; |
b4d89997 | 5686 | |
b4d89997 JF |
5687 | @end |
5688 | ||
f50860ee | 5689 | @implementation PackageListController |
b4d89997 | 5690 | |
59f3d290 JF |
5691 | - (bool) isSummarized { |
5692 | return false; | |
5693 | } | |
5694 | ||
9c5737d5 JF |
5695 | - (bool) showsSections { |
5696 | return true; | |
5697 | } | |
5698 | ||
f50860ee GP |
5699 | - (void) deselectWithAnimation:(BOOL)animated { |
5700 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
5701 | } | |
5702 | ||
bfb45dcb GP |
5703 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve { |
5704 | CGRect base = [[self view] bounds]; | |
5705 | base.size.height -= bounds.size.height; | |
5706 | base.origin = [list_ frame].origin; | |
5707 | ||
5708 | [UIView beginAnimations:nil context:NULL]; | |
5709 | [UIView setAnimationBeginsFromCurrentState:YES]; | |
5710 | [UIView setAnimationCurve:curve]; | |
5711 | [UIView setAnimationDuration:duration]; | |
5712 | [list_ setFrame:base]; | |
5713 | [UIView commitAnimations]; | |
5714 | } | |
5715 | ||
5716 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration { | |
5717 | [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear]; | |
5718 | } | |
5719 | ||
5720 | - (void) resizeForKeyboardBounds:(CGRect)bounds { | |
5721 | [self resizeForKeyboardBounds:bounds duration:0]; | |
5722 | } | |
5723 | ||
655c7ded JF |
5724 | - (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification { |
5725 | if (&UIKeyboardAnimationCurveUserInfoKey == NULL) | |
5726 | *curve = UIViewAnimationCurveEaseInOut; | |
5727 | else | |
5728 | [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve]; | |
5729 | ||
5730 | if (&UIKeyboardAnimationDurationUserInfoKey == NULL) | |
5731 | *duration = 0.3; | |
5732 | else | |
5733 | [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration]; | |
5734 | } | |
5735 | ||
bfb45dcb GP |
5736 | - (void) keyboardWillShow:(NSNotification *)notification { |
5737 | CGRect bounds; | |
5738 | CGPoint center; | |
bfb45dcb GP |
5739 | [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds]; |
5740 | [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:¢er]; | |
655c7ded JF |
5741 | |
5742 | NSTimeInterval duration; | |
5743 | UIViewAnimationCurve curve; | |
5744 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; | |
bfb45dcb GP |
5745 | |
5746 | 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 |
5747 | UIViewController *base = self; |
5748 | while ([base parentViewController] != nil) | |
5749 | base = [base parentViewController]; | |
5750 | CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]]; | |
bfb45dcb GP |
5751 | CGRect intersection = CGRectIntersection(viewframe, kbframe); |
5752 | ||
2e35f65f JF |
5753 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
5754 | intersection.size.height += CYStatusBarHeight([self interfaceOrientation]); | |
5755 | ||
bfb45dcb GP |
5756 | [self resizeForKeyboardBounds:intersection duration:duration curve:curve]; |
5757 | } | |
5758 | ||
5759 | - (void) keyboardWillHide:(NSNotification *)notification { | |
5760 | NSTimeInterval duration; | |
5761 | UIViewAnimationCurve curve; | |
655c7ded | 5762 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; |
bfb45dcb GP |
5763 | |
5764 | [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve]; | |
5765 | } | |
5766 | ||
5767 | - (void) viewWillAppear:(BOOL)animated { | |
5768 | [super viewWillAppear:animated]; | |
5769 | ||
5770 | [self resizeForKeyboardBounds:CGRectZero]; | |
5771 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
5772 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
5773 | } | |
5774 | ||
5775 | - (void) viewWillDisappear:(BOOL)animated { | |
5776 | [super viewWillDisappear:animated]; | |
5777 | ||
5778 | [self resizeForKeyboardBounds:CGRectZero]; | |
5779 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
5780 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
5781 | } | |
5782 | ||
f50860ee GP |
5783 | - (void) viewDidAppear:(BOOL)animated { |
5784 | [super viewDidAppear:animated]; | |
5785 | [self deselectWithAnimation:animated]; | |
5786 | } | |
5787 | ||
5788 | - (void) didSelectPackage:(Package *)package { | |
57e8b225 | 5789 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id]] autorelease]); |
f50860ee GP |
5790 | [view setDelegate:delegate_]; |
5791 | [[self navigationController] pushViewController:view animated:YES]; | |
5792 | } | |
5793 | ||
d03d7492 | 5794 | #if TryIndexedCollation |
a891c345 GP |
5795 | + (BOOL) hasIndexedCollation { |
5796 | return NO; // XXX: objc_getClass("UILocalizedIndexedCollation") != nil; | |
5797 | } | |
d03d7492 | 5798 | #endif |
a891c345 | 5799 | |
327624b6 JF |
5800 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
5801 | NSInteger count([sections_ count]); | |
5802 | return count == 0 ? 1 : count; | |
b4d89997 | 5803 | } |
2367a917 | 5804 | |
327624b6 | 5805 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
a891c345 | 5806 | if ([sections_ count] == 0 || [[sections_ objectAtIndex:section] count] == 0) |
327624b6 | 5807 | return nil; |
b4d89997 JF |
5808 | return [[sections_ objectAtIndex:section] name]; |
5809 | } | |
dc5812ec | 5810 | |
327624b6 JF |
5811 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
5812 | if ([sections_ count] == 0) | |
5813 | return 0; | |
5814 | return [[sections_ objectAtIndex:section] count]; | |
b4d89997 | 5815 | } |
dc5812ec | 5816 | |
327624b6 | 5817 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
0175295c JF |
5818 | @synchronized (database_) { |
5819 | if ([database_ era] != era_) | |
5820 | return nil; | |
5821 | ||
327624b6 JF |
5822 | Section *section([sections_ objectAtIndex:[path section]]); |
5823 | NSInteger row([path row]); | |
5824 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
0175295c JF |
5825 | return [[package retain] autorelease]; |
5826 | } } | |
dc5812ec | 5827 | |
327624b6 | 5828 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
c21004b9 | 5829 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
327624b6 JF |
5830 | if (cell == nil) |
5831 | cell = [[[PackageCell alloc] init] autorelease]; | |
59f3d290 | 5832 | [cell setPackage:[self packageAtIndexPath:path] asSummary:[self isSummarized]]; |
327624b6 | 5833 | return cell; |
b4d89997 | 5834 | } |
dc5812ec | 5835 | |
f50860ee | 5836 | - (void) tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path { |
327624b6 | 5837 | Package *package([self packageAtIndexPath:path]); |
59c6ae22 | 5838 | package = [database_ packageWithName:[package id]]; |
f50860ee | 5839 | [self didSelectPackage:package]; |
327624b6 JF |
5840 | } |
5841 | ||
5842 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
9c5737d5 | 5843 | if ([self showsSections]) |
59f3d290 JF |
5844 | return nil; |
5845 | ||
1527b095 | 5846 | return index_; |
327624b6 JF |
5847 | } |
5848 | ||
a891c345 | 5849 | - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
d03d7492 | 5850 | #if TryIndexedCollation |
a891c345 GP |
5851 | if ([[self class] hasIndexedCollation]) { |
5852 | return [[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionForSectionIndexTitleAtIndex:index]; | |
5853 | } | |
d03d7492 | 5854 | #endif |
a891c345 | 5855 | |
327624b6 | 5856 | return index; |
dc5812ec JF |
5857 | } |
5858 | ||
59f3d290 JF |
5859 | - (void) updateHeight { |
5860 | [list_ setRowHeight:([self isSummarized] ? 38 : 73)]; | |
5861 | } | |
5862 | ||
f50860ee GP |
5863 | - (id) initWithDatabase:(Database *)database title:(NSString *)title { |
5864 | if ((self = [super init]) != nil) { | |
36bb2ca2 | 5865 | database_ = database; |
f50860ee GP |
5866 | title_ = [title copy]; |
5867 | [[self navigationItem] setTitle:title_]; | |
36bb2ca2 | 5868 | |
d03d7492 JF |
5869 | #if TryIndexedCollation |
5870 | if ([[self class] hasIndexedCollation]) | |
7b33d201 | 5871 | index_ = [[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionIndexTitles]; |
d03d7492 JF |
5872 | else |
5873 | #endif | |
7b33d201 | 5874 | index_ = [NSMutableArray arrayWithCapacity:32]; |
d03d7492 | 5875 | |
7b33d201 | 5876 | indices_ = [NSMutableDictionary dictionaryWithCapacity:32]; |
327624b6 | 5877 | |
56bf1e78 | 5878 | packages_ = [NSArray array]; |
7b33d201 | 5879 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
61cc4dbc JF |
5880 | } return self; |
5881 | } | |
dc5812ec | 5882 | |
61cc4dbc JF |
5883 | - (void) loadView { |
5884 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
04fe1349 | 5885 | |
61cc4dbc JF |
5886 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; |
5887 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
5888 | [[self view] addSubview:list_]; | |
1527b095 | 5889 | |
61cc4dbc JF |
5890 | // XXX: is 20 the most optimal number here? |
5891 | [list_ setSectionIndexMinimumDisplayRowCount:20]; | |
59f3d290 | 5892 | |
61cc4dbc JF |
5893 | [(UITableView *) list_ setDataSource:self]; |
5894 | [list_ setDelegate:self]; | |
5895 | ||
5896 | [self updateHeight]; | |
5897 | } | |
5898 | ||
5899 | - (void) releaseSubviews { | |
5900 | list_ = nil; | |
dc5812ec JF |
5901 | } |
5902 | ||
5903 | - (void) setDelegate:(id)delegate { | |
2367a917 | 5904 | delegate_ = delegate; |
b4d89997 JF |
5905 | } |
5906 | ||
3025d5b4 JF |
5907 | - (bool) shouldYield { |
5908 | return false; | |
5909 | } | |
b4d89997 | 5910 | |
56bf1e78 JF |
5911 | - (bool) shouldBlock { |
5912 | return false; | |
5913 | } | |
5914 | ||
9c5737d5 | 5915 | - (NSMutableArray *) _reloadPackages { |
695fdd5c | 5916 | @synchronized (database_) { |
c28bc6f1 JF |
5917 | era_ = [database_ era]; |
5918 | NSArray *packages([database_ packages]); | |
5919 | ||
9c5737d5 | 5920 | return [NSMutableArray arrayWithArray:packages]; |
695fdd5c | 5921 | } } |
3025d5b4 JF |
5922 | |
5923 | - (void) _reloadData { | |
56bf1e78 JF |
5924 | if (reloading_ != 0) { |
5925 | reloading_ = 2; | |
5926 | return; | |
5927 | } | |
5928 | ||
ae60e2c1 JF |
5929 | NSArray *packages; |
5930 | ||
3025d5b4 | 5931 | if ([self shouldYield]) { |
bec28dda JF |
5932 | do { |
5933 | UIProgressHUD *hud; | |
56bf1e78 | 5934 | |
bec28dda JF |
5935 | if (![self shouldBlock]) |
5936 | hud = nil; | |
5937 | else { | |
5938 | hud = [delegate_ addProgressHUD]; | |
5939 | [hud setText:UCLocalize("LOADING")]; | |
5940 | } | |
56bf1e78 | 5941 | |
56bf1e78 | 5942 | reloading_ = 1; |
ae60e2c1 | 5943 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
bec28dda JF |
5944 | |
5945 | if (hud != nil) | |
5946 | [delegate_ removeProgressHUD:hud]; | |
56bf1e78 JF |
5947 | } while (reloading_ == 2); |
5948 | ||
5949 | reloading_ = 0; | |
3025d5b4 | 5950 | } else { |
ae60e2c1 | 5951 | packages = [self _reloadPackages]; |
3025d5b4 | 5952 | } |
36bb2ca2 | 5953 | |
ae60e2c1 JF |
5954 | packages_ = packages; |
5955 | ||
327624b6 | 5956 | [indices_ removeAllObjects]; |
56bf1e78 | 5957 | [sections_ removeAllObjects]; |
327624b6 | 5958 | |
b4d89997 JF |
5959 | Section *section = nil; |
5960 | ||
d03d7492 | 5961 | #if TryIndexedCollation |
a891c345 GP |
5962 | if ([[self class] hasIndexedCollation]) { |
5963 | id collation = [objc_getClass("UILocalizedIndexedCollation") currentCollation]; | |
5964 | NSArray *titles = [collation sectionIndexTitles]; | |
5965 | int secidx = -1; | |
c4dcf2c2 | 5966 | |
a891c345 GP |
5967 | _profile(PackageTable$reloadData$Section) |
5968 | for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { | |
5969 | Package *package; | |
5970 | int index; | |
b4d89997 | 5971 | |
a891c345 GP |
5972 | _profile(PackageTable$reloadData$Section$Package) |
5973 | package = [packages_ objectAtIndex:offset]; | |
5974 | index = [collation sectionForObject:package collationStringSelector:@selector(name)]; | |
808c6eb6 | 5975 | _end |
b4d89997 | 5976 | |
a891c345 GP |
5977 | while (secidx < index) { |
5978 | secidx += 1; | |
327624b6 | 5979 | |
a891c345 GP |
5980 | _profile(PackageTable$reloadData$Section$Allocate) |
5981 | section = [[[Section alloc] initWithName:[titles objectAtIndex:secidx] row:offset localize:NO] autorelease]; | |
5982 | _end | |
5983 | ||
5984 | _profile(PackageTable$reloadData$Section$Add) | |
5985 | [sections_ addObject:section]; | |
5986 | _end | |
5987 | } | |
5988 | ||
5989 | [section addToCount]; | |
808c6eb6 | 5990 | } |
a891c345 | 5991 | _end |
d03d7492 JF |
5992 | } else |
5993 | #endif | |
5994 | { | |
a891c345 | 5995 | [index_ removeAllObjects]; |
808c6eb6 | 5996 | |
9c5737d5 JF |
5997 | bool sectioned([self showsSections]); |
5998 | if (!sectioned) { | |
59f3d290 JF |
5999 | section = [[[Section alloc] initWithName:nil localize:false] autorelease]; |
6000 | [sections_ addObject:section]; | |
6001 | } | |
6002 | ||
a891c345 GP |
6003 | _profile(PackageTable$reloadData$Section) |
6004 | for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { | |
6005 | Package *package; | |
6006 | unichar index; | |
6007 | ||
6008 | _profile(PackageTable$reloadData$Section$Package) | |
6009 | package = [packages_ objectAtIndex:offset]; | |
6010 | index = [package index]; | |
6011 | _end | |
6012 | ||
9c5737d5 | 6013 | if (sectioned && (section == nil || [section index] != index)) { |
a891c345 GP |
6014 | _profile(PackageTable$reloadData$Section$Allocate) |
6015 | section = [[[Section alloc] initWithIndex:index row:offset] autorelease]; | |
6016 | _end | |
6017 | ||
6018 | [index_ addObject:[section name]]; | |
6019 | //[indices_ setObject:[NSNumber numberForInt:[sections_ count]] forKey:index]; | |
6020 | ||
6021 | _profile(PackageTable$reloadData$Section$Add) | |
6022 | [sections_ addObject:section]; | |
6023 | _end | |
6024 | } | |
6025 | ||
6026 | [section addToCount]; | |
6027 | } | |
6028 | _end | |
6029 | } | |
b4d89997 | 6030 | |
59f3d290 JF |
6031 | [self updateHeight]; |
6032 | ||
c4dcf2c2 | 6033 | _profile(PackageTable$reloadData$List) |
59f3d290 | 6034 | [(UITableView *) list_ setDataSource:self]; |
c4dcf2c2 JF |
6035 | [list_ reloadData]; |
6036 | _end | |
b4d89997 JF |
6037 | } |
6038 | ||
3025d5b4 JF |
6039 | - (void) reloadData { |
6040 | [super reloadData]; | |
6041 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
6042 | } | |
6043 | ||
a0be02eb | 6044 | - (void) resetCursor { |
4c6a29cd | 6045 | [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; |
a0be02eb JF |
6046 | } |
6047 | ||
59f3d290 JF |
6048 | - (void) clearData { |
6049 | [self updateHeight]; | |
6050 | ||
6051 | [list_ setDataSource:nil]; | |
6052 | [list_ reloadData]; | |
6053 | ||
6054 | [self resetCursor]; | |
6055 | } | |
6056 | ||
a3328c28 JF |
6057 | @end |
6058 | /* }}} */ | |
f50860ee GP |
6059 | /* Filtered Package List Controller {{{ */ |
6060 | @interface FilteredPackageListController : PackageListController { | |
a3328c28 | 6061 | SEL filter_; |
76933519 | 6062 | IMP imp_; |
7b33d201 | 6063 | _H<NSObject> object_; |
a3328c28 JF |
6064 | } |
6065 | ||
6066 | - (void) setObject:(id)object; | |
01d93940 | 6067 | - (void) setObject:(id)object forFilter:(SEL)filter; |
a3328c28 | 6068 | |
3025d5b4 JF |
6069 | - (SEL) filter; |
6070 | - (void) setFilter:(SEL)filter; | |
6071 | ||
f50860ee | 6072 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object; |
a3328c28 JF |
6073 | |
6074 | @end | |
6075 | ||
f50860ee | 6076 | @implementation FilteredPackageListController |
a3328c28 | 6077 | |
3025d5b4 JF |
6078 | - (SEL) filter { |
6079 | return filter_; | |
6080 | } | |
6081 | ||
01d93940 | 6082 | - (void) setFilter:(SEL)filter { |
aa32d91b | 6083 | @synchronized (self) { |
01d93940 JF |
6084 | filter_ = filter; |
6085 | ||
6086 | /* XXX: this is an unsafe optimization of doomy hell */ | |
6087 | Method method(class_getInstanceMethod([Package class], filter)); | |
6088 | _assert(method != NULL); | |
6089 | imp_ = method_getImplementation(method); | |
6090 | _assert(imp_ != NULL); | |
aa32d91b | 6091 | } } |
01d93940 | 6092 | |
a3328c28 | 6093 | - (void) setObject:(id)object { |
aa32d91b | 6094 | @synchronized (self) { |
7b33d201 | 6095 | object_ = object; |
aa32d91b | 6096 | } } |
a3328c28 | 6097 | |
01d93940 | 6098 | - (void) setObject:(id)object forFilter:(SEL)filter { |
d84597fe | 6099 | @synchronized (self) { |
01d93940 JF |
6100 | [self setFilter:filter]; |
6101 | [self setObject:object]; | |
d84597fe | 6102 | } } |
01d93940 | 6103 | |
9c5737d5 | 6104 | - (NSMutableArray *) _reloadPackages { |
aa32d91b | 6105 | @synchronized (database_) { |
c28bc6f1 JF |
6106 | era_ = [database_ era]; |
6107 | NSArray *packages([database_ packages]); | |
6108 | ||
aa32d91b JF |
6109 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); |
6110 | ||
6111 | IMP imp; | |
6112 | SEL filter; | |
6113 | _H<NSObject> object; | |
6114 | ||
6115 | @synchronized (self) { | |
6116 | imp = imp_; | |
6117 | filter = filter_; | |
6118 | object = object_; | |
6119 | } | |
6120 | ||
6121 | _profile(PackageTable$reloadData$Filter) | |
6122 | for (Package *package in packages) | |
6123 | if ([package valid] && (*reinterpret_cast<bool (*)(id, SEL, id)>(imp))(package, filter, object)) | |
6124 | [filtered addObject:package]; | |
76933519 | 6125 | _end |
aa32d91b JF |
6126 | |
6127 | return filtered; | |
6128 | } } | |
a3328c28 | 6129 | |
f50860ee GP |
6130 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object { |
6131 | if ((self = [super initWithDatabase:database title:title]) != nil) { | |
01d93940 | 6132 | [self setFilter:filter]; |
f50860ee | 6133 | [self setObject:object]; |
a3328c28 JF |
6134 | } return self; |
6135 | } | |
6136 | ||
dc5812ec | 6137 | @end |
b5e7eebb GP |
6138 | /* }}} */ |
6139 | ||
5829aea2 | 6140 | /* Home Controller {{{ */ |
a576488f | 6141 | @interface HomeController : CydiaWebViewController { |
b4d89997 | 6142 | } |
6840bff3 | 6143 | |
7b0ce2da | 6144 | @end |
b4d89997 | 6145 | |
5829aea2 | 6146 | @implementation HomeController |
46aa9775 | 6147 | |
3c62d654 JF |
6148 | - (id) init { |
6149 | if ((self = [super init]) != nil) { | |
90351d93 | 6150 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/home/", UI_]]]; |
ed7bfd8c | 6151 | [self reloadData]; |
3c62d654 JF |
6152 | } return self; |
6153 | } | |
6154 | ||
fe8e721f GP |
6155 | - (NSURL *) navigationURL { |
6156 | return [NSURL URLWithString:@"cydia://home"]; | |
6157 | } | |
6158 | ||
5829aea2 GP |
6159 | - (void) aboutButtonClicked { |
6160 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); | |
b4d89997 | 6161 | |
5829aea2 GP |
6162 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
6163 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
6164 | [alert setCancelButtonIndex:0]; | |
46aa9775 | 6165 | |
5829aea2 | 6166 | [alert setMessage: |
d53628b6 | 6167 | @"Copyright \u00a9 2008-2011\n" |
7e865c1e JF |
6168 | "SaurikIT, LLC\n" |
6169 | "\n" | |
5829aea2 GP |
6170 | "Jay Freeman (saurik)\n" |
6171 | "saurik@saurik.com\n" | |
6172 | "http://www.saurik.com/" | |
6173 | ]; | |
46aa9775 | 6174 | |
5829aea2 | 6175 | [alert show]; |
a0376fc1 JF |
6176 | } |
6177 | ||
e6124cb6 JF |
6178 | - (UIBarButtonItem *) leftButton { |
6179 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6180 | initWithTitle:UCLocalize("ABOUT") |
6181 | style:UIBarButtonItemStylePlain | |
6182 | target:self | |
6183 | action:@selector(aboutButtonClicked) | |
e6124cb6 | 6184 | ] autorelease]; |
7b0ce2da JF |
6185 | } |
6186 | ||
6187 | @end | |
6188 | /* }}} */ | |
5829aea2 | 6189 | /* Manage Controller {{{ */ |
a576488f | 6190 | @interface ManageController : CydiaWebViewController { |
7b0ce2da JF |
6191 | } |
6192 | ||
5829aea2 | 6193 | - (void) queueStatusDidChange; |
6840bff3 | 6194 | |
7b0ce2da JF |
6195 | @end |
6196 | ||
5829aea2 | 6197 | @implementation ManageController |
7b0ce2da | 6198 | |
3c62d654 JF |
6199 | - (id) init { |
6200 | if ((self = [super init]) != nil) { | |
90351d93 | 6201 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/manage/", UI_]]]; |
3c62d654 | 6202 | } return self; |
fe8e721f | 6203 | } |
7b0ce2da | 6204 | |
3c62d654 JF |
6205 | - (NSURL *) navigationURL { |
6206 | return [NSURL URLWithString:@"cydia://manage"]; | |
35f0a3b5 | 6207 | } |
7b0ce2da | 6208 | |
e6124cb6 JF |
6209 | - (UIBarButtonItem *) leftButton { |
6210 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6211 | initWithTitle:UCLocalize("SETTINGS") |
6212 | style:UIBarButtonItemStylePlain | |
6213 | target:self | |
6214 | action:@selector(settingsButtonClicked) | |
e6124cb6 | 6215 | ] autorelease]; |
7b0ce2da JF |
6216 | } |
6217 | ||
5829aea2 GP |
6218 | - (void) settingsButtonClicked { |
6219 | [delegate_ showSettings]; | |
b5e7eebb GP |
6220 | } |
6221 | ||
5829aea2 GP |
6222 | - (void) queueButtonClicked { |
6223 | [delegate_ queue]; | |
7b0ce2da JF |
6224 | } |
6225 | ||
29756674 JF |
6226 | - (UIBarButtonItem *) customButton { |
6227 | return Queuing_ ? [[[UIBarButtonItem alloc] | |
6228 | initWithTitle:UCLocalize("QUEUE") | |
6229 | style:UIBarButtonItemStyleDone | |
6230 | target:self | |
6231 | action:@selector(queueButtonClicked) | |
6232 | ] autorelease] : [super customButton]; | |
7b0ce2da JF |
6233 | } |
6234 | ||
5829aea2 | 6235 | - (void) queueStatusDidChange { |
29756674 | 6236 | [self applyRightButton]; |
7b0ce2da JF |
6237 | } |
6238 | ||
5829aea2 | 6239 | - (bool) isLoading { |
29756674 | 6240 | return !Queuing_ && [super isLoading]; |
7b0ce2da JF |
6241 | } |
6242 | ||
5829aea2 GP |
6243 | @end |
6244 | /* }}} */ | |
7b0ce2da | 6245 | |
5829aea2 GP |
6246 | /* Refresh Bar {{{ */ |
6247 | @interface RefreshBar : UINavigationBar { | |
7b33d201 JF |
6248 | _H<UIProgressIndicator> indicator_; |
6249 | _H<UITextLabel> prompt_; | |
6250 | _H<UIProgressBar> progress_; | |
6251 | _H<UINavigationButton> cancel_; | |
5829aea2 | 6252 | } |
7b0ce2da | 6253 | |
5829aea2 | 6254 | @end |
7b0ce2da | 6255 | |
5829aea2 | 6256 | @implementation RefreshBar |
7b0ce2da | 6257 | |
5829aea2 GP |
6258 | - (void) positionViews { |
6259 | CGRect frame = [cancel_ frame]; | |
6260 | frame.size = [cancel_ sizeThatFits:frame.size]; | |
6261 | frame.origin.x = [self frame].size.width - frame.size.width - 5; | |
6262 | frame.origin.y = ([self frame].size.height - frame.size.height) / 2; | |
6263 | [cancel_ setFrame:frame]; | |
7b0ce2da | 6264 | |
5829aea2 GP |
6265 | CGSize prgsize = {75, 100}; |
6266 | CGRect prgrect = {{ | |
6267 | [self frame].size.width - prgsize.width - 10, | |
6268 | ([self frame].size.height - prgsize.height) / 2 | |
6269 | } , prgsize}; | |
6270 | [progress_ setFrame:prgrect]; | |
7b0ce2da | 6271 | |
5829aea2 GP |
6272 | CGSize indsize([UIProgressIndicator defaultSizeForStyle:[indicator_ activityIndicatorViewStyle]]); |
6273 | unsigned indoffset = ([self frame].size.height - indsize.height) / 2; | |
6274 | CGRect indrect = {{indoffset, indoffset}, indsize}; | |
6275 | [indicator_ setFrame:indrect]; | |
2fc76a2d | 6276 | |
5829aea2 GP |
6277 | CGSize prmsize = {215, indsize.height + 4}; |
6278 | CGRect prmrect = {{ | |
6279 | indoffset * 2 + indsize.width, | |
6280 | unsigned([self frame].size.height - prmsize.height) / 2 - 1 | |
6281 | }, prmsize}; | |
6282 | [prompt_ setFrame:prmrect]; | |
2fc76a2d JF |
6283 | } |
6284 | ||
6840bff3 | 6285 | - (void) setFrame:(CGRect)frame { |
5829aea2 | 6286 | [super setFrame:frame]; |
5829aea2 | 6287 | [self positionViews]; |
2fc76a2d JF |
6288 | } |
6289 | ||
5829aea2 | 6290 | - (id) initWithFrame:(CGRect)frame delegate:(id)delegate { |
6840bff3 | 6291 | if ((self = [super initWithFrame:frame]) != nil) { |
5829aea2 | 6292 | [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
7b0ce2da | 6293 | |
5829aea2 | 6294 | [self setBarStyle:UIBarStyleBlack]; |
7b0ce2da | 6295 | |
5829aea2 GP |
6296 | UIBarStyle barstyle([self _barStyle:NO]); |
6297 | bool ugly(barstyle == UIBarStyleDefault); | |
3e9c9e85 | 6298 | |
5829aea2 GP |
6299 | UIProgressIndicatorStyle style = ugly ? |
6300 | UIProgressIndicatorStyleMediumBrown : | |
6301 | UIProgressIndicatorStyleMediumWhite; | |
2fc76a2d | 6302 | |
7b33d201 JF |
6303 | indicator_ = [[[UIProgressIndicator alloc] initWithFrame:CGRectZero] autorelease]; |
6304 | [(UIProgressIndicator *) indicator_ setStyle:style]; | |
5829aea2 GP |
6305 | [indicator_ startAnimation]; |
6306 | [self addSubview:indicator_]; | |
7b0ce2da | 6307 | |
7b33d201 | 6308 | prompt_ = [[[UITextLabel alloc] initWithFrame:CGRectZero] autorelease]; |
5829aea2 GP |
6309 | [prompt_ setColor:[UIColor colorWithCGColor:(ugly ? Blueish_ : Off_)]]; |
6310 | [prompt_ setBackgroundColor:[UIColor clearColor]]; | |
6311 | [prompt_ setFont:[UIFont systemFontOfSize:15]]; | |
6312 | [self addSubview:prompt_]; | |
7b0ce2da | 6313 | |
7b33d201 | 6314 | progress_ = [[[UIProgressBar alloc] initWithFrame:CGRectZero] autorelease]; |
5829aea2 | 6315 | [progress_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin]; |
7b33d201 | 6316 | [(UIProgressBar *) progress_ setStyle:0]; |
5829aea2 | 6317 | [self addSubview:progress_]; |
7b0ce2da | 6318 | |
7b33d201 | 6319 | cancel_ = [[[UINavigationButton alloc] initWithTitle:UCLocalize("CANCEL") style:UINavigationButtonStyleHighlighted] autorelease]; |
5829aea2 GP |
6320 | [cancel_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
6321 | [cancel_ addTarget:delegate action:@selector(cancelPressed) forControlEvents:UIControlEventTouchUpInside]; | |
6322 | [cancel_ setBarStyle:barstyle]; | |
3e9c9e85 | 6323 | |
5829aea2 GP |
6324 | [self positionViews]; |
6325 | } return self; | |
6326 | } | |
3e9c9e85 | 6327 | |
aaae308d JF |
6328 | - (void) setCancellable:(bool)cancellable { |
6329 | if (cancellable) | |
6330 | [self addSubview:cancel_]; | |
6331 | else | |
6332 | [cancel_ removeFromSuperview]; | |
5829aea2 | 6333 | } |
7b0ce2da | 6334 | |
5829aea2 GP |
6335 | - (void) start { |
6336 | [prompt_ setText:UCLocalize("UPDATING_DATABASE")]; | |
6337 | [progress_ setProgress:0]; | |
7b0ce2da JF |
6338 | } |
6339 | ||
5829aea2 | 6340 | - (void) stop { |
aaae308d | 6341 | [self setCancellable:NO]; |
7b0ce2da JF |
6342 | } |
6343 | ||
5829aea2 GP |
6344 | - (void) setPrompt:(NSString *)prompt { |
6345 | [prompt_ setText:prompt]; | |
7b0ce2da JF |
6346 | } |
6347 | ||
5829aea2 GP |
6348 | - (void) setProgress:(float)progress { |
6349 | [progress_ setProgress:progress]; | |
7b0ce2da JF |
6350 | } |
6351 | ||
5829aea2 GP |
6352 | @end |
6353 | /* }}} */ | |
b5e7eebb | 6354 | |
28e596e4 | 6355 | /* Cydia Navigation Controller Interface {{{ */ |
15f0d613 | 6356 | @interface UINavigationController (Cydia) |
28e596e4 JF |
6357 | |
6358 | - (NSArray *) navigationURLCollection; | |
15f0d613 | 6359 | - (void) unloadData; |
28e596e4 JF |
6360 | |
6361 | @end | |
6362 | /* }}} */ | |
7b0ce2da | 6363 | |
5829aea2 GP |
6364 | /* Cydia Tab Bar Controller {{{ */ |
6365 | @interface CYTabBarController : UITabBarController < | |
9f99f3da | 6366 | UITabBarControllerDelegate, |
5829aea2 GP |
6367 | ProgressDelegate |
6368 | > { | |
6369 | _transient Database *database_; | |
bf7c998c | 6370 | _H<RefreshBar, 1> refreshbar_; |
7b0ce2da | 6371 | |
5829aea2 GP |
6372 | bool dropped_; |
6373 | bool updating_; | |
6374 | // XXX: ok, "updatedelegate_"?... | |
6375 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
f79a4512 | 6376 | |
7b33d201 | 6377 | _H<UIViewController> remembered_; |
9f99f3da | 6378 | _transient UIViewController *transient_; |
7b0ce2da JF |
6379 | } |
6380 | ||
35f0a3b5 | 6381 | - (NSArray *) navigationURLCollection; |
5829aea2 GP |
6382 | - (void) dropBar:(BOOL)animated; |
6383 | - (void) beginUpdate; | |
6384 | - (void) raiseBar:(BOOL)animated; | |
6385 | - (BOOL) updating; | |
6c0ba3d9 | 6386 | - (void) unloadData; |
1cedb821 | 6387 | |
5829aea2 | 6388 | @end |
2fc76a2d | 6389 | |
5829aea2 | 6390 | @implementation CYTabBarController |
3e9c9e85 | 6391 | |
9f99f3da GP |
6392 | - (void) setUnselectedViewController:(UIViewController *)transient { |
6393 | NSMutableArray *controllers = [[self viewControllers] mutableCopy]; | |
6394 | if (transient != nil) { | |
6395 | if (transient_ == nil) | |
7b33d201 | 6396 | remembered_ = [controllers objectAtIndex:0]; |
9f99f3da GP |
6397 | transient_ = transient; |
6398 | [transient_ setTabBarItem:[remembered_ tabBarItem]]; | |
6399 | [controllers replaceObjectAtIndex:0 withObject:transient_]; | |
6400 | [self setSelectedIndex:0]; | |
6401 | [self setViewControllers:controllers]; | |
6402 | [self concealTabBarSelection]; | |
6403 | } else if (remembered_ != nil) { | |
6404 | [remembered_ setTabBarItem:[transient_ tabBarItem]]; | |
6405 | transient_ = transient; | |
6406 | [controllers replaceObjectAtIndex:0 withObject:remembered_]; | |
9f99f3da GP |
6407 | remembered_ = nil; |
6408 | [self setViewControllers:controllers]; | |
6409 | [self revealTabBarSelection]; | |
6410 | } | |
6411 | } | |
6412 | ||
6413 | - (UIViewController *) unselectedViewController { | |
6414 | return transient_; | |
6415 | } | |
6416 | ||
6417 | - (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { | |
6418 | if ([self unselectedViewController]) | |
6419 | [self setUnselectedViewController:nil]; | |
6420 | } | |
6421 | ||
35f0a3b5 | 6422 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
6423 | NSMutableArray *items([NSMutableArray array]); |
6424 | ||
35f0a3b5 | 6425 | // XXX: Should this deal with transient view controllers? |
fe8e721f | 6426 | for (id navigation in [self viewControllers]) { |
35f0a3b5 | 6427 | NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)]; |
fe8e721f GP |
6428 | if (stack != nil) |
6429 | [items addObject:stack]; | |
2fc76a2d | 6430 | } |
c713af59 | 6431 | |
fe8e721f GP |
6432 | return items; |
6433 | } | |
6434 | ||
6c0ba3d9 | 6435 | - (void) unloadData { |
29640f4e JF |
6436 | [super unloadData]; |
6437 | ||
15f0d613 JF |
6438 | for (UINavigationController *controller in [self viewControllers]) |
6439 | [controller unloadData]; | |
6440 | ||
29640f4e JF |
6441 | if (UIViewController *selected = [self selectedViewController]) |
6442 | [selected reloadData]; | |
c713af59 | 6443 | |
29640f4e JF |
6444 | if (UIViewController *unselected = [self unselectedViewController]) { |
6445 | [unselected unloadData]; | |
15f0d613 | 6446 | [unselected reloadData]; |
29640f4e | 6447 | } |
7b0ce2da JF |
6448 | } |
6449 | ||
35f0a3b5 | 6450 | - (void) dealloc { |
35f0a3b5 GP |
6451 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
6452 | ||
6453 | [super dealloc]; | |
6454 | } | |
6455 | ||
b5e7eebb GP |
6456 | - (id) initWithDatabase:(Database *)database { |
6457 | if ((self = [super init]) != nil) { | |
7b0ce2da | 6458 | database_ = database; |
9f99f3da | 6459 | [self setDelegate:self]; |
04fe1349 | 6460 | |
5829aea2 GP |
6461 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6462 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; | |
7b0ce2da | 6463 | |
7b33d201 | 6464 | refreshbar_ = [[[RefreshBar alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, [UINavigationBar defaultSize].height) delegate:self] autorelease]; |
7b0ce2da JF |
6465 | } return self; |
6466 | } | |
6467 | ||
5829aea2 GP |
6468 | - (void) setUpdate:(NSDate *)date { |
6469 | [self beginUpdate]; | |
6470 | } | |
1cedb821 | 6471 | |
5829aea2 | 6472 | - (void) beginUpdate { |
7b33d201 | 6473 | [(RefreshBar *) refreshbar_ start]; |
5829aea2 | 6474 | [self dropBar:YES]; |
7b0ce2da | 6475 | |
5829aea2 GP |
6476 | [updatedelegate_ retainNetworkActivityIndicator]; |
6477 | updating_ = true; | |
7b0ce2da | 6478 | |
5829aea2 GP |
6479 | [NSThread |
6480 | detachNewThreadSelector:@selector(performUpdate) | |
6481 | toTarget:self | |
6482 | withObject:nil | |
6483 | ]; | |
7b0ce2da JF |
6484 | } |
6485 | ||
d13edf44 JF |
6486 | - (void) performUpdate { |
6487 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
6488 | ||
5829aea2 GP |
6489 | Status status; |
6490 | status.setDelegate(self); | |
6491 | [database_ updateWithStatus:status]; | |
6492 | ||
6493 | [self | |
6494 | performSelectorOnMainThread:@selector(completeUpdate) | |
6495 | withObject:nil | |
6496 | waitUntilDone:NO | |
6497 | ]; | |
d13edf44 JF |
6498 | |
6499 | [pool release]; | |
f6e13561 GP |
6500 | } |
6501 | ||
5829aea2 GP |
6502 | - (void) stopUpdateWithSelector:(SEL)selector { |
6503 | updating_ = false; | |
6504 | [updatedelegate_ releaseNetworkActivityIndicator]; | |
b5e7eebb | 6505 | |
5829aea2 GP |
6506 | [self raiseBar:YES]; |
6507 | [refreshbar_ stop]; | |
bc11cf5b | 6508 | |
5829aea2 | 6509 | [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; |
2bdd73bd GP |
6510 | } |
6511 | ||
5829aea2 GP |
6512 | - (void) completeUpdate { |
6513 | if (!updating_) | |
6514 | return; | |
6515 | [self stopUpdateWithSelector:@selector(reloadData)]; | |
7b0ce2da JF |
6516 | } |
6517 | ||
5829aea2 GP |
6518 | - (void) cancelUpdate { |
6519 | [self stopUpdateWithSelector:@selector(updateData)]; | |
7b0ce2da JF |
6520 | } |
6521 | ||
5829aea2 GP |
6522 | - (void) cancelPressed { |
6523 | [self cancelUpdate]; | |
6524 | } | |
7b0ce2da | 6525 | |
5829aea2 GP |
6526 | - (BOOL) updating { |
6527 | return updating_; | |
7b0ce2da JF |
6528 | } |
6529 | ||
6915b806 JF |
6530 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
6531 | [refreshbar_ setPrompt:[event compoundMessage]]; | |
5829aea2 | 6532 | } |
c21004b9 | 6533 | |
ff2d5dcd | 6534 | - (bool) isProgressCancelled { |
5829aea2 GP |
6535 | return !updating_; |
6536 | } | |
7b0ce2da | 6537 | |
aaae308d JF |
6538 | - (void) setProgressCancellable:(NSNumber *)cancellable { |
6539 | [refreshbar_ setCancellable:(updating_ && [cancellable boolValue])]; | |
6540 | } | |
6541 | ||
d885343d | 6542 | - (void) setProgressPercent:(NSNumber *)percent { |
5829aea2 | 6543 | [refreshbar_ setProgress:[percent floatValue]]; |
7b0ce2da JF |
6544 | } |
6545 | ||
bcbac8f7 JF |
6546 | - (void) setProgressStatus:(NSDictionary *)status { |
6547 | if (status != nil) | |
6548 | [self setProgressPercent:[status objectForKey:@"Percent"]]; | |
6549 | } | |
6550 | ||
5829aea2 GP |
6551 | - (void) setUpdateDelegate:(id)delegate { |
6552 | updatedelegate_ = delegate; | |
7b0ce2da JF |
6553 | } |
6554 | ||
5829aea2 GP |
6555 | - (UIView *) transitionView { |
6556 | if ([self respondsToSelector:@selector(_transitionView)]) | |
6557 | return [self _transitionView]; | |
6558 | else | |
6559 | return MSHookIvar<id>(self, "_viewControllerTransitionView"); | |
f641a0e5 JF |
6560 | } |
6561 | ||
5829aea2 GP |
6562 | - (void) dropBar:(BOOL)animated { |
6563 | if (dropped_) | |
6564 | return; | |
6565 | dropped_ = true; | |
7b0ce2da | 6566 | |
5829aea2 GP |
6567 | UIView *transition([self transitionView]); |
6568 | [[self view] addSubview:refreshbar_]; | |
f6e13561 | 6569 | |
5829aea2 | 6570 | CGRect barframe([refreshbar_ frame]); |
f6e13561 | 6571 | |
a23207d2 | 6572 | if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
57daa971 | 6573 | barframe.origin.y = CYStatusBarHeight([self interfaceOrientation]); |
5829aea2 GP |
6574 | else |
6575 | barframe.origin.y = 0; | |
f6e13561 | 6576 | |
5829aea2 | 6577 | [refreshbar_ setFrame:barframe]; |
f6e13561 | 6578 | |
5829aea2 GP |
6579 | if (animated) |
6580 | [UIView beginAnimations:nil context:NULL]; | |
f6e13561 | 6581 | |
5829aea2 GP |
6582 | CGRect viewframe = [transition frame]; |
6583 | viewframe.origin.y += barframe.size.height; | |
6584 | viewframe.size.height -= barframe.size.height; | |
6585 | [transition setFrame:viewframe]; | |
f6e13561 | 6586 | |
5829aea2 GP |
6587 | if (animated) |
6588 | [UIView commitAnimations]; | |
f6e13561 | 6589 | |
5829aea2 GP |
6590 | // Ensure bar has the proper width for our view, it might have changed |
6591 | barframe.size.width = viewframe.size.width; | |
6592 | [refreshbar_ setFrame:barframe]; | |
f6e13561 GP |
6593 | } |
6594 | ||
5829aea2 GP |
6595 | - (void) raiseBar:(BOOL)animated { |
6596 | if (!dropped_) | |
6597 | return; | |
6598 | dropped_ = false; | |
f6e13561 | 6599 | |
5829aea2 GP |
6600 | UIView *transition([self transitionView]); |
6601 | [refreshbar_ removeFromSuperview]; | |
7b0ce2da | 6602 | |
5829aea2 | 6603 | CGRect barframe([refreshbar_ frame]); |
7b0ce2da | 6604 | |
5829aea2 GP |
6605 | if (animated) |
6606 | [UIView beginAnimations:nil context:NULL]; | |
7b0ce2da | 6607 | |
5829aea2 GP |
6608 | CGRect viewframe = [transition frame]; |
6609 | viewframe.origin.y -= barframe.size.height; | |
6610 | viewframe.size.height += barframe.size.height; | |
6611 | [transition setFrame:viewframe]; | |
9daa0bdc | 6612 | |
5829aea2 GP |
6613 | if (animated) |
6614 | [UIView commitAnimations]; | |
bfc87a4d JF |
6615 | } |
6616 | ||
5829aea2 GP |
6617 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { |
6618 | bool dropped(dropped_); | |
7b0ce2da | 6619 | |
5829aea2 GP |
6620 | if (dropped) |
6621 | [self raiseBar:NO]; | |
7b0ce2da | 6622 | |
5829aea2 | 6623 | [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; |
7b0ce2da | 6624 | |
5829aea2 GP |
6625 | if (dropped) |
6626 | [self dropBar:NO]; | |
b5e7eebb GP |
6627 | } |
6628 | ||
5829aea2 GP |
6629 | - (void) statusBarFrameChanged:(NSNotification *)notification { |
6630 | if (dropped_) { | |
6631 | [self raiseBar:NO]; | |
6632 | [self dropBar:NO]; | |
6633 | } | |
b5e7eebb GP |
6634 | } |
6635 | ||
7b0ce2da | 6636 | @end |
98228790 | 6637 | /* }}} */ |
f6e13561 | 6638 | |
28e596e4 | 6639 | /* Cydia Navigation Controller Implementation {{{ */ |
15f0d613 | 6640 | @implementation UINavigationController (Cydia) |
bc11cf5b | 6641 | |
35f0a3b5 | 6642 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
6643 | NSMutableArray *stack([NSMutableArray array]); |
6644 | ||
cd79e8cf | 6645 | for (CyteViewController *controller in [self viewControllers]) { |
fe8e721f GP |
6646 | NSString *url = [[controller navigationURL] absoluteString]; |
6647 | if (url != nil) | |
6648 | [stack addObject:url]; | |
6649 | } | |
6650 | ||
6651 | return stack; | |
6652 | } | |
6653 | ||
15f0d613 JF |
6654 | - (void) reloadData { |
6655 | [super reloadData]; | |
6656 | ||
6657 | if (UIViewController *visible = [self visibleViewController]) | |
6658 | [visible reloadData]; | |
6659 | } | |
6c0ba3d9 | 6660 | |
15f0d613 | 6661 | - (void) unloadData { |
cd79e8cf | 6662 | for (CyteViewController *page in [self viewControllers]) |
15f0d613 | 6663 | [page unloadData]; |
6c0ba3d9 | 6664 | |
15f0d613 | 6665 | [super unloadData]; |
dc63e78f JF |
6666 | } |
6667 | ||
5829aea2 GP |
6668 | @end |
6669 | /* }}} */ | |
7b0ce2da | 6670 | |
5829aea2 GP |
6671 | /* Cydia:// Protocol {{{ */ |
6672 | @interface CydiaURLProtocol : NSURLProtocol { | |
9fe5e5f8 JF |
6673 | } |
6674 | ||
7b0ce2da JF |
6675 | @end |
6676 | ||
5829aea2 GP |
6677 | @implementation CydiaURLProtocol |
6678 | ||
6679 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
6680 | NSURL *url([request URL]); | |
6681 | if (url == nil) | |
6682 | return NO; | |
b0a2900d | 6683 | |
5829aea2 | 6684 | NSString *scheme([[url scheme] lowercaseString]); |
b0a2900d JF |
6685 | if (scheme != nil && [scheme isEqualToString:@"cydia"]) |
6686 | return YES; | |
6687 | if ([[url absoluteString] hasPrefix:@"about:cydia-"]) | |
6688 | return YES; | |
6689 | ||
6690 | return NO; | |
aa5e5990 JF |
6691 | } |
6692 | ||
5829aea2 GP |
6693 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { |
6694 | return request; | |
6695 | } | |
aa5e5990 | 6696 | |
5829aea2 GP |
6697 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
6698 | id<NSURLProtocolClient> client([self client]); | |
6699 | if (icon == nil) | |
6700 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
6701 | else { | |
6702 | NSData *data(UIImagePNGRepresentation(icon)); | |
01d93940 | 6703 | |
5829aea2 GP |
6704 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
6705 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
6706 | [client URLProtocol:self didLoadData:data]; | |
6707 | [client URLProtocolDidFinishLoading:self]; | |
6708 | } | |
3931b718 JF |
6709 | } |
6710 | ||
5829aea2 GP |
6711 | - (void) startLoading { |
6712 | id<NSURLProtocolClient> client([self client]); | |
6713 | NSURLRequest *request([self request]); | |
bc11cf5b | 6714 | |
5829aea2 GP |
6715 | NSURL *url([request URL]); |
6716 | NSString *href([url absoluteString]); | |
b0a2900d JF |
6717 | NSString *scheme([[url scheme] lowercaseString]); |
6718 | ||
6719 | NSString *path; | |
6720 | ||
6721 | if ([scheme isEqualToString:@"cydia"]) | |
6722 | path = [href substringFromIndex:8]; | |
6723 | else if ([scheme isEqualToString:@"about"]) | |
6724 | path = [href substringFromIndex:12]; | |
6725 | else _assert(false); | |
bc11cf5b | 6726 | |
5829aea2 | 6727 | NSRange slash([path rangeOfString:@"/"]); |
bc11cf5b | 6728 | |
5829aea2 GP |
6729 | NSString *command; |
6730 | if (slash.location == NSNotFound) { | |
6731 | command = path; | |
6732 | path = nil; | |
6733 | } else { | |
6734 | command = [path substringToIndex:slash.location]; | |
6735 | path = [path substringFromIndex:(slash.location + 1)]; | |
6736 | } | |
39cda3a8 | 6737 | |
5829aea2 | 6738 | Database *database([Database sharedInstance]); |
bc11cf5b | 6739 | |
5829aea2 GP |
6740 | if ([command isEqualToString:@"package-icon"]) { |
6741 | if (path == nil) | |
6742 | goto fail; | |
6743 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6744 | Package *package([database packageWithName:path]); | |
6745 | if (package == nil) | |
6746 | goto fail; | |
eff20a8d | 6747 | [package parse]; |
5829aea2 GP |
6748 | UIImage *icon([package icon]); |
6749 | [self _returnPNGWithImage:icon forRequest:request]; | |
6750 | } else if ([command isEqualToString:@"source-icon"]) { | |
6751 | if (path == nil) | |
6752 | goto fail; | |
6753 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6754 | NSString *source(Simplify(path)); | |
6755 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sources/%@.png", App_, source]]); | |
6756 | if (icon == nil) | |
6757 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
6758 | [self _returnPNGWithImage:icon forRequest:request]; | |
6759 | } else if ([command isEqualToString:@"uikit-image"]) { | |
6760 | if (path == nil) | |
6761 | goto fail; | |
6762 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6763 | UIImage *icon(_UIImageWithName(path)); | |
6764 | [self _returnPNGWithImage:icon forRequest:request]; | |
6765 | } else if ([command isEqualToString:@"section-icon"]) { | |
6766 | if (path == nil) | |
6767 | goto fail; | |
6768 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6769 | NSString *section(Simplify(path)); | |
34841cd7 | 6770 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [section stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]); |
5829aea2 GP |
6771 | if (icon == nil) |
6772 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
6773 | [self _returnPNGWithImage:icon forRequest:request]; | |
6774 | } else fail: { | |
6775 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
6776 | } | |
39cda3a8 GP |
6777 | } |
6778 | ||
5829aea2 GP |
6779 | - (void) stopLoading { |
6780 | } | |
807ae6d7 | 6781 | |
5829aea2 GP |
6782 | @end |
6783 | /* }}} */ | |
807ae6d7 | 6784 | |
5829aea2 | 6785 | /* Section Controller {{{ */ |
f50860ee | 6786 | @interface SectionController : FilteredPackageListController { |
123b380c | 6787 | _H<NSString> section_; |
5829aea2 | 6788 | } |
807ae6d7 | 6789 | |
5829aea2 | 6790 | - (id) initWithDatabase:(Database *)database section:(NSString *)section; |
807ae6d7 | 6791 | |
5829aea2 | 6792 | @end |
bc11cf5b | 6793 | |
5829aea2 | 6794 | @implementation SectionController |
bc11cf5b | 6795 | |
fe8e721f GP |
6796 | - (NSURL *) navigationURL { |
6797 | NSString *name = section_; | |
6798 | if (name == nil) | |
6799 | name = @"all"; | |
6800 | ||
6801 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sections/%@", name]]; | |
6802 | } | |
6803 | ||
5829aea2 GP |
6804 | - (id) initWithDatabase:(Database *)database section:(NSString *)name { |
6805 | NSString *title; | |
3707eb56 | 6806 | if (name == nil) |
5829aea2 | 6807 | title = UCLocalize("ALL_PACKAGES"); |
3707eb56 | 6808 | else if (![name isEqual:@""]) |
5829aea2 | 6809 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(name) value:nil table:@"Sections"]; |
3707eb56 | 6810 | else |
5829aea2 | 6811 | title = UCLocalize("NO_SECTION"); |
fe8e721f | 6812 | |
5829aea2 | 6813 | if ((self = [super initWithDatabase:database title:title filter:@selector(isVisibleInSection:) with:name]) != nil) { |
3707eb56 | 6814 | section_ = name; |
5829aea2 | 6815 | } return self; |
b5e7eebb GP |
6816 | } |
6817 | ||
b5e7eebb GP |
6818 | @end |
6819 | /* }}} */ | |
5829aea2 | 6820 | /* Sections Controller {{{ */ |
cd79e8cf | 6821 | @interface SectionsController : CyteViewController < |
5829aea2 GP |
6822 | UITableViewDataSource, |
6823 | UITableViewDelegate | |
7585ce66 | 6824 | > { |
3931b718 | 6825 | _transient Database *database_; |
7b33d201 JF |
6826 | _H<NSMutableArray> sections_; |
6827 | _H<NSMutableArray> filtered_; | |
bf7c998c | 6828 | _H<UITableView, 2> list_; |
b5e7eebb GP |
6829 | } |
6830 | ||
5829aea2 | 6831 | - (id) initWithDatabase:(Database *)database; |
5829aea2 | 6832 | - (void) editButtonClicked; |
7585ce66 | 6833 | |
bc11cf5b | 6834 | @end |
b5e7eebb | 6835 | |
5829aea2 | 6836 | @implementation SectionsController |
b5e7eebb | 6837 | |
fe8e721f GP |
6838 | - (NSURL *) navigationURL { |
6839 | return [NSURL URLWithString:@"cydia://sections"]; | |
6840 | } | |
6841 | ||
a784d0a4 | 6842 | - (void) updateNavigationItem { |
8e5b801a | 6843 | [[self navigationItem] setTitle:[self isEditing] ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
a784d0a4 GP |
6844 | if ([sections_ count] == 0) { |
6845 | [[self navigationItem] setRightBarButtonItem:nil]; | |
6846 | } else { | |
6847 | [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] | |
8e5b801a | 6848 | initWithBarButtonSystemItem:([self isEditing] ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit) |
a784d0a4 GP |
6849 | target:self |
6850 | action:@selector(editButtonClicked) | |
6851 | ] animated:([[self navigationItem] rightBarButtonItem] != nil)]; | |
6852 | } | |
6853 | } | |
6854 | ||
8e5b801a GP |
6855 | - (void) setEditing:(BOOL)editing animated:(BOOL)animated { |
6856 | [super setEditing:editing animated:animated]; | |
35f0a3b5 | 6857 | |
8e5b801a | 6858 | if (editing) |
5829aea2 GP |
6859 | [list_ reloadData]; |
6860 | else | |
6861 | [delegate_ updateData]; | |
7585ce66 | 6862 | |
a784d0a4 | 6863 | [self updateNavigationItem]; |
b5e7eebb GP |
6864 | } |
6865 | ||
5829aea2 GP |
6866 | - (void) viewDidAppear:(BOOL)animated { |
6867 | [super viewDidAppear:animated]; | |
6868 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
6869 | } |
6870 | ||
5829aea2 GP |
6871 | - (void) viewWillDisappear:(BOOL)animated { |
6872 | [super viewWillDisappear:animated]; | |
8e5b801a | 6873 | if ([self isEditing]) [self setEditing:NO]; |
7585ce66 JF |
6874 | } |
6875 | ||
5829aea2 | 6876 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
b3551da8 | 6877 | Section *section = nil; |
8e5b801a | 6878 | int index = [indexPath row]; |
b3551da8 GP |
6879 | if (![self isEditing]) { |
6880 | index -= 1; | |
6881 | if (index >= 0) | |
6882 | section = [filtered_ objectAtIndex:index]; | |
6883 | } else { | |
6884 | section = [sections_ objectAtIndex:index]; | |
6885 | } | |
5829aea2 GP |
6886 | return section; |
6887 | } | |
7585ce66 | 6888 | |
5829aea2 | 6889 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8e5b801a GP |
6890 | if ([self isEditing]) |
6891 | return [sections_ count]; | |
6892 | else | |
6893 | return [filtered_ count] + 1; | |
7585ce66 JF |
6894 | } |
6895 | ||
5829aea2 GP |
6896 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6897 | return 45.0f; | |
6898 | }*/ | |
7585ce66 | 6899 | |
5829aea2 GP |
6900 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
6901 | static NSString *reuseIdentifier = @"SectionCell"; | |
7585ce66 | 6902 | |
35f0a3b5 | 6903 | SectionCell *cell = (SectionCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5829aea2 GP |
6904 | if (cell == nil) |
6905 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
7585ce66 | 6906 | |
8e5b801a | 6907 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:[self isEditing]]; |
54043703 | 6908 | |
5829aea2 | 6909 | return cell; |
54043703 JF |
6910 | } |
6911 | ||
5829aea2 | 6912 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
8e5b801a | 6913 | if ([self isEditing]) |
54043703 | 6914 | return; |
7585ce66 | 6915 | |
5829aea2 | 6916 | Section *section = [self sectionAtIndexPath:indexPath]; |
7585ce66 | 6917 | |
5829aea2 GP |
6918 | SectionController *controller = [[[SectionController alloc] |
6919 | initWithDatabase:database_ | |
6920 | section:[section name] | |
6921 | ] autorelease]; | |
6922 | [controller setDelegate:delegate_]; | |
7585ce66 | 6923 | |
5829aea2 | 6924 | [[self navigationController] pushViewController:controller animated:YES]; |
7585ce66 JF |
6925 | } |
6926 | ||
fe8e721f GP |
6927 | - (void) loadView { |
6928 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
6929 | ||
7b33d201 | 6930 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds]] autorelease]; |
fe8e721f GP |
6931 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6932 | [list_ setRowHeight:45.0f]; | |
7b33d201 | 6933 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
6934 | [list_ setDelegate:self]; |
6935 | [[self view] addSubview:list_]; | |
6936 | } | |
6937 | ||
6938 | - (void) viewDidLoad { | |
7d887d0b JF |
6939 | [super viewDidLoad]; |
6940 | ||
fe8e721f GP |
6941 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
6942 | } | |
6943 | ||
6944 | - (void) releaseSubviews { | |
fe8e721f GP |
6945 | list_ = nil; |
6946 | } | |
6947 | ||
5829aea2 GP |
6948 | - (id) initWithDatabase:(Database *)database { |
6949 | if ((self = [super init]) != nil) { | |
6950 | database_ = database; | |
7585ce66 | 6951 | |
7b33d201 JF |
6952 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
6953 | filtered_ = [NSMutableArray arrayWithCapacity:16]; | |
5829aea2 | 6954 | } return self; |
7585ce66 JF |
6955 | } |
6956 | ||
5829aea2 | 6957 | - (void) reloadData { |
fe8e721f GP |
6958 | [super reloadData]; |
6959 | ||
5829aea2 | 6960 | NSArray *packages = [database_ packages]; |
7585ce66 | 6961 | |
5829aea2 GP |
6962 | [sections_ removeAllObjects]; |
6963 | [filtered_ removeAllObjects]; | |
7585ce66 | 6964 | |
5829aea2 | 6965 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
7585ce66 | 6966 | |
5829aea2 GP |
6967 | _trace(); |
6968 | for (Package *package in packages) { | |
6969 | NSString *name([package section]); | |
6970 | NSString *key(name == nil ? @"" : name); | |
7585ce66 | 6971 | |
5829aea2 | 6972 | Section *section; |
7585ce66 | 6973 | |
5829aea2 GP |
6974 | _profile(SectionsView$reloadData$Section) |
6975 | section = [sections objectForKey:key]; | |
6976 | if (section == nil) { | |
6977 | _profile(SectionsView$reloadData$Section$Allocate) | |
729bc910 | 6978 | section = [[[Section alloc] initWithName:key localize:YES] autorelease]; |
5829aea2 GP |
6979 | [sections setObject:section forKey:key]; |
6980 | _end | |
6981 | } | |
6982 | _end | |
7585ce66 | 6983 | |
5829aea2 | 6984 | [section addToCount]; |
7585ce66 | 6985 | |
5829aea2 GP |
6986 | _profile(SectionsView$reloadData$Filter) |
6987 | if (![package valid] || ![package visible]) | |
6988 | continue; | |
6989 | _end | |
7585ce66 | 6990 | |
5829aea2 GP |
6991 | [section addToRow]; |
6992 | } | |
6993 | _trace(); | |
7585ce66 | 6994 | |
5829aea2 | 6995 | [sections_ addObjectsFromArray:[sections allValues]]; |
7585ce66 | 6996 | |
5829aea2 | 6997 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
7585ce66 | 6998 | |
7b33d201 | 6999 | for (Section *section in (id) sections_) { |
5829aea2 GP |
7000 | size_t count([section row]); |
7001 | if (count == 0) | |
7002 | continue; | |
7585ce66 | 7003 | |
5829aea2 GP |
7004 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
7005 | [section setCount:count]; | |
7006 | [filtered_ addObject:section]; | |
7007 | } | |
7585ce66 | 7008 | |
a784d0a4 | 7009 | [self updateNavigationItem]; |
5829aea2 GP |
7010 | [list_ reloadData]; |
7011 | _trace(); | |
7012 | } | |
7585ce66 | 7013 | |
6840bff3 | 7014 | - (void) editButtonClicked { |
8e5b801a | 7015 | [self setEditing:![self isEditing] animated:YES]; |
5829aea2 | 7016 | } |
7585ce66 | 7017 | |
5829aea2 GP |
7018 | @end |
7019 | /* }}} */ | |
7585ce66 | 7020 | |
5829aea2 | 7021 | /* Changes Controller {{{ */ |
cd79e8cf | 7022 | @interface ChangesController : CyteViewController < |
5829aea2 GP |
7023 | UITableViewDataSource, |
7024 | UITableViewDelegate | |
7025 | > { | |
7026 | _transient Database *database_; | |
7027 | unsigned era_; | |
56bf1e78 | 7028 | _H<NSArray> packages_; |
7b33d201 | 7029 | _H<NSMutableArray> sections_; |
bf7c998c | 7030 | _H<UITableView, 2> list_; |
5829aea2 | 7031 | unsigned upgrades_; |
5829aea2 | 7032 | } |
7585ce66 | 7033 | |
ea3bb538 | 7034 | - (id) initWithDatabase:(Database *)database; |
7585ce66 | 7035 | |
5829aea2 | 7036 | @end |
7585ce66 | 7037 | |
5829aea2 | 7038 | @implementation ChangesController |
7585ce66 | 7039 | |
fe8e721f GP |
7040 | - (NSURL *) navigationURL { |
7041 | return [NSURL URLWithString:@"cydia://changes"]; | |
7042 | } | |
7043 | ||
5829aea2 GP |
7044 | - (void) viewDidAppear:(BOOL)animated { |
7045 | [super viewDidAppear:animated]; | |
a70cd4ba | 7046 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; |
7585ce66 JF |
7047 | } |
7048 | ||
5829aea2 GP |
7049 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
7050 | NSInteger count([sections_ count]); | |
7051 | return count == 0 ? 1 : count; | |
7585ce66 JF |
7052 | } |
7053 | ||
5829aea2 GP |
7054 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
7055 | if ([sections_ count] == 0) | |
7056 | return nil; | |
7057 | return [[sections_ objectAtIndex:section] name]; | |
b5e7eebb GP |
7058 | } |
7059 | ||
5829aea2 GP |
7060 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
7061 | if ([sections_ count] == 0) | |
7062 | return 0; | |
7063 | return [[sections_ objectAtIndex:section] count]; | |
7064 | } | |
b5e7eebb | 7065 | |
5829aea2 GP |
7066 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
7067 | @synchronized (database_) { | |
7068 | if ([database_ era] != era_) | |
7069 | return nil; | |
b5e7eebb | 7070 | |
5829aea2 GP |
7071 | NSUInteger sectionIndex([path section]); |
7072 | if (sectionIndex >= [sections_ count]) | |
7073 | return nil; | |
7074 | Section *section([sections_ objectAtIndex:sectionIndex]); | |
7075 | NSInteger row([path row]); | |
56bf1e78 | 7076 | return [[[packages_ objectAtIndex:([section row] + row)] retain] autorelease]; |
5829aea2 | 7077 | } } |
b5e7eebb | 7078 | |
5829aea2 GP |
7079 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
7080 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); | |
7081 | if (cell == nil) | |
7082 | cell = [[[PackageCell alloc] init] autorelease]; | |
59f3d290 | 7083 | [cell setPackage:[self packageAtIndexPath:path] asSummary:false]; |
5829aea2 | 7084 | return cell; |
9cb0bff2 GP |
7085 | } |
7086 | ||
5829aea2 GP |
7087 | - (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path { |
7088 | Package *package([self packageAtIndexPath:path]); | |
57e8b225 | 7089 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id]] autorelease]); |
5829aea2 | 7090 | [view setDelegate:delegate_]; |
5829aea2 GP |
7091 | [[self navigationController] pushViewController:view animated:YES]; |
7092 | return path; | |
b5e7eebb GP |
7093 | } |
7094 | ||
5829aea2 GP |
7095 | - (void) refreshButtonClicked { |
7096 | [delegate_ beginUpdate]; | |
7097 | [[self navigationItem] setLeftBarButtonItem:nil animated:YES]; | |
b5e7eebb GP |
7098 | } |
7099 | ||
5829aea2 GP |
7100 | - (void) upgradeButtonClicked { |
7101 | [delegate_ distUpgrade]; | |
b5e7eebb GP |
7102 | } |
7103 | ||
fe8e721f GP |
7104 | - (void) loadView { |
7105 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
7106 | ||
7b33d201 | 7107 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; |
fe8e721f GP |
7108 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7109 | [list_ setRowHeight:73]; | |
7b33d201 | 7110 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
7111 | [list_ setDelegate:self]; |
7112 | [[self view] addSubview:list_]; | |
7113 | } | |
7114 | ||
7115 | - (void) viewDidLoad { | |
7d887d0b JF |
7116 | [super viewDidLoad]; |
7117 | ||
fe8e721f GP |
7118 | [[self navigationItem] setTitle:UCLocalize("CHANGES")]; |
7119 | } | |
7120 | ||
7121 | - (void) releaseSubviews { | |
fe8e721f GP |
7122 | list_ = nil; |
7123 | } | |
5829aea2 | 7124 | |
ea3bb538 | 7125 | - (id) initWithDatabase:(Database *)database { |
b5e7eebb GP |
7126 | if ((self = [super init]) != nil) { |
7127 | database_ = database; | |
b5e7eebb | 7128 | |
56bf1e78 | 7129 | packages_ = [NSArray array]; |
7b33d201 | 7130 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
5829aea2 | 7131 | } return self; |
807ae6d7 JF |
7132 | } |
7133 | ||
9c5737d5 | 7134 | - (NSMutableArray *) _reloadPackages { |
c28bc6f1 JF |
7135 | @synchronized (database_) { |
7136 | era_ = [database_ era]; | |
7137 | NSArray *packages([database_ packages]); | |
7138 | ||
56bf1e78 | 7139 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); |
9fe5e5f8 | 7140 | |
5829aea2 GP |
7141 | _trace(); |
7142 | _profile(ChangesController$_reloadPackages$Filter) | |
7143 | for (Package *package in packages) | |
7144 | if ([package upgradableAndEssential:YES] || [package visible]) | |
56bf1e78 | 7145 | CFArrayAppendValue((CFMutableArrayRef) filtered, package); |
5829aea2 GP |
7146 | _end |
7147 | _trace(); | |
7148 | _profile(ChangesController$_reloadPackages$radixSort) | |
56bf1e78 | 7149 | [filtered radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackageChangesRadix) withContext:NULL]; |
5829aea2 GP |
7150 | _end |
7151 | _trace(); | |
56bf1e78 JF |
7152 | |
7153 | return filtered; | |
c28bc6f1 | 7154 | } } |
9fe5e5f8 | 7155 | |
a70cd4ba | 7156 | - (void) _reloadData { |
ae60e2c1 JF |
7157 | NSArray *packages; |
7158 | ||
c28bc6f1 JF |
7159 | reload: |
7160 | if (true) { | |
7161 | UIProgressHUD *hud([delegate_ addProgressHUD]); | |
7162 | [hud setText:UCLocalize("LOADING")]; | |
7163 | //NSLog(@"HUD:%@::%@", delegate_, hud); | |
ae60e2c1 | 7164 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
c28bc6f1 JF |
7165 | [delegate_ removeProgressHUD:hud]; |
7166 | } else { | |
ae60e2c1 | 7167 | packages = [self _reloadPackages]; |
c28bc6f1 | 7168 | } |
807ae6d7 | 7169 | |
c28bc6f1 JF |
7170 | @synchronized (database_) { |
7171 | if (era_ != [database_ era]) | |
7172 | goto reload; | |
807ae6d7 | 7173 | |
ae60e2c1 | 7174 | packages_ = packages; |
bdca103d JF |
7175 | [sections_ removeAllObjects]; |
7176 | ||
5829aea2 GP |
7177 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
7178 | Section *ignored = nil; | |
7179 | Section *section = nil; | |
7180 | time_t last = 0; | |
807ae6d7 | 7181 | |
5829aea2 GP |
7182 | upgrades_ = 0; |
7183 | bool unseens = false; | |
807ae6d7 | 7184 | |
5829aea2 | 7185 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
807ae6d7 | 7186 | |
56bf1e78 JF |
7187 | for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) { |
7188 | Package *package = [packages_ objectAtIndex:offset]; | |
807ae6d7 | 7189 | |
5829aea2 | 7190 | BOOL uae = [package upgradableAndEssential:YES]; |
807ae6d7 | 7191 | |
5829aea2 GP |
7192 | if (!uae) { |
7193 | unseens = true; | |
7194 | time_t seen([package seen]); | |
d90a4cd6 | 7195 | |
5829aea2 GP |
7196 | if (section == nil || last != seen) { |
7197 | last = seen; | |
7198 | ||
7199 | NSString *name; | |
7200 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); | |
7201 | [name autorelease]; | |
7202 | ||
7203 | _profile(ChangesController$reloadData$Allocate) | |
7204 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; | |
7205 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
7206 | [sections_ addObject:section]; | |
7207 | _end | |
7208 | } | |
7209 | ||
7210 | [section addToCount]; | |
7211 | } else if ([package ignored]) { | |
7212 | if (ignored == nil) { | |
7213 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7214 | } | |
7215 | [ignored addToCount]; | |
7216 | } else { | |
7217 | ++upgrades_; | |
7218 | [upgradable addToCount]; | |
7219 | } | |
7220 | } | |
7221 | _trace(); | |
7222 | ||
7223 | CFRelease(formatter); | |
7224 | ||
7225 | if (unseens) { | |
7226 | Section *last = [sections_ lastObject]; | |
7227 | size_t count = [last count]; | |
56bf1e78 | 7228 | [packages_ removeObjectsInRange:NSMakeRange([packages_ count] - count, count)]; |
5829aea2 GP |
7229 | [sections_ removeLastObject]; |
7230 | } | |
7231 | ||
7232 | if ([ignored count] != 0) | |
7233 | [sections_ insertObject:ignored atIndex:0]; | |
7234 | if (upgrades_ != 0) | |
7235 | [sections_ insertObject:upgradable atIndex:0]; | |
7236 | ||
7237 | [list_ reloadData]; | |
7238 | ||
7239 | if (upgrades_ > 0) | |
7240 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7241 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7242 | style:UIBarButtonItemStylePlain | |
7243 | target:self | |
7244 | action:@selector(upgradeButtonClicked) | |
7245 | ] autorelease]]; | |
7246 | ||
7247 | if (![delegate_ updating]) | |
7248 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7249 | initWithTitle:UCLocalize("REFRESH") | |
7250 | style:UIBarButtonItemStylePlain | |
7251 | target:self | |
7252 | action:@selector(refreshButtonClicked) | |
7253 | ] autorelease]]; | |
7254 | ||
7255 | PrintTimes(); | |
7256 | } } | |
7257 | ||
a70cd4ba JF |
7258 | - (void) reloadData { |
7259 | [super reloadData]; | |
7260 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
7261 | } | |
7262 | ||
5829aea2 GP |
7263 | @end |
7264 | /* }}} */ | |
7265 | /* Search Controller {{{ */ | |
f50860ee | 7266 | @interface SearchController : FilteredPackageListController < |
5829aea2 GP |
7267 | UISearchBarDelegate |
7268 | > { | |
bf7c998c | 7269 | _H<UISearchBar, 1> search_; |
fe8e721f | 7270 | BOOL searchloaded_; |
5829aea2 GP |
7271 | } |
7272 | ||
43625891 | 7273 | - (id) initWithDatabase:(Database *)database query:(NSString *)query; |
5829aea2 | 7274 | - (void) reloadData; |
d90a4cd6 GP |
7275 | |
7276 | @end | |
7277 | ||
5829aea2 | 7278 | @implementation SearchController |
d90a4cd6 | 7279 | |
fe8e721f | 7280 | - (NSURL *) navigationURL { |
35f0a3b5 GP |
7281 | if ([search_ text] == nil || [[search_ text] isEqualToString:@""]) |
7282 | return [NSURL URLWithString:@"cydia://search"]; | |
7283 | else | |
7284 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://search/%@", [search_ text]]]; | |
fe8e721f GP |
7285 | } |
7286 | ||
59f3d290 | 7287 | - (void) useSearch { |
d84597fe | 7288 | [self setObject:[[search_ text] componentsSeparatedByString:@" "] forFilter:@selector(isUnfilteredAndSearchedForBy:)]; |
59f3d290 JF |
7289 | [self clearData]; |
7290 | [self reloadData]; | |
7291 | } | |
7292 | ||
7293 | - (void) viewWillAppear:(BOOL)animated { | |
7294 | [super viewWillAppear:animated]; | |
7295 | ||
7296 | if ([self filter] == @selector(isUnfilteredAndSelectedForBy:)) | |
7297 | [self useSearch]; | |
7298 | } | |
7299 | ||
3025d5b4 JF |
7300 | - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar { |
7301 | [self setObject:[search_ text] forFilter:@selector(isUnfilteredAndSelectedForBy:)]; | |
59f3d290 JF |
7302 | [self clearData]; |
7303 | [self reloadData]; | |
3025d5b4 JF |
7304 | } |
7305 | ||
7306 | - (void) searchBarButtonClicked:(UISearchBar *)searchBar { | |
5829aea2 | 7307 | [search_ resignFirstResponder]; |
59f3d290 | 7308 | [self useSearch]; |
5829aea2 GP |
7309 | } |
7310 | ||
3025d5b4 JF |
7311 | - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar { |
7312 | [search_ setText:@""]; | |
7313 | [self searchBarButtonClicked:searchBar]; | |
7314 | } | |
7315 | ||
7316 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { | |
7317 | [self searchBarButtonClicked:searchBar]; | |
7318 | } | |
7319 | ||
5829aea2 | 7320 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { |
f50860ee | 7321 | [self setObject:text forFilter:@selector(isUnfilteredAndSelectedForBy:)]; |
5829aea2 GP |
7322 | [self reloadData]; |
7323 | } | |
7324 | ||
3025d5b4 | 7325 | - (bool) shouldYield { |
56bf1e78 JF |
7326 | return YES; |
7327 | } | |
7328 | ||
7329 | - (bool) shouldBlock { | |
3025d5b4 JF |
7330 | return [self filter] == @selector(isUnfilteredAndSearchedForBy:); |
7331 | } | |
7332 | ||
59f3d290 JF |
7333 | - (bool) isSummarized { |
7334 | return [self filter] == @selector(isUnfilteredAndSelectedForBy:); | |
7335 | } | |
7336 | ||
9c5737d5 JF |
7337 | - (bool) showsSections { |
7338 | return false; | |
7339 | } | |
7340 | ||
7341 | - (NSMutableArray *) _reloadPackages { | |
7342 | NSMutableArray *packages([super _reloadPackages]); | |
7343 | if ([self filter] == @selector(isUnfilteredAndSearchedForBy:)) | |
7344 | [packages radixSortUsingSelector:@selector(rank)]; | |
7345 | return packages; | |
7346 | } | |
7347 | ||
43625891 | 7348 | - (id) initWithDatabase:(Database *)database query:(NSString *)query { |
d84597fe | 7349 | if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH") filter:@selector(isUnfilteredAndSearchedForBy:) with:[query componentsSeparatedByString:@" "]])) { |
900095fd JF |
7350 | search_ = [[[UISearchBar alloc] init] autorelease]; |
7351 | [search_ setDelegate:self]; | |
43625891 JF |
7352 | |
7353 | if (query != nil) | |
7354 | [search_ setText:query]; | |
fe8e721f | 7355 | } return self; |
5829aea2 GP |
7356 | } |
7357 | ||
6840bff3 | 7358 | - (void) viewDidAppear:(BOOL)animated { |
5829aea2 | 7359 | [super viewDidAppear:animated]; |
fe8e721f GP |
7360 | |
7361 | if (!searchloaded_) { | |
7362 | searchloaded_ = YES; | |
7363 | [search_ setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; | |
5829aea2 GP |
7364 | [search_ layoutSubviews]; |
7365 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; | |
7366 | ||
7367 | UITextField *textField; | |
7368 | if ([search_ respondsToSelector:@selector(searchField)]) | |
7369 | textField = [search_ searchField]; | |
7370 | else | |
7371 | textField = MSHookIvar<UITextField *>(search_, "_searchField"); | |
7372 | ||
7373 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
5829aea2 GP |
7374 | [textField setEnablesReturnKeyAutomatically:NO]; |
7375 | [[self navigationItem] setTitleView:textField]; | |
d90a4cd6 | 7376 | } |
5829aea2 | 7377 | } |
d90a4cd6 | 7378 | |
d90a4cd6 | 7379 | - (void) reloadData { |
d84597fe JF |
7380 | id object([search_ text]); |
7381 | if ([self filter] == @selector(isUnfilteredAndSearchedForBy:)) | |
7382 | object = [object componentsSeparatedByString:@" "]; | |
7383 | ||
7384 | [self setObject:object]; | |
f50860ee | 7385 | [self resetCursor]; |
9dac415b JF |
7386 | |
7387 | [super reloadData]; | |
d90a4cd6 GP |
7388 | } |
7389 | ||
5829aea2 GP |
7390 | - (void) didSelectPackage:(Package *)package { |
7391 | [search_ resignFirstResponder]; | |
7392 | [super didSelectPackage:package]; | |
d90a4cd6 GP |
7393 | } |
7394 | ||
7395 | @end | |
7396 | /* }}} */ | |
5829aea2 | 7397 | /* Package Settings Controller {{{ */ |
cd79e8cf | 7398 | @interface PackageSettingsController : CyteViewController < |
c21004b9 JF |
7399 | UITableViewDataSource, |
7400 | UITableViewDelegate | |
7401 | > { | |
807ae6d7 | 7402 | _transient Database *database_; |
7b33d201 JF |
7403 | _H<NSString> name_; |
7404 | _H<Package> package_; | |
bf7c998c | 7405 | _H<UITableView, 2> table_; |
7b33d201 JF |
7406 | _H<UISwitch> subscribedSwitch_; |
7407 | _H<UISwitch> ignoredSwitch_; | |
7408 | _H<UITableViewCell> subscribedCell_; | |
7409 | _H<UITableViewCell> ignoredCell_; | |
807ae6d7 JF |
7410 | } |
7411 | ||
5829aea2 | 7412 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
c21004b9 | 7413 | |
807ae6d7 JF |
7414 | @end |
7415 | ||
5829aea2 | 7416 | @implementation PackageSettingsController |
807ae6d7 | 7417 | |
fe8e721f GP |
7418 | - (NSURL *) navigationURL { |
7419 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/settings", [package_ id]]]; | |
7420 | } | |
7421 | ||
5829aea2 GP |
7422 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7423 | if (package_ == nil) | |
7424 | return 0; | |
e954c30a | 7425 | |
2136717a DH |
7426 | if ([package_ installed] == nil) |
7427 | return 1; | |
7428 | else | |
7429 | return 2; | |
e954c30a GP |
7430 | } |
7431 | ||
5829aea2 GP |
7432 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7433 | if (package_ == nil) | |
7434 | return 0; | |
7435 | ||
2136717a DH |
7436 | // both sections contain just one item right now. |
7437 | return 1; | |
b5e7eebb GP |
7438 | } |
7439 | ||
5829aea2 | 7440 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
2136717a | 7441 | return nil; |
e954c30a GP |
7442 | } |
7443 | ||
5829aea2 | 7444 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
2136717a DH |
7445 | if (section == 0) |
7446 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
7447 | else | |
7448 | return UCLocalize("IGNORE_UPGRADES_EX"); | |
807ae6d7 JF |
7449 | } |
7450 | ||
5829aea2 GP |
7451 | - (void) onSubscribed:(id)control { |
7452 | bool value([control isOn]); | |
7453 | if (package_ == nil) | |
7454 | return; | |
7455 | if ([package_ setSubscribed:value]) | |
7456 | [delegate_ updateData]; | |
807ae6d7 JF |
7457 | } |
7458 | ||
e5e70358 JF |
7459 | - (void) _updateIgnored { |
7460 | const char *package([name_ UTF8String]); | |
7461 | bool on([ignoredSwitch_ isOn]); | |
7462 | ||
7463 | pid_t pid(ExecFork()); | |
7464 | if (pid == 0) { | |
7465 | FILE *dpkg(popen("dpkg --set-selections", "w")); | |
7466 | fwrite(package, strlen(package), 1, dpkg); | |
7467 | ||
7468 | if (on) | |
7469 | fwrite(" hold\n", 6, 1, dpkg); | |
7470 | else | |
7471 | fwrite(" install\n", 9, 1, dpkg); | |
7472 | ||
7473 | pclose(dpkg); | |
7474 | ||
7475 | exit(0); | |
7476 | _assert(false); | |
7477 | } | |
7478 | ||
7479 | _forever { | |
7480 | int status; | |
7481 | int result(waitpid(pid, &status, 0)); | |
7482 | ||
7483 | if (result != -1) { | |
7484 | _assert(result == pid); | |
7485 | break; | |
7486 | } | |
7487 | } | |
7488 | } | |
7489 | ||
5829aea2 | 7490 | - (void) onIgnored:(id)control { |
e5e70358 JF |
7491 | NSInvocation *invocation([NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(_updateIgnored)]]); |
7492 | [invocation setTarget:self]; | |
7493 | [invocation setSelector:@selector(_updateIgnored)]; | |
7494 | ||
7495 | [delegate_ reloadDataWithInvocation:invocation]; | |
5829aea2 | 7496 | } |
807ae6d7 | 7497 | |
46aa9775 | 7498 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7499 | if (package_ == nil) |
7500 | return nil; | |
b5e7eebb | 7501 | |
2136717a | 7502 | switch ([indexPath section]) { |
5829aea2 GP |
7503 | case 0: return subscribedCell_; |
7504 | case 1: return ignoredCell_; | |
36fbb2aa | 7505 | |
5829aea2 GP |
7506 | _nodefault |
7507 | } | |
807ae6d7 | 7508 | |
5829aea2 | 7509 | return nil; |
807ae6d7 JF |
7510 | } |
7511 | ||
fe8e721f GP |
7512 | - (void) loadView { |
7513 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
807ae6d7 | 7514 | |
7b33d201 | 7515 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f | 7516 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b33d201 | 7517 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f GP |
7518 | [table_ setDelegate:self]; |
7519 | [[self view] addSubview:table_]; | |
807ae6d7 | 7520 | |
7b33d201 | 7521 | subscribedSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7522 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7523 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7524 | |
7b33d201 | 7525 | ignoredSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7526 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7527 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7528 | |
7b33d201 | 7529 | subscribedCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7530 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; |
7531 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
7532 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
5829aea2 | 7533 | |
7b33d201 | 7534 | ignoredCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7535 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; |
7536 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
7537 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
fe8e721f | 7538 | } |
5829aea2 | 7539 | |
fe8e721f | 7540 | - (void) viewDidLoad { |
7d887d0b JF |
7541 | [super viewDidLoad]; |
7542 | ||
fe8e721f GP |
7543 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
7544 | } | |
5829aea2 | 7545 | |
fe8e721f | 7546 | - (void) releaseSubviews { |
fe8e721f | 7547 | ignoredCell_ = nil; |
fe8e721f | 7548 | subscribedCell_ = nil; |
fe8e721f | 7549 | table_ = nil; |
fe8e721f | 7550 | ignoredSwitch_ = nil; |
fe8e721f GP |
7551 | subscribedSwitch_ = nil; |
7552 | } | |
7553 | ||
7554 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { | |
6840bff3 | 7555 | if ((self = [super init]) != nil) { |
fe8e721f | 7556 | database_ = database; |
7b33d201 | 7557 | name_ = package; |
807ae6d7 JF |
7558 | } return self; |
7559 | } | |
7560 | ||
7561 | - (void) reloadData { | |
fe8e721f GP |
7562 | [super reloadData]; |
7563 | ||
5829aea2 | 7564 | package_ = [database_ packageWithName:name_]; |
f3e2c0ac | 7565 | |
5829aea2 | 7566 | if (package_ != nil) { |
5829aea2 GP |
7567 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; |
7568 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
f3e2c0ac | 7569 | } // XXX: what now, G? |
dc5812ec | 7570 | |
5829aea2 GP |
7571 | [table_ reloadData]; |
7572 | } | |
6d9712c4 | 7573 | |
dc5812ec | 7574 | @end |
2367a917 | 7575 | /* }}} */ |
d90a4cd6 | 7576 | |
5829aea2 | 7577 | /* Installed Controller {{{ */ |
f50860ee | 7578 | @interface InstalledController : FilteredPackageListController { |
5829aea2 | 7579 | BOOL expert_; |
dc5812ec JF |
7580 | } |
7581 | ||
5829aea2 GP |
7582 | - (id) initWithDatabase:(Database *)database; |
7583 | ||
7584 | - (void) updateRoleButton; | |
7585 | - (void) queueStatusDidChange; | |
dc5812ec | 7586 | |
dc5812ec JF |
7587 | @end |
7588 | ||
5829aea2 | 7589 | @implementation InstalledController |
b4d89997 | 7590 | |
fe8e721f GP |
7591 | - (NSURL *) navigationURL { |
7592 | return [NSURL URLWithString:@"cydia://installed"]; | |
7593 | } | |
b5e7eebb | 7594 | |
5829aea2 GP |
7595 | - (id) initWithDatabase:(Database *)database { |
7596 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED") filter:@selector(isInstalledAndUnfiltered:) with:[NSNumber numberWithBool:YES]]) != nil) { | |
7597 | [self updateRoleButton]; | |
7598 | [self queueStatusDidChange]; | |
7599 | } return self; | |
dc5812ec JF |
7600 | } |
7601 | ||
5829aea2 GP |
7602 | #if !AlwaysReload |
7603 | - (void) queueButtonClicked { | |
7604 | [delegate_ queue]; | |
dc5812ec | 7605 | } |
5829aea2 | 7606 | #endif |
dc5812ec | 7607 | |
5829aea2 GP |
7608 | - (void) queueStatusDidChange { |
7609 | #if !AlwaysReload | |
7610 | if (IsWildcat_) { | |
7611 | if (Queuing_) { | |
7612 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7613 | initWithTitle:UCLocalize("QUEUE") | |
7614 | style:UIBarButtonItemStyleDone | |
7615 | target:self | |
7616 | action:@selector(queueButtonClicked) | |
7617 | ] autorelease]]; | |
7618 | } else { | |
7619 | [[self navigationItem] setLeftBarButtonItem:nil]; | |
7620 | } | |
7621 | } | |
7622 | #endif | |
dc5812ec JF |
7623 | } |
7624 | ||
5829aea2 GP |
7625 | - (void) updateRoleButton { |
7626 | if (Role_ != nil && ![Role_ isEqualToString:@"Developer"]) | |
7627 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7628 | initWithTitle:(expert_ ? UCLocalize("EXPERT") : UCLocalize("SIMPLE")) | |
7629 | style:(expert_ ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
7630 | target:self | |
7631 | action:@selector(roleButtonClicked) | |
7632 | ] autorelease]]; | |
dc5812ec JF |
7633 | } |
7634 | ||
5829aea2 | 7635 | - (void) roleButtonClicked { |
f50860ee GP |
7636 | [self setObject:[NSNumber numberWithBool:expert_]]; |
7637 | [self reloadData]; | |
5829aea2 | 7638 | expert_ = !expert_; |
dc5812ec | 7639 | |
5829aea2 | 7640 | [self updateRoleButton]; |
9a7b04c5 JF |
7641 | } |
7642 | ||
5829aea2 GP |
7643 | @end |
7644 | /* }}} */ | |
b5e7eebb | 7645 | |
5829aea2 | 7646 | /* Source Cell {{{ */ |
a9311516 | 7647 | @interface SourceCell : CyteTableViewCell < |
b97fcfc6 | 7648 | CyteTableViewCellDelegate |
5829aea2 | 7649 | > { |
7b33d201 JF |
7650 | _H<UIImage> icon_; |
7651 | _H<NSString> origin_; | |
7652 | _H<NSString> label_; | |
5829aea2 | 7653 | } |
dc5812ec | 7654 | |
5829aea2 | 7655 | - (void) setSource:(Source *)source; |
6da1297d | 7656 | |
5829aea2 | 7657 | @end |
dc5812ec | 7658 | |
5829aea2 | 7659 | @implementation SourceCell |
36bb2ca2 | 7660 | |
8252b666 JF |
7661 | - (void) _setImage:(UIImage *)image { |
7662 | icon_ = image; | |
7663 | [content_ setNeedsDisplay]; | |
7664 | } | |
7665 | ||
7666 | - (void) _setSource:(Source *)source { | |
7667 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
7668 | ||
7669 | if (NSString *base = [source base]) | |
7670 | if ([base length] != 0) { | |
7671 | NSURL *url([NSURL URLWithString:[base stringByAppendingString:@"CydiaIcon.png"]]); | |
7672 | ||
e2add7b8 JF |
7673 | if (NSData *data = [NSURLConnection |
7674 | sendSynchronousRequest:[NSURLRequest | |
7675 | requestWithURL:url | |
7676 | //cachePolicy:NSURLRequestUseProtocolCachePolicy | |
7677 | //timeoutInterval:5 | |
7678 | ] | |
7679 | ||
7680 | returningResponse:NULL | |
7681 | error:NULL | |
7682 | ]) | |
8252b666 JF |
7683 | if (UIImage *image = [UIImage imageWithData:data]) |
7684 | [self performSelectorOnMainThread:@selector(_setImage:) withObject:image waitUntilDone:NO]; | |
7685 | } | |
7686 | ||
7687 | [pool release]; | |
7688 | } | |
7689 | ||
5829aea2 | 7690 | - (void) setSource:(Source *)source { |
8252b666 | 7691 | icon_ = [UIImage applicationImageNamed:@"unknown.png"]; |
5829aea2 | 7692 | |
7b33d201 JF |
7693 | origin_ = [source name]; |
7694 | label_ = [source uri]; | |
5829aea2 GP |
7695 | |
7696 | [content_ setNeedsDisplay]; | |
8252b666 JF |
7697 | |
7698 | [NSThread detachNewThreadSelector:@selector(_setSource:) toTarget:self withObject:source]; | |
77801ff1 JF |
7699 | } |
7700 | ||
5829aea2 GP |
7701 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
7702 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
7703 | UIView *content([self contentView]); | |
7704 | CGRect bounds([content bounds]); | |
77801ff1 | 7705 | |
b97fcfc6 | 7706 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
5829aea2 GP |
7707 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7708 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
7709 | [content addSubview:content_]; | |
dc5812ec | 7710 | |
5829aea2 GP |
7711 | [content_ setDelegate:self]; |
7712 | [content_ setOpaque:YES]; | |
7713 | } return self; | |
7714 | } | |
b4d89997 | 7715 | |
003fc610 GP |
7716 | - (NSString *) accessibilityLabel { |
7717 | return label_; | |
7718 | } | |
7719 | ||
5829aea2 GP |
7720 | - (void) drawContentRect:(CGRect)rect { |
7721 | bool highlighted(highlighted_); | |
7722 | float width(rect.size.width); | |
36bb2ca2 | 7723 | |
5829aea2 GP |
7724 | if (icon_ != nil) |
7725 | [icon_ drawInRect:CGRectMake(10, 10, 30, 30)]; | |
36bb2ca2 | 7726 | |
5829aea2 GP |
7727 | if (highlighted) |
7728 | UISetColor(White_); | |
dc5812ec | 7729 | |
5829aea2 GP |
7730 | if (!highlighted) |
7731 | UISetColor(Black_); | |
7732 | [origin_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - 80) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; | |
f79a4512 | 7733 | |
5829aea2 GP |
7734 | if (!highlighted) |
7735 | UISetColor(Blue_); | |
7736 | [label_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; | |
7737 | } | |
dc5812ec | 7738 | |
5829aea2 GP |
7739 | @end |
7740 | /* }}} */ | |
3f8edf70 | 7741 | /* Source Controller {{{ */ |
f50860ee | 7742 | @interface SourceController : FilteredPackageListController { |
2d5372fc | 7743 | _transient Source *source_; |
7b33d201 | 7744 | _H<NSString> key_; |
3f8edf70 GP |
7745 | } |
7746 | ||
7747 | - (id) initWithDatabase:(Database *)database source:(Source *)source; | |
7748 | ||
7749 | @end | |
7750 | ||
7751 | @implementation SourceController | |
7752 | ||
fe8e721f GP |
7753 | - (NSURL *) navigationURL { |
7754 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sources/%@", [source_ name]]]; | |
7755 | } | |
7756 | ||
3f8edf70 | 7757 | - (id) initWithDatabase:(Database *)database source:(Source *)source { |
7c642bd6 JF |
7758 | if ((self = [super initWithDatabase:database title:[source label] filter:@selector(isVisibleInSource:) with:source]) != nil) { |
7759 | source_ = source; | |
7b33d201 | 7760 | key_ = [source key]; |
3f8edf70 GP |
7761 | } return self; |
7762 | } | |
7763 | ||
2d5372fc | 7764 | - (void) reloadData { |
d669236d | 7765 | source_ = [database_ sourceWithKey:key_]; |
7b33d201 | 7766 | key_ = [source_ key]; |
2d5372fc | 7767 | [self setObject:source_]; |
9dac415b | 7768 | |
b6494e70 | 7769 | [[self navigationItem] setTitle:[source_ label]]; |
2d5372fc GP |
7770 | |
7771 | [super reloadData]; | |
7772 | } | |
7773 | ||
3f8edf70 GP |
7774 | @end |
7775 | /* }}} */ | |
7776 | /* Sources Controller {{{ */ | |
cd79e8cf | 7777 | @interface SourcesController : CyteViewController < |
5829aea2 GP |
7778 | UITableViewDataSource, |
7779 | UITableViewDelegate | |
7780 | > { | |
7781 | _transient Database *database_; | |
bf7c998c | 7782 | _H<UITableView, 2> list_; |
7b33d201 | 7783 | _H<NSMutableArray> sources_; |
5829aea2 | 7784 | int offset_; |
723a0072 | 7785 | |
7b33d201 JF |
7786 | _H<NSString> href_; |
7787 | _H<UIProgressHUD> hud_; | |
7788 | _H<NSError> error_; | |
dc63e78f | 7789 | |
5829aea2 GP |
7790 | //NSURLConnection *installer_; |
7791 | NSURLConnection *trivial_; | |
7792 | NSURLConnection *trivial_bz2_; | |
7793 | NSURLConnection *trivial_gz_; | |
7794 | //NSURLConnection *automatic_; | |
b4d89997 | 7795 | |
5829aea2 GP |
7796 | BOOL cydia_; |
7797 | } | |
dc5812ec | 7798 | |
5829aea2 | 7799 | - (id) initWithDatabase:(Database *)database; |
5829aea2 GP |
7800 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated; |
7801 | ||
7802 | @end | |
7803 | ||
7804 | @implementation SourcesController | |
7805 | ||
7806 | - (void) _releaseConnection:(NSURLConnection *)connection { | |
7807 | if (connection != nil) { | |
7808 | [connection cancel]; | |
7809 | //[connection setDelegate:nil]; | |
7810 | [connection release]; | |
36bb2ca2 | 7811 | } |
5829aea2 | 7812 | } |
dc5812ec | 7813 | |
5829aea2 | 7814 | - (void) dealloc { |
5829aea2 GP |
7815 | //[self _releaseConnection:installer_]; |
7816 | [self _releaseConnection:trivial_]; | |
7817 | [self _releaseConnection:trivial_gz_]; | |
7818 | [self _releaseConnection:trivial_bz2_]; | |
7819 | //[self _releaseConnection:automatic_]; | |
3178d79b | 7820 | |
5829aea2 GP |
7821 | [super dealloc]; |
7822 | } | |
b5e7eebb | 7823 | |
fe8e721f GP |
7824 | - (NSURL *) navigationURL { |
7825 | return [NSURL URLWithString:@"cydia://sources"]; | |
7826 | } | |
7827 | ||
5829aea2 GP |
7828 | - (void) viewDidAppear:(BOOL)animated { |
7829 | [super viewDidAppear:animated]; | |
7830 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7831 | } | |
9bb3b295 | 7832 | |
5829aea2 GP |
7833 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7834 | return offset_ == 0 ? 1 : 2; | |
7835 | } | |
8fe19fc1 | 7836 | |
5829aea2 GP |
7837 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
7838 | switch (section + (offset_ == 0 ? 1 : 0)) { | |
7839 | case 0: return UCLocalize("ENTERED_BY_USER"); | |
7840 | case 1: return UCLocalize("INSTALLED_BY_PACKAGE"); | |
7841 | ||
7842 | _nodefault | |
7843 | } | |
36bb2ca2 | 7844 | } |
b4d89997 | 7845 | |
5829aea2 GP |
7846 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7847 | int count = [sources_ count]; | |
7848 | switch (section) { | |
7849 | case 0: return (offset_ == 0 ? count : offset_); | |
7850 | case 1: return count - offset_; | |
b4d89997 | 7851 | |
5829aea2 GP |
7852 | _nodefault |
7853 | } | |
7854 | } | |
3178d79b | 7855 | |
5829aea2 GP |
7856 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
7857 | unsigned idx = 0; | |
7858 | switch (indexPath.section) { | |
7859 | case 0: idx = indexPath.row; break; | |
7860 | case 1: idx = indexPath.row + offset_; break; | |
3178d79b | 7861 | |
5829aea2 GP |
7862 | _nodefault |
7863 | } | |
7864 | return [sources_ objectAtIndex:idx]; | |
b4d89997 JF |
7865 | } |
7866 | ||
5829aea2 GP |
7867 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
7868 | static NSString *cellIdentifier = @"SourceCell"; | |
7869 | ||
7870 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
7871 | if(cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; | |
7872 | [cell setSource:[self sourceAtIndexPath:indexPath]]; | |
b89fa270 | 7873 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
5829aea2 GP |
7874 | |
7875 | return cell; | |
f6e13561 GP |
7876 | } |
7877 | ||
5829aea2 GP |
7878 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
7879 | Source *source = [self sourceAtIndexPath:indexPath]; | |
7880 | ||
3f8edf70 | 7881 | SourceController *controller = [[[SourceController alloc] |
5829aea2 | 7882 | initWithDatabase:database_ |
3f8edf70 | 7883 | source:source |
5829aea2 GP |
7884 | ] autorelease]; |
7885 | ||
3f8edf70 | 7886 | [controller setDelegate:delegate_]; |
5829aea2 | 7887 | |
3f8edf70 | 7888 | [[self navigationController] pushViewController:controller animated:YES]; |
36bb2ca2 | 7889 | } |
b4d89997 | 7890 | |
6840bff3 | 7891 | - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7892 | Source *source = [self sourceAtIndexPath:indexPath]; |
7893 | return [source record] != nil; | |
dcb47737 RP |
7894 | } |
7895 | ||
6840bff3 | 7896 | - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
0e1aa02c JF |
7897 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
7898 | Source *source = [self sourceAtIndexPath:indexPath]; | |
7899 | [Sources_ removeObjectForKey:[source key]]; | |
7900 | [delegate_ syncData]; | |
7901 | } | |
5829aea2 | 7902 | } |
bcccf498 | 7903 | |
5829aea2 | 7904 | - (void) complete { |
93460555 | 7905 | [delegate_ addTrivialSource:href_]; |
5829aea2 | 7906 | [delegate_ syncData]; |
3178d79b JF |
7907 | } |
7908 | ||
5829aea2 GP |
7909 | - (NSString *) getWarning { |
7910 | NSString *href(href_); | |
7911 | NSRange colon([href rangeOfString:@"://"]); | |
7912 | if (colon.location != NSNotFound) | |
7913 | href = [href substringFromIndex:(colon.location + 3)]; | |
7914 | href = [href stringByAddingPercentEscapes]; | |
7915 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; | |
7916 | href = [href stringByCachingURLWithCurrentCDN]; | |
7917 | ||
7918 | NSURL *url([NSURL URLWithString:href]); | |
7919 | ||
7920 | NSStringEncoding encoding; | |
7921 | NSError *error(nil); | |
7922 | ||
7923 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
7924 | return [warning length] == 0 ? nil : warning; | |
7925 | return nil; | |
807ae6d7 | 7926 | } |
b4d89997 | 7927 | |
5829aea2 GP |
7928 | - (void) _endConnection:(NSURLConnection *)connection { |
7929 | // XXX: the memory management in this method is horribly awkward | |
7930 | ||
7931 | NSURLConnection **field = NULL; | |
7932 | if (connection == trivial_) | |
7933 | field = &trivial_; | |
7934 | else if (connection == trivial_bz2_) | |
7935 | field = &trivial_bz2_; | |
7936 | else if (connection == trivial_gz_) | |
7937 | field = &trivial_gz_; | |
7938 | _assert(field != NULL); | |
7939 | [connection release]; | |
7940 | *field = nil; | |
7941 | ||
7942 | if ( | |
7943 | trivial_ == nil && | |
7944 | trivial_bz2_ == nil && | |
7945 | trivial_gz_ == nil | |
7946 | ) { | |
9eae15b8 JF |
7947 | [delegate_ releaseNetworkActivityIndicator]; |
7948 | ||
7949 | [delegate_ removeProgressHUD:hud_]; | |
9eae15b8 JF |
7950 | hud_ = nil; |
7951 | ||
5829aea2 GP |
7952 | bool defer(false); |
7953 | ||
7954 | if (cydia_) { | |
7955 | if (NSString *warning = [self yieldToSelector:@selector(getWarning)]) { | |
7956 | defer = true; | |
7957 | ||
7958 | UIAlertView *alert = [[[UIAlertView alloc] | |
7959 | initWithTitle:UCLocalize("SOURCE_WARNING") | |
7960 | message:warning | |
7961 | delegate:self | |
7962 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
7963 | otherButtonTitles: |
7964 | UCLocalize("ADD_ANYWAY"), | |
7965 | nil | |
5829aea2 GP |
7966 | ] autorelease]; |
7967 | ||
7968 | [alert setContext:@"warning"]; | |
7969 | [alert setNumberOfRows:1]; | |
7970 | [alert show]; | |
7971 | } else | |
7972 | [self complete]; | |
7973 | } else if (error_ != nil) { | |
7974 | UIAlertView *alert = [[[UIAlertView alloc] | |
7975 | initWithTitle:UCLocalize("VERIFICATION_ERROR") | |
7976 | message:[error_ localizedDescription] | |
7977 | delegate:self | |
7978 | cancelButtonTitle:UCLocalize("OK") | |
7979 | otherButtonTitles:nil | |
7980 | ] autorelease]; | |
7981 | ||
7982 | [alert setContext:@"urlerror"]; | |
7983 | [alert show]; | |
7984 | } else { | |
7985 | UIAlertView *alert = [[[UIAlertView alloc] | |
7986 | initWithTitle:UCLocalize("NOT_REPOSITORY") | |
7987 | message:UCLocalize("NOT_REPOSITORY_EX") | |
7988 | delegate:self | |
7989 | cancelButtonTitle:UCLocalize("OK") | |
7990 | otherButtonTitles:nil | |
7991 | ] autorelease]; | |
7992 | ||
7993 | [alert setContext:@"trivial"]; | |
7994 | [alert show]; | |
7995 | } | |
7996 | ||
7b33d201 JF |
7997 | href_ = nil; |
7998 | error_ = nil; | |
5829aea2 | 7999 | } |
807ae6d7 | 8000 | } |
8fe19fc1 | 8001 | |
5829aea2 GP |
8002 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { |
8003 | switch ([response statusCode]) { | |
8004 | case 200: | |
8005 | cydia_ = YES; | |
8006 | } | |
8e05f686 RP |
8007 | } |
8008 | ||
5829aea2 GP |
8009 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { |
8010 | lprintf("connection:\"%s\" didFailWithError:\"%s\"", [href_ UTF8String], [[error localizedDescription] UTF8String]); | |
7b33d201 | 8011 | error_ = error; |
5829aea2 | 8012 | [self _endConnection:connection]; |
807ae6d7 | 8013 | } |
b4d89997 | 8014 | |
5829aea2 GP |
8015 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
8016 | [self _endConnection:connection]; | |
8017 | } | |
b4d89997 | 8018 | |
5829aea2 GP |
8019 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
8020 | NSMutableURLRequest *request = [NSMutableURLRequest | |
8021 | requestWithURL:[NSURL URLWithString:href] | |
8022 | cachePolicy:NSURLRequestUseProtocolCachePolicy | |
8023 | timeoutInterval:120.0 | |
8024 | ]; | |
bc11cf5b | 8025 | |
5829aea2 | 8026 | [request setHTTPMethod:method]; |
b4d89997 | 8027 | |
5829aea2 GP |
8028 | if (Machine_ != NULL) |
8029 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
8030 | if (UniqueID_ != nil) | |
8031 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
b4d89997 | 8032 | |
5829aea2 | 8033 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
3178d79b JF |
8034 | } |
8035 | ||
6840bff3 | 8036 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
5829aea2 | 8037 | NSString *context([alert context]); |
dc5812ec | 8038 | |
5829aea2 GP |
8039 | if ([context isEqualToString:@"source"]) { |
8040 | switch (button) { | |
8041 | case 1: { | |
8042 | NSString *href = [[alert textField] text]; | |
baf80942 | 8043 | |
5829aea2 | 8044 | //installer_ = [[self _requestHRef:href method:@"GET"] retain]; |
f6e13561 | 8045 | |
5829aea2 GP |
8046 | if (![href hasSuffix:@"/"]) |
8047 | href_ = [href stringByAppendingString:@"/"]; | |
8048 | else | |
8049 | href_ = href; | |
b4d89997 | 8050 | |
5829aea2 GP |
8051 | trivial_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages"] method:@"HEAD"] retain]; |
8052 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; | |
8053 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
8054 | //trivial_bz2_ = [[self _requestHRef:[href stringByAppendingString:@"dists/Release"] method:@"HEAD"] retain]; | |
b4d89997 | 8055 | |
5829aea2 | 8056 | cydia_ = false; |
8fe19fc1 | 8057 | |
5829aea2 | 8058 | // XXX: this is stupid |
7b33d201 | 8059 | hud_ = [delegate_ addProgressHUD]; |
5829aea2 | 8060 | [hud_ setText:UCLocalize("VERIFYING_URL")]; |
dcaecde2 | 8061 | [delegate_ retainNetworkActivityIndicator]; |
5829aea2 | 8062 | } break; |
770f2a8e | 8063 | |
5829aea2 GP |
8064 | case 0: |
8065 | break; | |
bc11cf5b | 8066 | |
5829aea2 GP |
8067 | _nodefault |
8068 | } | |
770f2a8e | 8069 | |
5829aea2 GP |
8070 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8071 | } else if ([context isEqualToString:@"trivial"]) | |
8072 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8073 | else if ([context isEqualToString:@"urlerror"]) | |
8074 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8075 | else if ([context isEqualToString:@"warning"]) { | |
8076 | switch (button) { | |
8077 | case 1: | |
8078 | [self complete]; | |
8079 | break; | |
770f2a8e | 8080 | |
5829aea2 GP |
8081 | case 0: |
8082 | break; | |
b5e7eebb | 8083 | |
5829aea2 GP |
8084 | _nodefault |
8085 | } | |
770f2a8e | 8086 | |
5829aea2 | 8087 | href_ = nil; |
b5e7eebb | 8088 | |
5829aea2 GP |
8089 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8090 | } | |
8091 | } | |
770f2a8e | 8092 | |
fe8e721f GP |
8093 | - (void) loadView { |
8094 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
770f2a8e | 8095 | |
7b33d201 | 8096 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; |
fe8e721f GP |
8097 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8098 | [list_ setRowHeight:56]; | |
7b33d201 | 8099 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
8100 | [list_ setDelegate:self]; |
8101 | [[self view] addSubview:list_]; | |
8102 | } | |
770f2a8e | 8103 | |
fe8e721f | 8104 | - (void) viewDidLoad { |
7d887d0b JF |
8105 | [super viewDidLoad]; |
8106 | ||
fe8e721f GP |
8107 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; |
8108 | [self updateButtonsForEditingStatus:NO animated:NO]; | |
8109 | } | |
770f2a8e | 8110 | |
fe8e721f | 8111 | - (void) releaseSubviews { |
fe8e721f GP |
8112 | list_ = nil; |
8113 | } | |
770f2a8e | 8114 | |
fe8e721f GP |
8115 | - (id) initWithDatabase:(Database *)database { |
8116 | if ((self = [super init]) != nil) { | |
8117 | database_ = database; | |
7b33d201 | 8118 | sources_ = [NSMutableArray arrayWithCapacity:16]; |
807ae6d7 JF |
8119 | } return self; |
8120 | } | |
770f2a8e | 8121 | |
807ae6d7 | 8122 | - (void) reloadData { |
fe8e721f GP |
8123 | [super reloadData]; |
8124 | ||
5829aea2 | 8125 | pkgSourceList list; |
53ca7fdd | 8126 | if ([database_ popErrorWithTitle:UCLocalize("SOURCES") forOperation:list.ReadMainList()]) |
5829aea2 GP |
8127 | return; |
8128 | ||
8129 | [sources_ removeAllObjects]; | |
8130 | [sources_ addObjectsFromArray:[database_ sources]]; | |
8131 | _trace(); | |
8132 | [sources_ sortUsingSelector:@selector(compareByNameAndType:)]; | |
8133 | _trace(); | |
8134 | ||
8135 | int count([sources_ count]); | |
8136 | offset_ = 0; | |
8137 | for (int i = 0; i != count; i++) { | |
8138 | if ([[sources_ objectAtIndex:i] record] == nil) | |
8139 | break; | |
8140 | offset_++; | |
770f2a8e | 8141 | } |
807ae6d7 | 8142 | |
5829aea2 GP |
8143 | [list_ setEditing:NO]; |
8144 | [self updateButtonsForEditingStatus:NO animated:NO]; | |
8145 | [list_ reloadData]; | |
770f2a8e JF |
8146 | } |
8147 | ||
5829aea2 GP |
8148 | - (void) showAddSourcePrompt { |
8149 | UIAlertView *alert = [[[UIAlertView alloc] | |
8150 | initWithTitle:UCLocalize("ENTER_APT_URL") | |
8151 | message:nil | |
8152 | delegate:self | |
8153 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8154 | otherButtonTitles: |
8155 | UCLocalize("ADD_SOURCE"), | |
8156 | nil | |
5829aea2 | 8157 | ] autorelease]; |
770f2a8e | 8158 | |
5829aea2 | 8159 | [alert setContext:@"source"]; |
770f2a8e | 8160 | |
5829aea2 GP |
8161 | [alert setNumberOfRows:1]; |
8162 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
770f2a8e | 8163 | |
5829aea2 GP |
8164 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
8165 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
8166 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
8167 | [traits setKeyboardType:UIKeyboardTypeURL]; | |
8168 | // XXX: UIReturnKeyDone | |
8169 | [traits setReturnKeyType:UIReturnKeyNext]; | |
770f2a8e | 8170 | |
5829aea2 | 8171 | [alert show]; |
770f2a8e JF |
8172 | } |
8173 | ||
5829aea2 GP |
8174 | - (void) addButtonClicked { |
8175 | [self showAddSourcePrompt]; | |
770f2a8e JF |
8176 | } |
8177 | ||
5829aea2 GP |
8178 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated { |
8179 | [[self navigationItem] setLeftBarButtonItem:(editing ? [[[UIBarButtonItem alloc] | |
8180 | initWithTitle:UCLocalize("ADD") | |
8181 | style:UIBarButtonItemStylePlain | |
8182 | target:self | |
8183 | action:@selector(addButtonClicked) | |
8184 | ] autorelease] : [[self navigationItem] backBarButtonItem]) animated:animated]; | |
8185 | ||
8186 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
8187 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
8188 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
8189 | target:self | |
8190 | action:@selector(editButtonClicked) | |
8191 | ] autorelease] animated:animated]; | |
8192 | ||
8193 | if (IsWildcat_ && !editing) | |
8194 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8195 | initWithTitle:UCLocalize("SETTINGS") | |
8196 | style:UIBarButtonItemStylePlain | |
8197 | target:self | |
8198 | action:@selector(settingsButtonClicked) | |
8199 | ] autorelease]]; | |
770f2a8e JF |
8200 | } |
8201 | ||
5829aea2 GP |
8202 | - (void) settingsButtonClicked { |
8203 | [delegate_ showSettings]; | |
8204 | } | |
8205 | ||
8206 | - (void) editButtonClicked { | |
8207 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
8208 | ||
8209 | [self updateButtonsForEditingStatus:[list_ isEditing] animated:YES]; | |
770f2a8e JF |
8210 | } |
8211 | ||
21c6da4b GP |
8212 | @end |
8213 | /* }}} */ | |
f3e11d24 | 8214 | |
5829aea2 | 8215 | /* Settings Controller {{{ */ |
cd79e8cf | 8216 | @interface SettingsController : CyteViewController < |
c21004b9 JF |
8217 | UITableViewDataSource, |
8218 | UITableViewDelegate | |
8219 | > { | |
21c6da4b | 8220 | _transient Database *database_; |
3931b718 JF |
8221 | // XXX: ok, "roledelegate_"?... |
8222 | _transient id roledelegate_; | |
bf7c998c | 8223 | _H<UITableView, 2> table_; |
7b33d201 JF |
8224 | _H<UISegmentedControl> segment_; |
8225 | _H<UIView> container_; | |
21c6da4b | 8226 | } |
c21004b9 JF |
8227 | |
8228 | - (void) showDoneButton; | |
8229 | - (void) resizeSegmentedControl; | |
8230 | ||
21c6da4b GP |
8231 | @end |
8232 | ||
5829aea2 | 8233 | @implementation SettingsController |
fe8e721f | 8234 | |
fe8e721f GP |
8235 | - (void) loadView { |
8236 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
8237 | ||
7b33d201 | 8238 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f GP |
8239 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8240 | [table_ setDelegate:self]; | |
7b33d201 | 8241 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f GP |
8242 | [[self view] addSubview:table_]; |
8243 | ||
8244 | NSArray *items = [NSArray arrayWithObjects: | |
8245 | UCLocalize("USER"), | |
8246 | UCLocalize("HACKER"), | |
8247 | UCLocalize("DEVELOPER"), | |
8248 | nil]; | |
7b33d201 JF |
8249 | segment_ = [[[UISegmentedControl alloc] initWithItems:items] autorelease]; |
8250 | container_ = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, 44.0f)] autorelease]; | |
fe8e721f GP |
8251 | [container_ addSubview:segment_]; |
8252 | } | |
8253 | ||
8254 | - (void) viewDidLoad { | |
7d887d0b JF |
8255 | [super viewDidLoad]; |
8256 | ||
fe8e721f GP |
8257 | [[self navigationItem] setTitle:UCLocalize("WHO_ARE_YOU")]; |
8258 | ||
8259 | int index = -1; | |
8260 | if ([Role_ isEqualToString:@"User"]) index = 0; | |
8261 | if ([Role_ isEqualToString:@"Hacker"]) index = 1; | |
8262 | if ([Role_ isEqualToString:@"Developer"]) index = 2; | |
8263 | if (index != -1) { | |
8264 | [segment_ setSelectedSegmentIndex:index]; | |
8265 | [self showDoneButton]; | |
8266 | } | |
8267 | ||
8268 | [segment_ addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged]; | |
8269 | [self resizeSegmentedControl]; | |
8270 | } | |
8271 | ||
8272 | - (void) releaseSubviews { | |
fe8e721f | 8273 | table_ = nil; |
fe8e721f | 8274 | segment_ = nil; |
fe8e721f | 8275 | container_ = nil; |
21c6da4b GP |
8276 | } |
8277 | ||
8278 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { | |
6840bff3 | 8279 | if ((self = [super init]) != nil) { |
21c6da4b GP |
8280 | database_ = database; |
8281 | roledelegate_ = delegate; | |
21c6da4b GP |
8282 | } return self; |
8283 | } | |
8284 | ||
e62f29c6 GP |
8285 | - (void) resizeSegmentedControl { |
8286 | CGFloat width = [[self view] frame].size.width; | |
8287 | [segment_ setFrame:CGRectMake(width / 32.0f, 0, width - (width / 32.0f * 2.0f), 44.0f)]; | |
8288 | } | |
8289 | ||
2bdd73bd GP |
8290 | - (void) viewWillAppear:(BOOL)animated { |
8291 | [super viewWillAppear:animated]; | |
bc11cf5b | 8292 | |
e62f29c6 GP |
8293 | [self resizeSegmentedControl]; |
8294 | } | |
8295 | ||
8296 | - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { | |
8297 | [self resizeSegmentedControl]; | |
8298 | } | |
8299 | ||
8300 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { | |
8301 | [self resizeSegmentedControl]; | |
2bdd73bd GP |
8302 | } |
8303 | ||
21c6da4b | 8304 | - (void) save { |
fed0d010 | 8305 | NSString *role(nil); |
bc11cf5b | 8306 | |
21c6da4b | 8307 | switch ([segment_ selectedSegmentIndex]) { |
124c6201 GP |
8308 | case 0: role = @"User"; break; |
8309 | case 1: role = @"Hacker"; break; | |
8310 | case 2: role = @"Developer"; break; | |
21c6da4b GP |
8311 | |
8312 | _nodefault | |
8313 | } | |
8314 | ||
124c6201 | 8315 | if (![role isEqualToString:Role_]) { |
fed0d010 | 8316 | bool rolling(Role_ == nil); |
124c6201 | 8317 | Role_ = role; |
bc11cf5b | 8318 | |
124c6201 GP |
8319 | Settings_ = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
8320 | Role_, @"Role", | |
8321 | nil]; | |
21c6da4b | 8322 | |
124c6201 | 8323 | [Metadata_ setObject:Settings_ forKey:@"Settings"]; |
124c6201 | 8324 | Changed_ = true; |
bc11cf5b | 8325 | |
fed0d010 JF |
8326 | if (rolling) |
8327 | [roledelegate_ loadData]; | |
8328 | else | |
8329 | [roledelegate_ updateData]; | |
124c6201 | 8330 | } |
21c6da4b GP |
8331 | } |
8332 | ||
8333 | - (void) segmentChanged:(UISegmentedControl *)control { | |
8334 | [self showDoneButton]; | |
8335 | } | |
8336 | ||
8576ab50 | 8337 | - (void) saveAndClose { |
21c6da4b | 8338 | [self save]; |
8576ab50 GP |
8339 | |
8340 | [[self navigationItem] setRightBarButtonItem:nil]; | |
21c6da4b GP |
8341 | [[self navigationController] dismissModalViewControllerAnimated:YES]; |
8342 | } | |
8343 | ||
8576ab50 GP |
8344 | - (void) doneButtonClicked { |
8345 | UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)] autorelease]; | |
8346 | [spinner startAnimating]; | |
8347 | UIBarButtonItem *spinItem = [[[UIBarButtonItem alloc] initWithCustomView:spinner] autorelease]; | |
8348 | [[self navigationItem] setRightBarButtonItem:spinItem]; | |
8349 | ||
8350 | [self performSelector:@selector(saveAndClose) withObject:nil afterDelay:0]; | |
8351 | } | |
8352 | ||
21c6da4b | 8353 | - (void) showDoneButton { |
dd9de556 | 8354 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
21c6da4b GP |
8355 | initWithTitle:UCLocalize("DONE") |
8356 | style:UIBarButtonItemStyleDone | |
8357 | target:self | |
8358 | action:@selector(doneButtonClicked) | |
dd9de556 | 8359 | ] autorelease] animated:([[self navigationItem] rightBarButtonItem] == nil)]; |
21c6da4b GP |
8360 | } |
8361 | ||
8362 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { | |
e62f29c6 GP |
8363 | // XXX: For not having a single cell in the table, this sure is a lot of sections. |
8364 | return 6; | |
21c6da4b GP |
8365 | } |
8366 | ||
8367 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
8368 | return 0; // :( | |
8369 | } | |
8370 | ||
6840bff3 | 8371 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
21c6da4b GP |
8372 | return nil; // This method is required by the protocol. |
8373 | } | |
8374 | ||
8375 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { | |
bc11cf5b | 8376 | if (section == 1) |
21c6da4b | 8377 | return UCLocalize("ROLE_EX"); |
bc11cf5b | 8378 | if (section == 4) |
21c6da4b GP |
8379 | return [NSString stringWithFormat: |
8380 | @"%@: %@\n%@: %@\n%@: %@", | |
bc11cf5b JF |
8381 | UCLocalize("USER"), UCLocalize("USER_EX"), |
8382 | UCLocalize("HACKER"), UCLocalize("HACKER_EX"), | |
21c6da4b GP |
8383 | UCLocalize("DEVELOPER"), UCLocalize("DEVELOPER_EX") |
8384 | ]; | |
8385 | else return nil; | |
8386 | } | |
8387 | ||
8388 | - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { | |
36fbb2aa | 8389 | return section == 3 ? 44.0f : 0; |
21c6da4b GP |
8390 | } |
8391 | ||
8392 | - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { | |
36fbb2aa | 8393 | return section == 3 ? container_ : nil; |
21c6da4b GP |
8394 | } |
8395 | ||
fe8e721f GP |
8396 | - (void) reloadData { |
8397 | [super reloadData]; | |
9dac415b | 8398 | |
fe8e721f GP |
8399 | [table_ reloadData]; |
8400 | } | |
8401 | ||
f3e11d24 GP |
8402 | @end |
8403 | /* }}} */ | |
8404 | /* Stash Controller {{{ */ | |
cd79e8cf | 8405 | @interface StashController : CyteViewController { |
7b33d201 JF |
8406 | _H<UIActivityIndicatorView> spinner_; |
8407 | _H<UILabel> status_; | |
8408 | _H<UILabel> caption_; | |
f3e11d24 | 8409 | } |
6840bff3 | 8410 | |
f3e11d24 GP |
8411 | @end |
8412 | ||
5829aea2 | 8413 | @implementation StashController |
f3e11d24 | 8414 | |
fe8e721f GP |
8415 | - (void) loadView { |
8416 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
8417 | [[self view] setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; | |
8418 | ||
7b33d201 | 8419 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
fe8e721f GP |
8420 | CGRect spinrect = [spinner_ frame]; |
8421 | spinrect.origin.x = ([[self view] frame].size.width / 2) - (spinrect.size.width / 2); | |
8422 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; | |
8423 | [spinner_ setFrame:spinrect]; | |
8424 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
8425 | [[self view] addSubview:spinner_]; | |
8426 | [spinner_ startAnimating]; | |
8427 | ||
8428 | CGRect captrect; | |
8429 | captrect.size.width = [[self view] frame].size.width; | |
8430 | captrect.size.height = 40.0f; | |
8431 | captrect.origin.x = 0; | |
8432 | captrect.origin.y = ([[self view] frame].size.height / 2) - (captrect.size.height * 2); | |
7b33d201 | 8433 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
fe8e721f GP |
8434 | [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; |
8435 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
8436 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; | |
8437 | [caption_ setTextColor:[UIColor whiteColor]]; | |
8438 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
8439 | [caption_ setShadowColor:[UIColor blackColor]]; | |
8440 | [caption_ setTextAlignment:UITextAlignmentCenter]; | |
8441 | [[self view] addSubview:caption_]; | |
8442 | ||
8443 | CGRect statusrect; | |
8444 | statusrect.size.width = [[self view] frame].size.width; | |
8445 | statusrect.size.height = 30.0f; | |
8446 | statusrect.origin.x = 0; | |
8447 | statusrect.origin.y = ([[self view] frame].size.height / 2) - statusrect.size.height; | |
7b33d201 | 8448 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
fe8e721f GP |
8449 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
8450 | [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; | |
8451 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; | |
8452 | [status_ setTextColor:[UIColor whiteColor]]; | |
8453 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
8454 | [status_ setShadowColor:[UIColor blackColor]]; | |
8455 | [status_ setTextAlignment:UITextAlignmentCenter]; | |
8456 | [[self view] addSubview:status_]; | |
8457 | } | |
8458 | ||
67dd58c7 JF |
8459 | - (void) releaseSubviews { |
8460 | spinner_ = nil; | |
8461 | status_ = nil; | |
8462 | caption_ = nil; | |
8463 | } | |
8464 | ||
770f2a8e | 8465 | @end |
807ae6d7 | 8466 | /* }}} */ |
770f2a8e | 8467 | |
a06e9179 JF |
8468 | @interface CYURLCache : SDURLCache { |
8469 | } | |
8470 | ||
8471 | @end | |
8472 | ||
8473 | @implementation CYURLCache | |
8474 | ||
8475 | - (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request { | |
8476 | #if !ForRelease | |
8477 | if (false); | |
8478 | else if ([event isEqualToString:@"no-cache"]) | |
8479 | event = @"!!!"; | |
8480 | else if ([event isEqualToString:@"store"]) | |
8481 | event = @">>>"; | |
8482 | else if ([event isEqualToString:@"invalid"]) | |
8483 | event = @"???"; | |
8484 | else if ([event isEqualToString:@"memory"]) | |
8485 | event = @"mem"; | |
8486 | else if ([event isEqualToString:@"disk"]) | |
8487 | event = @"ssd"; | |
8488 | else if ([event isEqualToString:@"miss"]) | |
8489 | event = @"---"; | |
8490 | ||
8491 | NSLog(@"%@: %@", event, [[request URL] absoluteString]); | |
8492 | #endif | |
8493 | } | |
8494 | ||
5e845121 JF |
8495 | - (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request { |
8496 | if (NSURLResponse *response = [cached response]) | |
8497 | if (NSString *mime = [response MIMEType]) | |
8498 | if ([mime isEqualToString:@"text/cache-manifest"]) { | |
8499 | NSURL *url([response URL]); | |
8500 | ||
8501 | #if !ForRelease | |
8502 | NSLog(@"###: %@", [url absoluteString]); | |
8503 | #endif | |
8504 | ||
8505 | @synchronized (HostConfig_) { | |
8506 | [CachedURLs_ addObject:url]; | |
8507 | } | |
8508 | } | |
8509 | ||
8510 | [super storeCachedResponse:cached forRequest:request]; | |
8511 | } | |
8512 | ||
a06e9179 JF |
8513 | @end |
8514 | ||
2367a917 | 8515 | @interface Cydia : UIApplication < |
adb61bda | 8516 | ConfirmationControllerDelegate, |
6915b806 | 8517 | DatabaseDelegate, |
eb30da80 | 8518 | CydiaDelegate, |
68046ccc JF |
8519 | UINavigationControllerDelegate, |
8520 | UITabBarControllerDelegate | |
2367a917 | 8521 | > { |
7b33d201 JF |
8522 | _H<UIWindow> window_; |
8523 | _H<CYTabBarController> tabbar_; | |
70750ab3 | 8524 | _H<CydiaLoadingViewController> emulated_; |
3931b718 | 8525 | |
7b33d201 JF |
8526 | _H<NSMutableArray> essential_; |
8527 | _H<NSMutableArray> broken_; | |
dc5812ec JF |
8528 | |
8529 | Database *database_; | |
dc5812ec | 8530 | |
7b33d201 | 8531 | _H<NSURL> starturl_; |
54043703 | 8532 | |
235f5487 | 8533 | unsigned locked_; |
54043703 | 8534 | unsigned activity_; |
9b619239 | 8535 | |
7b33d201 | 8536 | _H<StashController> stash_; |
f3e11d24 | 8537 | |
8c02abc8 | 8538 | bool loaded_; |
dc5812ec JF |
8539 | } |
8540 | ||
fed0d010 | 8541 | - (void) loadData; |
670a0494 | 8542 | |
dc5812ec JF |
8543 | @end |
8544 | ||
8545 | @implementation Cydia | |
8546 | ||
b5e7eebb | 8547 | - (void) beginUpdate { |
7585ce66 | 8548 | [tabbar_ beginUpdate]; |
b5e7eebb GP |
8549 | } |
8550 | ||
8551 | - (BOOL) updating { | |
7585ce66 | 8552 | return [tabbar_ updating]; |
b5e7eebb GP |
8553 | } |
8554 | ||
9bedffaa JF |
8555 | - (void) _loaded { |
8556 | if ([broken_ count] != 0) { | |
8557 | int count = [broken_ count]; | |
8558 | ||
37d2b2a9 | 8559 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8560 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
b5e7eebb GP |
8561 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
8562 | delegate:self | |
8563 | cancelButtonTitle:UCLocalize("FORCIBLY_CLEAR") | |
1aa29546 JF |
8564 | otherButtonTitles: |
8565 | UCLocalize("TEMPORARY_IGNORE"), | |
8566 | nil | |
9bedffaa JF |
8567 | ] autorelease]; |
8568 | ||
37d2b2a9 | 8569 | [alert setContext:@"fixhalf"]; |
59befad5 | 8570 | [alert setNumberOfRows:2]; |
37d2b2a9 | 8571 | [alert show]; |
9bedffaa JF |
8572 | } else if (!Ignored_ && [essential_ count] != 0) { |
8573 | int count = [essential_ count]; | |
8574 | ||
37d2b2a9 | 8575 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8576 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
b5e7eebb GP |
8577 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
8578 | delegate:self | |
8579 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
1aa29546 JF |
8580 | otherButtonTitles: |
8581 | UCLocalize("UPGRADE_ESSENTIAL"), | |
8582 | UCLocalize("COMPLETE_UPGRADE"), | |
8583 | nil | |
9bedffaa JF |
8584 | ] autorelease]; |
8585 | ||
37d2b2a9 GP |
8586 | [alert setContext:@"upgrade"]; |
8587 | [alert show]; | |
9bedffaa JF |
8588 | } |
8589 | } | |
8590 | ||
7623f855 | 8591 | - (void) _saveConfig { |
ffbb3bd5 JF |
8592 | _trace(); |
8593 | MetaFile_.Sync(); | |
8594 | _trace(); | |
8595 | ||
7623f855 | 8596 | if (Changed_) { |
7623f855 | 8597 | NSString *error(nil); |
ffbb3bd5 | 8598 | |
7623f855 JF |
8599 | if (NSData *data = [NSPropertyListSerialization dataFromPropertyList:Metadata_ format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error]) { |
8600 | _trace(); | |
8601 | NSError *error(nil); | |
8602 | if (![data writeToFile:@"/var/lib/cydia/metadata.plist" options:NSAtomicWrite error:&error]) | |
8603 | NSLog(@"failure to save metadata data: %@", error); | |
8604 | _trace(); | |
ffbb3bd5 JF |
8605 | |
8606 | Changed_ = false; | |
7623f855 JF |
8607 | } else { |
8608 | NSLog(@"failure to serialize metadata: %@", error); | |
7623f855 | 8609 | } |
7623f855 JF |
8610 | } |
8611 | } | |
8612 | ||
89571a5b | 8613 | // Navigation controller for the queuing badge. |
15f0d613 | 8614 | - (UINavigationController *) queueNavigationController { |
89571a5b GP |
8615 | NSArray *controllers = [tabbar_ viewControllers]; |
8616 | return [controllers objectAtIndex:3]; | |
8617 | } | |
8618 | ||
302bf91c JF |
8619 | - (void) unloadData { |
8620 | [tabbar_ unloadData]; | |
8621 | } | |
8622 | ||
7623f855 JF |
8623 | - (void) _updateData { |
8624 | [self _saveConfig]; | |
8625 | ||
302bf91c | 8626 | [self unloadData]; |
f6371a33 | 8627 | |
15f0d613 | 8628 | UINavigationController *navigation = [self queueNavigationController]; |
b5e7eebb | 8629 | |
4305896c | 8630 | id queuedelegate = nil; |
89571a5b GP |
8631 | if ([[navigation viewControllers] count] > 0) |
8632 | queuedelegate = [[navigation viewControllers] objectAtIndex:0]; | |
bc11cf5b | 8633 | |
89571a5b GP |
8634 | [queuedelegate queueStatusDidChange]; |
8635 | [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; | |
7623f855 JF |
8636 | } |
8637 | ||
53fb38da | 8638 | - (void) _refreshIfPossible:(NSDate *)update { |
8c02abc8 | 8639 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
bc11cf5b | 8640 | |
45e21ffa | 8641 | bool recently = false; |
45e21ffa GP |
8642 | if (update != nil) { |
8643 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
8644 | if (interval <= 0 && interval > -(15*60)) | |
8645 | recently = true; | |
8646 | } | |
8647 | ||
8648 | // Don't automatic refresh if: | |
8649 | // - We already refreshed recently. | |
8650 | // - We already auto-refreshed this launch. | |
8651 | // - Auto-refresh is disabled. | |
8652 | if (recently || loaded_ || ManualRefresh) { | |
35f0a3b5 | 8653 | // If we are cancelling, we need to make sure it knows it's already loaded. |
45e21ffa | 8654 | loaded_ = true; |
d13edf44 JF |
8655 | |
8656 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
45e21ffa GP |
8657 | } else { |
8658 | // We are going to load, so remember that. | |
8659 | loaded_ = true; | |
45e21ffa | 8660 | |
d13edf44 JF |
8661 | SCNetworkReachabilityFlags flags; { |
8662 | SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(NULL, "cydia.saurik.com")); | |
8663 | SCNetworkReachabilityGetFlags(reachability, &flags); | |
8664 | CFRelease(reachability); | |
8665 | } | |
afb5333a | 8666 | |
d13edf44 JF |
8667 | // XXX: this elaborate mess is what Apple is using to determine this? :( |
8668 | // XXX: do we care if the user has to intervene? maybe that's ok? | |
8669 | bool reachable( | |
8670 | (flags & kSCNetworkReachabilityFlagsReachable) != 0 && ( | |
8671 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || ( | |
8672 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || | |
8673 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0 | |
8674 | ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 || | |
8675 | (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0 | |
8676 | ) | |
8677 | ); | |
bc11cf5b | 8678 | |
d13edf44 JF |
8679 | // If we can reach the server, auto-refresh! |
8680 | if (reachable) | |
8681 | [tabbar_ performSelectorOnMainThread:@selector(setUpdate:) withObject:update waitUntilDone:NO]; | |
8682 | } | |
9aecdc9c | 8683 | |
8c02abc8 | 8684 | [pool release]; |
9aecdc9c GP |
8685 | } |
8686 | ||
8687 | - (void) refreshIfPossible { | |
53fb38da | 8688 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible:) toTarget:self withObject:[Metadata_ objectForKey:@"LastUpdate"]]; |
9aecdc9c GP |
8689 | } |
8690 | ||
e09e1589 JF |
8691 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
8692 | @synchronized (self) { | |
851f4a99 GP |
8693 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
8694 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
dc5812ec | 8695 | |
4ba8f30a | 8696 | [database_ yieldToSelector:@selector(reloadDataWithInvocation:) withObject:invocation]; |
d061f4ba | 8697 | |
36fbb2aa JF |
8698 | if (hud != nil) |
8699 | [self removeProgressHUD:hud]; | |
c25a610d | 8700 | |
36bb2ca2 | 8701 | size_t changes(0); |
9bedffaa | 8702 | |
a54b1c10 | 8703 | [essential_ removeAllObjects]; |
9bedffaa | 8704 | [broken_ removeAllObjects]; |
b4d89997 | 8705 | |
670a0494 | 8706 | NSArray *packages([database_ packages]); |
affeffc7 | 8707 | for (Package *package in packages) { |
9bedffaa JF |
8708 | if ([package half]) |
8709 | [broken_ addObject:package]; | |
31f3cfff | 8710 | if ([package upgradableAndEssential:NO]) { |
a54b1c10 JF |
8711 | if ([package essential]) |
8712 | [essential_ addObject:package]; | |
36bb2ca2 | 8713 | ++changes; |
a54b1c10 | 8714 | } |
36bb2ca2 | 8715 | } |
b4d89997 | 8716 | |
89571a5b | 8717 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; |
36bb2ca2 | 8718 | if (changes != 0) { |
0e1784b4 | 8719 | _trace(); |
36bb2ca2 | 8720 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); |
45e21ffa | 8721 | [changesItem setBadgeValue:badge]; |
65a03a7a | 8722 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
0e1784b4 | 8723 | [self setApplicationIconBadgeNumber:changes]; |
c25a610d | 8724 | } else { |
0e1784b4 | 8725 | _trace(); |
45e21ffa GP |
8726 | [changesItem setBadgeValue:nil]; |
8727 | [changesItem setAnimatedBadge:NO]; | |
0e1784b4 | 8728 | [self setApplicationIconBadgeNumber:0]; |
c25a610d | 8729 | } |
b4d89997 | 8730 | |
7623f855 | 8731 | [self _updateData]; |
6d9712c4 | 8732 | |
8c02abc8 | 8733 | [self refreshIfPossible]; |
e09e1589 | 8734 | } } |
6d9712c4 | 8735 | |
7b0ce2da | 8736 | - (void) updateData { |
7623f855 | 8737 | [self _updateData]; |
b4d89997 JF |
8738 | } |
8739 | ||
7b0ce2da JF |
8740 | - (void) update_ { |
8741 | [database_ update]; | |
fca2f596 | 8742 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
7b0ce2da JF |
8743 | } |
8744 | ||
2e9123cb JF |
8745 | - (void) disemulate { |
8746 | if (emulated_ == nil) | |
8747 | return; | |
8748 | ||
8749 | [window_ addSubview:[tabbar_ view]]; | |
8750 | [[emulated_ view] removeFromSuperview]; | |
2e9123cb JF |
8751 | emulated_ = nil; |
8752 | [window_ setUserInteractionEnabled:YES]; | |
8753 | } | |
8754 | ||
8755 | - (void) presentModalViewController:(UIViewController *)controller force:(BOOL)force { | |
15f0d613 | 8756 | UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:controller] autorelease]); |
6915b806 JF |
8757 | if (IsWildcat_) |
8758 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
2e9123cb JF |
8759 | |
8760 | UIViewController *parent; | |
8761 | if (emulated_ == nil) | |
8762 | parent = tabbar_; | |
8763 | else if (!force) | |
8764 | parent = emulated_; | |
8765 | else { | |
8766 | [self disemulate]; | |
8767 | parent = tabbar_; | |
8768 | } | |
8769 | ||
8770 | [parent presentModalViewController:navigation animated:YES]; | |
6915b806 JF |
8771 | } |
8772 | ||
8773 | - (ProgressController *) invokeNewProgress:(NSInvocation *)invocation forController:(UINavigationController *)navigation withTitle:(NSString *)title { | |
8774 | ProgressController *progress([[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]); | |
8775 | ||
8776 | if (navigation != nil) | |
8777 | [navigation pushViewController:progress animated:YES]; | |
8778 | else | |
2e9123cb | 8779 | [self presentModalViewController:progress force:YES]; |
6915b806 JF |
8780 | |
8781 | [progress invoke:invocation withTitle:title]; | |
8782 | return progress; | |
8783 | } | |
8784 | ||
8785 | - (void) detachNewProgressSelector:(SEL)selector toTarget:(id)target forController:(UINavigationController *)navigation title:(NSString *)title { | |
8786 | [self invokeNewProgress:[NSInvocation invocationWithSelector:selector forTarget:target] forController:navigation withTitle:title]; | |
8787 | } | |
8788 | ||
8789 | - (void) repairWithInvocation:(NSInvocation *)invocation { | |
8790 | _trace(); | |
8d5bc2ad | 8791 | [self invokeNewProgress:invocation forController:nil withTitle:@"REPAIRING"]; |
6915b806 JF |
8792 | _trace(); |
8793 | } | |
8794 | ||
8795 | - (void) repairWithSelector:(SEL)selector { | |
8796 | [self performSelectorOnMainThread:@selector(repairWithInvocation:) withObject:[NSInvocation invocationWithSelector:selector forTarget:database_] waitUntilDone:YES]; | |
8797 | } | |
8798 | ||
e09e1589 JF |
8799 | - (void) reloadData { |
8800 | [self reloadDataWithInvocation:nil]; | |
8801 | } | |
8802 | ||
7b0ce2da | 8803 | - (void) syncData { |
1fe5dc43 JF |
8804 | [self _saveConfig]; |
8805 | ||
670a0494 | 8806 | FILE *file(fopen("/etc/apt/sources.list.d/cydia.list", "w")); |
7b0ce2da JF |
8807 | _assert(file != NULL); |
8808 | ||
670a0494 JF |
8809 | for (NSString *key in [Sources_ allKeys]) { |
8810 | NSDictionary *source([Sources_ objectForKey:key]); | |
7b0ce2da JF |
8811 | |
8812 | fprintf(file, "%s %s %s\n", | |
8813 | [[source objectForKey:@"Type"] UTF8String], | |
8814 | [[source objectForKey:@"URI"] UTF8String], | |
8815 | [[source objectForKey:@"Distribution"] UTF8String] | |
8816 | ); | |
8817 | } | |
8818 | ||
8819 | fclose(file); | |
8820 | ||
8d5bc2ad | 8821 | [self detachNewProgressSelector:@selector(update_) toTarget:self forController:nil title:@"UPDATING_SOURCES"]; |
7b0ce2da JF |
8822 | } |
8823 | ||
93460555 JF |
8824 | - (void) addTrivialSource:(NSString *)href { |
8825 | [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
8826 | @"deb", @"Type", | |
8827 | href, @"URI", | |
8828 | @"./", @"Distribution", | |
8829 | nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href]]; | |
516d1d40 JF |
8830 | |
8831 | Changed_ = true; | |
93460555 JF |
8832 | } |
8833 | ||
b4d89997 JF |
8834 | - (void) resolve { |
8835 | pkgProblemResolver *resolver = [database_ resolver]; | |
8836 | ||
8837 | resolver->InstallProtect(); | |
8838 | if (!resolver->Resolve(true)) | |
8839 | _error->Discard(); | |
2367a917 JF |
8840 | } |
8841 | ||
670a0494 | 8842 | - (bool) perform { |
d6c371f5 JF |
8843 | // XXX: this is a really crappy way of doing this. |
8844 | // like, seriously: this state machine is still broken, and cancelling this here doesn't really /fix/ that. | |
8845 | // for one, the user can still /start/ a reloading data event while they have a queue, which is stupid | |
8846 | // for two, this just means there is a race condition between the refresh completing and the confirmation controller appearing. | |
8847 | if ([tabbar_ updating]) | |
8848 | [tabbar_ cancelUpdate]; | |
8849 | ||
670a0494 JF |
8850 | if (![database_ prepare]) |
8851 | return false; | |
49048579 | 8852 | |
adb61bda | 8853 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
affeffc7 | 8854 | [page setDelegate:self]; |
15f0d613 | 8855 | UINavigationController *confirm_([[[UINavigationController alloc] initWithRootViewController:page] autorelease]); |
49048579 | 8856 | |
36fbb2aa JF |
8857 | if (IsWildcat_) |
8858 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 8859 | [tabbar_ presentModalViewController:confirm_ animated:YES]; |
670a0494 JF |
8860 | |
8861 | return true; | |
3178d79b JF |
8862 | } |
8863 | ||
dc63e78f JF |
8864 | - (void) queue { |
8865 | @synchronized (self) { | |
8866 | [self perform]; | |
8867 | } | |
8868 | } | |
8869 | ||
8870 | - (void) clearPackage:(Package *)package { | |
8871 | @synchronized (self) { | |
8872 | [package clear]; | |
8873 | [self resolve]; | |
8874 | [self perform]; | |
8875 | } | |
8876 | } | |
8877 | ||
77801ff1 JF |
8878 | - (void) installPackages:(NSArray *)packages { |
8879 | @synchronized (self) { | |
8880 | for (Package *package in packages) | |
8881 | [package install]; | |
8882 | [self resolve]; | |
8883 | [self perform]; | |
8884 | } | |
8885 | } | |
8886 | ||
36bb2ca2 JF |
8887 | - (void) installPackage:(Package *)package { |
8888 | @synchronized (self) { | |
8889 | [package install]; | |
8890 | [self resolve]; | |
8891 | [self perform]; | |
8892 | } | |
8893 | } | |
8894 | ||
8895 | - (void) removePackage:(Package *)package { | |
8896 | @synchronized (self) { | |
8897 | [package remove]; | |
8898 | [self resolve]; | |
8899 | [self perform]; | |
8900 | } | |
8901 | } | |
8902 | ||
8903 | - (void) distUpgrade { | |
8904 | @synchronized (self) { | |
670a0494 JF |
8905 | if (![database_ upgrade]) |
8906 | return; | |
36bb2ca2 JF |
8907 | [self perform]; |
8908 | } | |
b4d89997 JF |
8909 | } |
8910 | ||
fca2f596 JF |
8911 | - (void) perform_ { |
8912 | [database_ perform]; | |
8913 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; | |
8914 | } | |
8915 | ||
b5e7eebb | 8916 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f6371a33 | 8917 | Queuing_ = false; |
235f5487 | 8918 | ++locked_; |
fca2f596 | 8919 | [self detachNewProgressSelector:@selector(perform_) toTarget:self forController:navigation title:@"RUNNING"]; |
235f5487 | 8920 | --locked_; |
dc5812ec JF |
8921 | } |
8922 | ||
21c6da4b | 8923 | - (void) showSettings { |
a54fed5b | 8924 | [self presentModalViewController:[[[SettingsController alloc] initWithDatabase:database_ delegate:self] autorelease] force:NO]; |
7b0ce2da JF |
8925 | } |
8926 | ||
54043703 JF |
8927 | - (void) retainNetworkActivityIndicator { |
8928 | if (activity_++ == 0) | |
8929 | [self setNetworkActivityIndicatorVisible:YES]; | |
848ed88b JF |
8930 | |
8931 | #if TraceLogging | |
8932 | NSLog(@"retainNetworkActivityIndicator->%d", activity_); | |
8933 | #endif | |
54043703 JF |
8934 | } |
8935 | ||
8936 | - (void) releaseNetworkActivityIndicator { | |
8937 | if (--activity_ == 0) | |
8938 | [self setNetworkActivityIndicatorVisible:NO]; | |
848ed88b JF |
8939 | |
8940 | #if TraceLogging | |
8941 | NSLog(@"releaseNetworkActivityIndicator->%d", activity_); | |
8942 | #endif | |
8943 | ||
54043703 JF |
8944 | } |
8945 | ||
674dce72 GP |
8946 | - (void) cancelAndClear:(bool)clear { |
8947 | @synchronized (self) { | |
8948 | if (clear) { | |
c6ca67ba | 8949 | [database_ clear]; |
b5e7eebb | 8950 | Queuing_ = false; |
674dce72 | 8951 | } else { |
b5e7eebb | 8952 | Queuing_ = true; |
6067f1b8 JF |
8953 | } |
8954 | ||
a4217bbb | 8955 | [self _updateData]; |
674dce72 | 8956 | } |
37d2b2a9 | 8957 | } |
7b0ce2da | 8958 | |
b5e7eebb GP |
8959 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
8960 | NSString *context([alert context]); | |
bc11cf5b | 8961 | |
6915b806 JF |
8962 | if ([context isEqualToString:@"conffile"]) { |
8963 | FILE *input = [database_ input]; | |
8964 | if (button == [alert cancelButtonIndex]) | |
8965 | fprintf(input, "N\n"); | |
8966 | else if (button == [alert firstOtherButtonIndex]) | |
8967 | fprintf(input, "Y\n"); | |
8968 | fflush(input); | |
8969 | ||
8970 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8971 | } else if ([context isEqualToString:@"fixhalf"]) { | |
efa53fa9 | 8972 | if (button == [alert cancelButtonIndex]) { |
37d2b2a9 | 8973 | @synchronized (self) { |
7b33d201 | 8974 | for (Package *broken in (id) broken_) { |
37d2b2a9 GP |
8975 | [broken remove]; |
8976 | ||
8977 | NSString *id = [broken id]; | |
8978 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]); | |
8979 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]); | |
8980 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]); | |
8981 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]); | |
8982 | } | |
7b0ce2da | 8983 | |
37d2b2a9 GP |
8984 | [self resolve]; |
8985 | [self perform]; | |
8986 | } | |
efa53fa9 | 8987 | } else if (button == [alert firstOtherButtonIndex]) { |
37d2b2a9 GP |
8988 | [broken_ removeAllObjects]; |
8989 | [self _loaded]; | |
7b0ce2da JF |
8990 | } |
8991 | ||
37d2b2a9 | 8992 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 8993 | } else if ([context isEqualToString:@"upgrade"]) { |
37d2b2a9 GP |
8994 | if (button == [alert firstOtherButtonIndex]) { |
8995 | @synchronized (self) { | |
7b33d201 | 8996 | for (Package *essential in (id) essential_) |
37d2b2a9 | 8997 | [essential install]; |
7b0ce2da | 8998 | |
37d2b2a9 GP |
8999 | [self resolve]; |
9000 | [self perform]; | |
9001 | } | |
9002 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
9003 | [self distUpgrade]; | |
9004 | } else if (button == [alert cancelButtonIndex]) { | |
9005 | Ignored_ = YES; | |
7b0ce2da JF |
9006 | } |
9007 | ||
37d2b2a9 | 9008 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9009 | } |
7b0ce2da JF |
9010 | } |
9011 | ||
d13edf44 JF |
9012 | - (void) system:(NSString *)command { |
9013 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9014 | ||
985d2dff | 9015 | _trace(); |
670a0494 | 9016 | system([command UTF8String]); |
985d2dff | 9017 | _trace(); |
d13edf44 JF |
9018 | |
9019 | [pool release]; | |
670a0494 JF |
9020 | } |
9021 | ||
9022 | - (void) applicationWillSuspend { | |
9023 | [database_ clean]; | |
9024 | [super applicationWillSuspend]; | |
bd150f54 JF |
9025 | } |
9026 | ||
235f5487 | 9027 | - (BOOL) isSafeToSuspend { |
5927fe03 JF |
9028 | if (locked_ != 0) { |
9029 | #if !ForRelease | |
9030 | NSLog(@"isSafeToSuspend: locked_ != 0"); | |
9031 | #endif | |
9032 | return false; | |
9033 | } | |
9034 | ||
262afe11 GP |
9035 | // Use external process status API internally. |
9036 | // This is probably a really bad idea. | |
235f5487 | 9037 | // XXX: what is the point of this? does this solve anything at all? |
262afe11 GP |
9038 | uint64_t status = 0; |
9039 | int notify_token; | |
9040 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
9041 | notify_get_state(notify_token, &status); | |
9042 | notify_cancel(notify_token); | |
9043 | } | |
9044 | ||
5927fe03 JF |
9045 | if (status != 0) { |
9046 | #if !ForRelease | |
9047 | NSLog(@"isSafeToSuspend: status != 0"); | |
9048 | #endif | |
9049 | return false; | |
9050 | } | |
9051 | ||
9052 | #if !ForRelease | |
9053 | NSLog(@"isSafeToSuspend: -> true"); | |
9054 | #endif | |
9055 | return true; | |
235f5487 JF |
9056 | } |
9057 | ||
9058 | - (void) applicationSuspend:(__GSEvent *)event { | |
9059 | if ([self isSafeToSuspend]) | |
bd150f54 | 9060 | [super applicationSuspend:event]; |
bd150f54 JF |
9061 | } |
9062 | ||
6d9712c4 | 9063 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
235f5487 | 9064 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9065 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
9066 | } | |
9067 | ||
9068 | - (void) _setSuspended:(BOOL)value { | |
235f5487 | 9069 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9070 | [super _setSuspended:value]; |
9071 | } | |
9072 | ||
7b0ce2da | 9073 | - (UIProgressHUD *) addProgressHUD { |
d061f4ba | 9074 | UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window_] autorelease]); |
04fe1349 JF |
9075 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
9076 | ||
d061f4ba | 9077 | [window_ setUserInteractionEnabled:NO]; |
534e31fc | 9078 | |
f36e5eac JF |
9079 | UIViewController *target(tabbar_); |
9080 | if (UIViewController *modal = [target modalViewController]) | |
9081 | target = modal; | |
9082 | ||
9083 | UIView *view([target view]); | |
9084 | [view addSubview:hud]; | |
9085 | ||
9086 | [hud show:YES]; | |
534e31fc | 9087 | |
235f5487 | 9088 | ++locked_; |
7b0ce2da JF |
9089 | return hud; |
9090 | } | |
9091 | ||
d061f4ba | 9092 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
f36e5eac | 9093 | --locked_; |
d061f4ba JF |
9094 | [hud show:NO]; |
9095 | [hud removeFromSuperview]; | |
9096 | [window_ setUserInteractionEnabled:YES]; | |
9097 | } | |
9098 | ||
cd79e8cf | 9099 | - (CyteViewController *) pageForPackage:(NSString *)name { |
57e8b225 | 9100 | return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name] autorelease]; |
c390d3ab JF |
9101 | } |
9102 | ||
cd79e8cf | 9103 | - (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external { |
e47c4742 | 9104 | NSString *scheme([[url scheme] lowercaseString]); |
f6e13561 | 9105 | if ([[url absoluteString] length] <= [scheme length] + 3) |
e47c4742 | 9106 | return nil; |
f6e13561 GP |
9107 | NSString *path([[url absoluteString] substringFromIndex:[scheme length] + 3]); |
9108 | NSArray *components([path pathComponents]); | |
9109 | ||
f5a17517 | 9110 | if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) |
f6e13561 GP |
9111 | return [self pageForPackage:[components objectAtIndex:1]]; |
9112 | ||
9113 | if ([components count] < 1 || ![scheme isEqualToString:@"cydia"]) | |
e47c4742 | 9114 | return nil; |
f6e13561 GP |
9115 | |
9116 | NSString *base([components objectAtIndex:0]); | |
9117 | ||
cd79e8cf | 9118 | CyteViewController *controller = nil; |
f5a17517 | 9119 | |
f70ea899 | 9120 | if ([base isEqualToString:@"url"]) { |
106d645f GP |
9121 | // This kind of URL can contain slashes in the argument, so we can't parse them below. |
9122 | NSString *destination = [[url absoluteString] substringFromIndex:([scheme length] + [@"://" length] + [base length] + [@"/" length])]; | |
a576488f | 9123 | controller = [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:destination]] autorelease]; |
028dbd1c | 9124 | } else if (!external && [components count] == 1) { |
fe8e721f GP |
9125 | if ([base isEqualToString:@"manage"]) { |
9126 | controller = [[[ManageController alloc] init] autorelease]; | |
9127 | } | |
9128 | ||
f6e13561 | 9129 | if ([base isEqualToString:@"sources"]) { |
f5a17517 | 9130 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9131 | } |
9132 | ||
9133 | if ([base isEqualToString:@"home"]) { | |
f5a17517 | 9134 | controller = [[[HomeController alloc] init] autorelease]; |
f6e13561 GP |
9135 | } |
9136 | ||
9137 | if ([base isEqualToString:@"sections"]) { | |
f5a17517 | 9138 | controller = [[[SectionsController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9139 | } |
9140 | ||
9141 | if ([base isEqualToString:@"search"]) { | |
43625891 | 9142 | controller = [[[SearchController alloc] initWithDatabase:database_ query:nil] autorelease]; |
f6e13561 GP |
9143 | } |
9144 | ||
9145 | if ([base isEqualToString:@"changes"]) { | |
ea3bb538 | 9146 | controller = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9147 | } |
9148 | ||
9149 | if ([base isEqualToString:@"installed"]) { | |
f5a17517 | 9150 | controller = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9151 | } |
9152 | } else if ([components count] == 2) { | |
9153 | NSString *argument = [components objectAtIndex:1]; | |
9154 | ||
9155 | if ([base isEqualToString:@"package"]) { | |
f5a17517 | 9156 | controller = [self pageForPackage:argument]; |
f6e13561 GP |
9157 | } |
9158 | ||
028dbd1c | 9159 | if (!external && [base isEqualToString:@"search"]) { |
43625891 | 9160 | controller = [[[SearchController alloc] initWithDatabase:database_ query:argument] autorelease]; |
f6e13561 GP |
9161 | } |
9162 | ||
028dbd1c | 9163 | if (!external && [base isEqualToString:@"sections"]) { |
f6e13561 GP |
9164 | if ([argument isEqualToString:@"all"]) |
9165 | argument = nil; | |
f5a17517 | 9166 | controller = [[[SectionController alloc] initWithDatabase:database_ section:argument] autorelease]; |
f6e13561 GP |
9167 | } |
9168 | ||
028dbd1c | 9169 | if (!external && [base isEqualToString:@"sources"]) { |
f6e13561 | 9170 | if ([argument isEqualToString:@"add"]) { |
f5a17517 GP |
9171 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
9172 | [(SourcesController *)controller showAddSourcePrompt]; | |
f6e13561 | 9173 | } else { |
d669236d GP |
9174 | Source *source = [database_ sourceWithKey:argument]; |
9175 | controller = [[[SourceController alloc] initWithDatabase:database_ source:source] autorelease]; | |
f6e13561 GP |
9176 | } |
9177 | } | |
9178 | ||
028dbd1c | 9179 | if (!external && [base isEqualToString:@"launch"]) { |
f6e13561 | 9180 | [self launchApplicationWithIdentifier:argument suspended:NO]; |
f5a17517 | 9181 | return nil; |
f6e13561 | 9182 | } |
028dbd1c | 9183 | } else if (!external && [components count] == 3) { |
f6e13561 GP |
9184 | NSString *arg1 = [components objectAtIndex:1]; |
9185 | NSString *arg2 = [components objectAtIndex:2]; | |
9186 | ||
9187 | if ([base isEqualToString:@"package"]) { | |
9188 | if ([arg2 isEqualToString:@"settings"]) { | |
f5a17517 | 9189 | controller = [[[PackageSettingsController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 GP |
9190 | } else if ([arg2 isEqualToString:@"files"]) { |
9191 | if (Package *package = [database_ packageWithName:arg1]) { | |
f5a17517 GP |
9192 | controller = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
9193 | [(FileTable *)controller setPackage:package]; | |
f6e13561 GP |
9194 | } |
9195 | } | |
c390d3ab JF |
9196 | } |
9197 | } | |
9198 | ||
f5a17517 GP |
9199 | [controller setDelegate:self]; |
9200 | return controller; | |
c390d3ab JF |
9201 | } |
9202 | ||
028dbd1c | 9203 | - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external { |
cd79e8cf | 9204 | CyteViewController *page([self pageForURL:url forExternal:external]); |
40364973 | 9205 | |
c713af59 | 9206 | if (page != nil) { |
15f0d613 | 9207 | UINavigationController *nav = [[[UINavigationController alloc] init] autorelease]; |
c713af59 | 9208 | [nav setViewControllers:[NSArray arrayWithObject:page]]; |
9f99f3da | 9209 | [tabbar_ setUnselectedViewController:nav]; |
c713af59 | 9210 | } |
40364973 | 9211 | |
f6e13561 | 9212 | return page != nil; |
40364973 GP |
9213 | } |
9214 | ||
c390d3ab JF |
9215 | - (void) applicationOpenURL:(NSURL *)url { |
9216 | [super applicationOpenURL:url]; | |
d817e4de | 9217 | |
7b33d201 JF |
9218 | if (!loaded_) |
9219 | starturl_ = url; | |
9220 | else | |
9221 | [self openCydiaURL:url forExternal:YES]; | |
c390d3ab JF |
9222 | } |
9223 | ||
bc11cf5b | 9224 | - (void) applicationWillResignActive:(UIApplication *)application { |
7eff7ea6 | 9225 | // Stop refreshing if you get a phone call or lock the device. |
7585ce66 JF |
9226 | if ([tabbar_ updating]) |
9227 | [tabbar_ cancelUpdate]; | |
bc11cf5b | 9228 | |
ca584c15 GP |
9229 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
9230 | [super applicationWillResignActive:application]; | |
7eff7ea6 GP |
9231 | } |
9232 | ||
fe8e721f | 9233 | - (void) applicationWillTerminate:(UIApplication *)application { |
35f0a3b5 GP |
9234 | Changed_ = true; |
9235 | [Metadata_ setObject:[tabbar_ navigationURLCollection] forKey:@"InterfaceState"]; | |
fe8e721f GP |
9236 | [Metadata_ setObject:[NSDate date] forKey:@"LastClosed"]; |
9237 | [Metadata_ setObject:[NSNumber numberWithInt:[tabbar_ selectedIndex]] forKey:@"InterfaceIndex"]; | |
9238 | ||
9239 | [self _saveConfig]; | |
9240 | } | |
9241 | ||
6915b806 JF |
9242 | - (void) setConfigurationData:(NSString *)data { |
9243 | static Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$"); | |
9244 | ||
9245 | if (!conffile_r(data)) { | |
9246 | lprintf("E:invalid conffile\n"); | |
9247 | return; | |
9248 | } | |
9249 | ||
9250 | NSString *ofile = conffile_r[1]; | |
9251 | //NSString *nfile = conffile_r[2]; | |
9252 | ||
9253 | UIAlertView *alert = [[[UIAlertView alloc] | |
9254 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") | |
9255 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] | |
9256 | delegate:self | |
9257 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
9258 | otherButtonTitles: | |
9259 | UCLocalize("ACCEPT_NEW_COPY"), | |
9260 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), | |
9261 | nil | |
9262 | ] autorelease]; | |
9263 | ||
9264 | [alert setContext:@"conffile"]; | |
a08145a8 | 9265 | [alert setNumberOfRows:2]; |
6915b806 JF |
9266 | [alert show]; |
9267 | } | |
9268 | ||
f3e11d24 | 9269 | - (void) addStashController { |
235f5487 | 9270 | ++locked_; |
7b33d201 | 9271 | stash_ = [[[StashController alloc] init] autorelease]; |
f3e11d24 GP |
9272 | [window_ addSubview:[stash_ view]]; |
9273 | } | |
9274 | ||
9275 | - (void) removeStashController { | |
9276 | [[stash_ view] removeFromSuperview]; | |
7b33d201 | 9277 | stash_ = nil; |
235f5487 | 9278 | --locked_; |
f3e11d24 GP |
9279 | } |
9280 | ||
9281 | - (void) stash { | |
9282 | [self setIdleTimerDisabled:YES]; | |
9283 | ||
9e1f1e91 | 9284 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
f3e11d24 | 9285 | UpdateExternalStatus(1); |
f3e11d24 | 9286 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/free.sh"]; |
f3e11d24 | 9287 | UpdateExternalStatus(0); |
f3e11d24 GP |
9288 | |
9289 | [self removeStashController]; | |
9290 | ||
9291 | if (ExecFork() == 0) { | |
9292 | execlp("launchctl", "launchctl", "stop", "com.apple.SpringBoard", NULL); | |
9293 | perror("launchctl stop"); | |
9294 | } | |
9295 | } | |
9296 | ||
f6e13561 | 9297 | - (void) setupViewControllers { |
7b33d201 | 9298 | tabbar_ = [[[CYTabBarController alloc] initWithDatabase:database_] autorelease]; |
851f4a99 GP |
9299 | |
9300 | NSMutableArray *items([NSMutableArray arrayWithObjects: | |
89571a5b GP |
9301 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage applicationImageNamed:@"home.png"] tag:0] autorelease], |
9302 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SECTIONS") image:[UIImage applicationImageNamed:@"install.png"] tag:0] autorelease], | |
9303 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage applicationImageNamed:@"changes.png"] tag:0] autorelease], | |
9304 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage applicationImageNamed:@"search.png"] tag:0] autorelease], | |
f6e13561 GP |
9305 | nil]); |
9306 | ||
851f4a99 | 9307 | if (IsWildcat_) { |
89571a5b GP |
9308 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage applicationImageNamed:@"source.png"] tag:0] autorelease] atIndex:3]; |
9309 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage applicationImageNamed:@"manage.png"] tag:0] autorelease] atIndex:3]; | |
851f4a99 | 9310 | } else { |
89571a5b | 9311 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("MANAGE") image:[UIImage applicationImageNamed:@"manage.png"] tag:0] autorelease] atIndex:3]; |
851f4a99 GP |
9312 | } |
9313 | ||
9314 | NSMutableArray *controllers([NSMutableArray array]); | |
851f4a99 | 9315 | for (UITabBarItem *item in items) { |
15f0d613 | 9316 | UINavigationController *controller([[[UINavigationController alloc] init] autorelease]); |
851f4a99 GP |
9317 | [controller setTabBarItem:item]; |
9318 | [controllers addObject:controller]; | |
9319 | } | |
851f4a99 | 9320 | [tabbar_ setViewControllers:controllers]; |
4305896c | 9321 | |
f6e13561 | 9322 | [tabbar_ setUpdateDelegate:self]; |
851f4a99 GP |
9323 | } |
9324 | ||
bd150f54 | 9325 | - (void) applicationDidFinishLaunching:(id)unused { |
7e30ba6d | 9326 | _trace(); |
1e94d48b JF |
9327 | if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) |
9328 | [self setApplicationSupportsShakeToEdit:NO]; | |
9329 | ||
48f1762f JF |
9330 | @synchronized (HostConfig_) { |
9331 | [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; | |
9332 | } | |
3171f7fe | 9333 | |
a06e9179 | 9334 | [NSURLCache setSharedURLCache:[[[CYURLCache alloc] |
71cc7be1 JF |
9335 | initWithMemoryCapacity:524288 |
9336 | diskCapacity:10485760 | |
9337 | diskPath:[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia/SDURLCache", @"/var/root"] | |
9338 | ] autorelease]]; | |
9339 | ||
a576488f | 9340 | [CydiaWebViewController _initialize]; |
ea173384 | 9341 | |
bfc87a4d JF |
9342 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
9343 | ||
793aee35 JF |
9344 | // this would disallow http{,s} URLs from accessing this data |
9345 | //[WebView registerURLSchemeAsLocal:@"cydia"]; | |
9346 | ||
7b33d201 JF |
9347 | Font12_ = [UIFont systemFontOfSize:12]; |
9348 | Font12Bold_ = [UIFont boldSystemFontOfSize:12]; | |
9349 | Font14_ = [UIFont systemFontOfSize:14]; | |
9350 | Font18Bold_ = [UIFont boldSystemFontOfSize:18]; | |
9351 | Font22Bold_ = [UIFont boldSystemFontOfSize:22]; | |
f641a0e5 | 9352 | |
7b33d201 JF |
9353 | essential_ = [NSMutableArray arrayWithCapacity:4]; |
9354 | broken_ = [NSMutableArray arrayWithCapacity:4]; | |
bd150f54 | 9355 | |
4e89e880 | 9356 | // XXX: I really need this thing... like, seriously... I'm sorry |
a576488f | 9357 | [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] reloadData]; |
4e89e880 | 9358 | |
7b33d201 | 9359 | window_ = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
f641a0e5 JF |
9360 | [window_ orderFront:self]; |
9361 | [window_ makeKey:self]; | |
c390d3ab | 9362 | [window_ setHidden:NO]; |
04fe1349 | 9363 | |
f3e11d24 GP |
9364 | if ( |
9365 | readlink("/Applications", NULL, 0) == -1 && errno == EINVAL || | |
9366 | readlink("/Library/Ringtones", NULL, 0) == -1 && errno == EINVAL || | |
9367 | readlink("/Library/Wallpaper", NULL, 0) == -1 && errno == EINVAL || | |
9368 | //readlink("/usr/bin", NULL, 0) == -1 && errno == EINVAL || | |
9369 | readlink("/usr/include", NULL, 0) == -1 && errno == EINVAL || | |
9370 | readlink("/usr/lib/pam", NULL, 0) == -1 && errno == EINVAL || | |
9371 | readlink("/usr/libexec", NULL, 0) == -1 && errno == EINVAL || | |
9372 | readlink("/usr/share", NULL, 0) == -1 && errno == EINVAL || | |
9373 | //readlink("/var/lib", NULL, 0) == -1 && errno == EINVAL || | |
9374 | false | |
9375 | ) { | |
9376 | [self addStashController]; | |
3931b718 JF |
9377 | // XXX: this would be much cleaner as a yieldToSelector: |
9378 | // that way the removeStashController could happen right here inline | |
9379 | // we also could no longer require the useless stash_ field anymore | |
f3e11d24 GP |
9380 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
9381 | return; | |
9382 | } | |
9383 | ||
770f2a8e | 9384 | database_ = [Database sharedInstance]; |
6915b806 | 9385 | [database_ setDelegate:self]; |
bfc87a4d | 9386 | |
89571a5b | 9387 | [window_ setUserInteractionEnabled:NO]; |
0be165c8 | 9388 | [self setupViewControllers]; |
6915b806 | 9389 | |
70750ab3 | 9390 | emulated_ = [[[CydiaLoadingViewController alloc] init] autorelease]; |
6915b806 | 9391 | [window_ addSubview:[emulated_ view]]; |
5ccb47d8 | 9392 | |
fed0d010 | 9393 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
c626a63f | 9394 | _trace(); |
fed0d010 JF |
9395 | } |
9396 | ||
d3a28a81 GP |
9397 | - (NSArray *) defaultStartPages { |
9398 | NSMutableArray *standard = [NSMutableArray array]; | |
9399 | [standard addObject:[NSArray arrayWithObject:@"cydia://home"]]; | |
9400 | [standard addObject:[NSArray arrayWithObject:@"cydia://sections"]]; | |
9401 | [standard addObject:[NSArray arrayWithObject:@"cydia://changes"]]; | |
9402 | if (!IsWildcat_) { | |
9403 | [standard addObject:[NSArray arrayWithObject:@"cydia://manage"]]; | |
9404 | } else { | |
9405 | [standard addObject:[NSArray arrayWithObject:@"cydia://installed"]]; | |
9406 | [standard addObject:[NSArray arrayWithObject:@"cydia://sources"]]; | |
9407 | } | |
9408 | [standard addObject:[NSArray arrayWithObject:@"cydia://search"]]; | |
9409 | return standard; | |
9410 | } | |
9411 | ||
fed0d010 | 9412 | - (void) loadData { |
c626a63f | 9413 | _trace(); |
fed0d010 | 9414 | if (Role_ == nil) { |
0c1cb67a | 9415 | [window_ setUserInteractionEnabled:YES]; |
a54fed5b | 9416 | [self showSettings]; |
fed0d010 | 9417 | return; |
0c1cb67a | 9418 | } else { |
6915b806 JF |
9419 | if ([emulated_ modalViewController] != nil) |
9420 | [emulated_ dismissModalViewControllerAnimated:YES]; | |
0c1cb67a | 9421 | [window_ setUserInteractionEnabled:NO]; |
fed0d010 JF |
9422 | } |
9423 | ||
851f4a99 | 9424 | [self reloadData]; |
670a0494 | 9425 | PrintTimes(); |
5ccb47d8 | 9426 | |
2e9123cb | 9427 | [self disemulate]; |
5ccb47d8 | 9428 | |
d3a28a81 GP |
9429 | int savedIndex = [[Metadata_ objectForKey:@"InterfaceIndex"] intValue]; |
9430 | NSArray *saved = [[Metadata_ objectForKey:@"InterfaceState"] mutableCopy]; | |
9431 | int standardIndex = 0; | |
9432 | NSArray *standard = [self defaultStartPages]; | |
fe8e721f | 9433 | |
d3a28a81 GP |
9434 | BOOL valid = YES; |
9435 | ||
9436 | if (saved == nil) | |
9437 | valid = NO; | |
9438 | ||
9439 | NSDate *closed = [Metadata_ objectForKey:@"LastClosed"]; | |
9440 | if (valid && closed != nil) { | |
fe8e721f GP |
9441 | NSTimeInterval interval([closed timeIntervalSinceNow]); |
9442 | // XXX: Is 15 minutes the optimal time here? | |
d3a28a81 GP |
9443 | if (interval > 0 && interval <= -(15*60)) |
9444 | valid = NO; | |
fe8e721f GP |
9445 | } |
9446 | ||
d3a28a81 GP |
9447 | if (valid && [saved count] != [standard count]) |
9448 | valid = NO; | |
efa53fa9 | 9449 | |
d3a28a81 GP |
9450 | if (valid) { |
9451 | for (unsigned int i = 0; i < [standard count]; i++) { | |
9452 | NSArray *std = [standard objectAtIndex:i], *sav = [saved objectAtIndex:i]; | |
9453 | // XXX: The "hasPrefix" sanity check here could be, in theory, fooled, | |
9454 | // but it's good enough for now. | |
9455 | if ([sav count] == 0 || ![[sav objectAtIndex:0] hasPrefix:[std objectAtIndex:0]]) { | |
9456 | valid = NO; | |
9457 | break; | |
9458 | } | |
fe8e721f | 9459 | } |
fe8e721f GP |
9460 | } |
9461 | ||
d3a28a81 GP |
9462 | NSArray *items = nil; |
9463 | if (valid) { | |
9464 | [tabbar_ setSelectedIndex:savedIndex]; | |
9465 | items = saved; | |
9466 | } else { | |
9467 | [tabbar_ setSelectedIndex:standardIndex]; | |
9468 | items = standard; | |
9469 | } | |
9470 | ||
fe8e721f GP |
9471 | for (unsigned int tab = 0; tab < [[tabbar_ viewControllers] count]; tab++) { |
9472 | NSArray *stack = [items objectAtIndex:tab]; | |
15f0d613 | 9473 | UINavigationController *navigation = [[tabbar_ viewControllers] objectAtIndex:tab]; |
fe8e721f GP |
9474 | NSMutableArray *current = [NSMutableArray array]; |
9475 | ||
9476 | for (unsigned int nav = 0; nav < [stack count]; nav++) { | |
9477 | NSString *addr = [stack objectAtIndex:nav]; | |
9478 | NSURL *url = [NSURL URLWithString:addr]; | |
cd79e8cf | 9479 | CyteViewController *page = [self pageForURL:url forExternal:NO]; |
fe8e721f GP |
9480 | if (page != nil) |
9481 | [current addObject:page]; | |
9482 | } | |
9483 | ||
9484 | [navigation setViewControllers:current]; | |
9485 | } | |
f6e13561 | 9486 | |
89571a5b | 9487 | // (Try to) show the startup URL. |
f6e13561 | 9488 | if (starturl_ != nil) { |
028dbd1c | 9489 | [self openCydiaURL:starturl_ forExternal:NO]; |
f6e13561 GP |
9490 | starturl_ = nil; |
9491 | } | |
bd150f54 JF |
9492 | } |
9493 | ||
b5e7eebb | 9494 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
674dce72 | 9495 | if (item != nil && IsWildcat_) { |
b5e7eebb | 9496 | [sheet showFromBarButtonItem:item animated:YES]; |
674dce72 GP |
9497 | } else { |
9498 | [sheet showInView:window_]; | |
9499 | } | |
36bb2ca2 JF |
9500 | } |
9501 | ||
6915b806 JF |
9502 | - (void) addProgressEvent:(CydiaProgressEvent *)event forTask:(NSString *)task { |
9503 | id<ProgressDelegate> progress([database_ progressDelegate] ?: [self invokeNewProgress:nil forController:nil withTitle:task]); | |
9504 | [progress setTitle:task]; | |
9505 | [progress addProgressEvent:event]; | |
9506 | } | |
9507 | ||
9508 | - (void) addProgressEventForTask:(NSArray *)data { | |
9509 | CydiaProgressEvent *event([data objectAtIndex:0]); | |
9510 | NSString *task([data count] < 2 ? nil : [data objectAtIndex:1]); | |
9511 | [self addProgressEvent:event forTask:task]; | |
9512 | } | |
9513 | ||
9514 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task { | |
9515 | [self performSelectorOnMainThread:@selector(addProgressEventForTask:) withObject:[NSArray arrayWithObjects:event, task, nil] waitUntilDone:YES]; | |
9516 | } | |
9517 | ||
dc5812ec JF |
9518 | @end |
9519 | ||
b4d89997 JF |
9520 | /*IMP alloc_; |
9521 | id Alloc_(id self, SEL selector) { | |
9522 | id object = alloc_(self, selector); | |
c390d3ab | 9523 | lprintf("[%s]A-%p\n", self->isa->name, object); |
b4d89997 JF |
9524 | return object; |
9525 | }*/ | |
9526 | ||
36bb2ca2 JF |
9527 | /*IMP dealloc_; |
9528 | id Dealloc_(id self, SEL selector) { | |
9529 | id object = dealloc_(self, selector); | |
c390d3ab | 9530 | lprintf("[%s]D-%p\n", self->isa->name, object); |
36bb2ca2 JF |
9531 | return object; |
9532 | }*/ | |
b4d89997 | 9533 | |
f79a4512 JF |
9534 | Class $WebDefaultUIKitDelegate; |
9535 | ||
d791dce4 | 9536 | MSHook(void, UIWebDocumentView$_setUIKitDelegate$, UIWebDocumentView *self, SEL _cmd, id delegate) { |
f79a4512 JF |
9537 | if (delegate == nil && $WebDefaultUIKitDelegate != nil) |
9538 | delegate = [$WebDefaultUIKitDelegate sharedUIKitDelegate]; | |
d791dce4 | 9539 | return _UIWebDocumentView$_setUIKitDelegate$(self, _cmd, delegate); |
f79a4512 JF |
9540 | } |
9541 | ||
baf82ed4 | 9542 | static NSSet *MobilizedFiles_; |
10d65752 | 9543 | |
baf82ed4 JF |
9544 | static NSURL *MobilizeURL(NSURL *url) { |
9545 | NSString *path([url path]); | |
9546 | if ([path hasPrefix:@"/var/root/"]) { | |
9547 | NSString *file([path substringFromIndex:10]); | |
9548 | if ([MobilizedFiles_ containsObject:file]) | |
9549 | url = [NSURL fileURLWithPath:[@"/var/mobile/" stringByAppendingString:file] isDirectory:NO]; | |
10d65752 | 9550 | } |
79b42fd1 | 9551 | |
baf82ed4 JF |
9552 | return url; |
9553 | } | |
79b42fd1 | 9554 | |
baf82ed4 JF |
9555 | Class $CFXPreferencesPropertyListSource; |
9556 | @class CFXPreferencesPropertyListSource; | |
79b42fd1 | 9557 | |
baf82ed4 JF |
9558 | MSHook(BOOL, CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync, CFXPreferencesPropertyListSource *self, SEL _cmd) { |
9559 | NSURL *&url(MSHookIvar<NSURL *>(self, "_url")), *old(url); | |
9560 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9561 | url = MobilizeURL(url); | |
9562 | BOOL value(_CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync(self, _cmd)); | |
9563 | //NSLog(@"%@ %s", [url absoluteString], value ? "YES" : "NO"); | |
9564 | url = old; | |
9565 | [pool release]; | |
9566 | return value; | |
9567 | } | |
79b42fd1 | 9568 | |
baf82ed4 JF |
9569 | MSHook(void *, CFXPreferencesPropertyListSource$createPlistFromDisk, CFXPreferencesPropertyListSource *self, SEL _cmd) { |
9570 | NSURL *&url(MSHookIvar<NSURL *>(self, "_url")), *old(url); | |
9571 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9572 | url = MobilizeURL(url); | |
9573 | void *value(_CFXPreferencesPropertyListSource$createPlistFromDisk(self, _cmd)); | |
9574 | //NSLog(@"%@ %@", [url absoluteString], value); | |
9575 | url = old; | |
9576 | [pool release]; | |
9577 | return value; | |
79b42fd1 GP |
9578 | } |
9579 | ||
30c5be06 JF |
9580 | Class $NSURLConnection; |
9581 | ||
9582 | MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) { | |
9583 | NSMutableURLRequest *copy([request mutableCopy]); | |
9584 | ||
9585 | NSURL *url([copy URL]); | |
5e845121 JF |
9586 | |
9587 | NSString *href([url absoluteString]); | |
30c5be06 | 9588 | NSString *host([url host]); |
e4b48f2f JF |
9589 | NSString *scheme([[url scheme] lowercaseString]); |
9590 | ||
9591 | NSString *compound([NSString stringWithFormat:@"%@:%@", scheme, host]); | |
30c5be06 | 9592 | |
48f1762f JF |
9593 | @synchronized (HostConfig_) { |
9594 | if ([copy respondsToSelector:@selector(setHTTPShouldUsePipelining:)]) | |
9595 | if ([PipelinedHosts_ containsObject:host] || [PipelinedHosts_ containsObject:compound]) | |
9596 | [copy setHTTPShouldUsePipelining:YES]; | |
5e845121 JF |
9597 | |
9598 | if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"]) | |
9599 | if ([control isEqualToString:@"max-age=0"]) | |
9600 | if ([CachedURLs_ containsObject:href]) { | |
9601 | #if !ForRelease | |
9602 | NSLog(@"~~~: %@", href); | |
9603 | #endif | |
9604 | ||
9605 | [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; | |
9606 | ||
9607 | [copy setValue:nil forHTTPHeaderField:@"Cache-Control"]; | |
9608 | [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"]; | |
9609 | [copy setValue:nil forHTTPHeaderField:@"If-None-Match"]; | |
9610 | } | |
48f1762f | 9611 | } |
30c5be06 JF |
9612 | |
9613 | if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) { | |
9614 | } return self; | |
9615 | } | |
9616 | ||
d13edf44 JF |
9617 | int main(int argc, char *argv[]) { |
9618 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9619 | ||
d6dad1b4 | 9620 | _trace(); |
f79a4512 | 9621 | |
9a60abe5 JF |
9622 | UpdateExternalStatus(0); |
9623 | ||
01d93940 JF |
9624 | if (Class $UIDevice = objc_getClass("UIDevice")) { |
9625 | UIDevice *device([$UIDevice currentDevice]); | |
9626 | IsWildcat_ = [device respondsToSelector:@selector(isWildcat)] && [device isWildcat]; | |
9627 | } else | |
9628 | IsWildcat_ = false; | |
9629 | ||
57e8b225 JF |
9630 | UIScreen *screen([UIScreen mainScreen]); |
9631 | if ([screen respondsToSelector:@selector(scale)]) | |
9632 | ScreenScale_ = [screen scale]; | |
9633 | else | |
9634 | ScreenScale_ = 1; | |
9635 | ||
c138614d JF |
9636 | UIDevice *device([UIDevice currentDevice]); |
9637 | if (![device respondsToSelector:@selector(userInterfaceIdiom)]) | |
9638 | Idiom_ = @"iphone"; | |
9639 | else { | |
9640 | UIUserInterfaceIdiom idiom([device userInterfaceIdiom]); | |
9641 | if (idiom == UIUserInterfaceIdiomPhone) | |
9642 | Idiom_ = @"iphone"; | |
9643 | else if (idiom == UIUserInterfaceIdiomPad) | |
9644 | Idiom_ = @"ipad"; | |
9645 | else | |
9646 | NSLog(@"unknown UIUserInterfaceIdiom!"); | |
9647 | } | |
9648 | ||
7b33d201 | 9649 | SessionData_ = [NSMutableDictionary dictionaryWithCapacity:4]; |
ef974f52 | 9650 | |
7b33d201 | 9651 | HostConfig_ = [[[NSObject alloc] init] autorelease]; |
48f1762f JF |
9652 | @synchronized (HostConfig_) { |
9653 | BridgedHosts_ = [NSMutableSet setWithCapacity:4]; | |
9654 | PipelinedHosts_ = [NSMutableSet setWithCapacity:4]; | |
5e845121 | 9655 | CachedURLs_ = [NSMutableSet setWithCapacity:32]; |
48f1762f | 9656 | } |
5df7ecfb | 9657 | |
c138614d | 9658 | UI_ = CydiaURL([NSString stringWithFormat:@"ui/ios~%@", Idiom_]); |
57e8b225 | 9659 | |
df213583 | 9660 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
677b8415 | 9661 | |
baf82ed4 JF |
9662 | MobilizedFiles_ = [NSMutableSet setWithObjects: |
9663 | @"Library/Preferences/com.apple.Accessibility.plist", | |
9664 | @"Library/Preferences/com.apple.preferences.sounds.plist", | |
9665 | nil]; | |
9666 | ||
7376b55c JF |
9667 | /* Library Hacks {{{ */ |
9668 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
9669 | ||
baf82ed4 JF |
9670 | $CFXPreferencesPropertyListSource = objc_getClass("CFXPreferencesPropertyListSource"); |
9671 | ||
9672 | Method CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync(class_getInstanceMethod($CFXPreferencesPropertyListSource, @selector(_backingPlistChangedSinceLastSync))); | |
9673 | if (CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync != NULL) { | |
9674 | _CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync = reinterpret_cast<BOOL (*)(CFXPreferencesPropertyListSource *, SEL)>(method_getImplementation(CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync)); | |
9675 | method_setImplementation(CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync, reinterpret_cast<IMP>(&$CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync)); | |
9676 | } | |
9677 | ||
9678 | Method CFXPreferencesPropertyListSource$createPlistFromDisk(class_getInstanceMethod($CFXPreferencesPropertyListSource, @selector(createPlistFromDisk))); | |
9679 | if (CFXPreferencesPropertyListSource$createPlistFromDisk != NULL) { | |
9680 | _CFXPreferencesPropertyListSource$createPlistFromDisk = reinterpret_cast<void *(*)(CFXPreferencesPropertyListSource *, SEL)>(method_getImplementation(CFXPreferencesPropertyListSource$createPlistFromDisk)); | |
9681 | method_setImplementation(CFXPreferencesPropertyListSource$createPlistFromDisk, reinterpret_cast<IMP>(&$CFXPreferencesPropertyListSource$createPlistFromDisk)); | |
9682 | } | |
9683 | ||
7376b55c JF |
9684 | $WebDefaultUIKitDelegate = objc_getClass("WebDefaultUIKitDelegate"); |
9685 | Method UIWebDocumentView$_setUIKitDelegate$(class_getInstanceMethod([WebView class], @selector(_setUIKitDelegate:))); | |
9686 | if (UIWebDocumentView$_setUIKitDelegate$ != NULL) { | |
9687 | _UIWebDocumentView$_setUIKitDelegate$ = reinterpret_cast<void (*)(UIWebDocumentView *, SEL, id)>(method_getImplementation(UIWebDocumentView$_setUIKitDelegate$)); | |
9688 | method_setImplementation(UIWebDocumentView$_setUIKitDelegate$, reinterpret_cast<IMP>(&$UIWebDocumentView$_setUIKitDelegate$)); | |
9689 | } | |
10d65752 | 9690 | |
30c5be06 JF |
9691 | $NSURLConnection = objc_getClass("NSURLConnection"); |
9692 | Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:))); | |
9693 | if (NSURLConnection$init$ != NULL) { | |
9694 | _NSURLConnection$init$ = reinterpret_cast<id (*)(NSURLConnection *, SEL, NSURLRequest *, id, BOOL, int64_t, BOOL, NSDictionary *)>(method_getImplementation(NSURLConnection$init$)); | |
9695 | method_setImplementation(NSURLConnection$init$, reinterpret_cast<IMP>(&$NSURLConnection$init$)); | |
9696 | } | |
7376b55c JF |
9697 | /* }}} */ |
9698 | /* Set Locale {{{ */ | |
f79a4512 | 9699 | Locale_ = CFLocaleCopyCurrent(); |
b1ce61ec | 9700 | Languages_ = [NSLocale preferredLanguages]; |
25fdc941 | 9701 | |
b1ce61ec | 9702 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); |
18876387 | 9703 | //NSLog(@"%@", [Languages_ description]); |
78430d06 | 9704 | |
b1ce61ec | 9705 | const char *lang; |
25fdc941 JF |
9706 | if (Locale_ != NULL) |
9707 | lang = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]; | |
3caee0a4 JF |
9708 | else if (Languages_ != nil && [Languages_ count] != 0) |
9709 | lang = [[Languages_ objectAtIndex:0] UTF8String]; | |
9710 | else | |
78430d06 | 9711 | // XXX: consider just setting to C and then falling through? |
b1ce61ec | 9712 | lang = NULL; |
3caee0a4 JF |
9713 | |
9714 | if (lang != NULL) { | |
9715 | Pcre pattern("^([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?$"); | |
9716 | lang = !pattern(lang) ? NULL : [pattern->*@"%1$@%2$@" UTF8String]; | |
78430d06 JF |
9717 | } |
9718 | ||
b1ce61ec | 9719 | NSLog(@"Setting Language: %s", lang); |
3caee0a4 JF |
9720 | |
9721 | if (lang != NULL) { | |
9722 | setenv("LANG", lang, true); | |
9723 | std::setlocale(LC_ALL, lang); | |
9724 | } | |
7376b55c | 9725 | /* }}} */ |
f79a4512 | 9726 | |
d791dce4 | 9727 | apr_app_initialize(&argc, const_cast<const char * const **>(&argv), NULL); |
f79a4512 | 9728 | |
7376b55c | 9729 | /* Parse Arguments {{{ */ |
de3b1ab4 JF |
9730 | bool substrate(false); |
9731 | ||
9732 | if (argc != 0) { | |
9733 | char **args(argv); | |
9734 | int arge(1); | |
9735 | ||
9736 | for (int argi(1); argi != argc; ++argi) | |
9737 | if (strcmp(argv[argi], "--") == 0) { | |
9738 | arge = argi; | |
9739 | argv[argi] = argv[0]; | |
9740 | argv += argi; | |
9741 | argc -= argi; | |
9742 | break; | |
9743 | } | |
9744 | ||
9745 | for (int argi(1); argi != arge; ++argi) | |
d791dce4 | 9746 | if (strcmp(args[argi], "--substrate") == 0) |
de3b1ab4 JF |
9747 | substrate = true; |
9748 | else | |
9749 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
9750 | } | |
7376b55c | 9751 | /* }}} */ |
d73cede2 | 9752 | |
7376b55c | 9753 | App_ = [[NSBundle mainBundle] bundlePath]; |
d791dce4 | 9754 | Advanced_ = YES; |
7376b55c | 9755 | |
b4d89997 JF |
9756 | setuid(0); |
9757 | setgid(0); | |
9758 | ||
9759 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); | |
9760 | alloc_ = alloc->method_imp; | |
9761 | alloc->method_imp = (IMP) &Alloc_;*/ | |
9762 | ||
36bb2ca2 JF |
9763 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
9764 | dealloc_ = dealloc->method_imp; | |
9765 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
9766 | ||
d791dce4 | 9767 | /* System Information {{{ */ |
3178d79b | 9768 | size_t size; |
c390d3ab JF |
9769 | |
9770 | int maxproc; | |
9771 | size = sizeof(maxproc); | |
9772 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
9773 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
9774 | else if (maxproc < 64) { | |
9775 | maxproc = 64; | |
9776 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
9777 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
9778 | } | |
9779 | ||
bfc87a4d JF |
9780 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
9781 | char *osversion = new char[size]; | |
9782 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
9783 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
9784 | else | |
9785 | System_ = [NSString stringWithUTF8String:osversion]; | |
9786 | ||
3178d79b JF |
9787 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
9788 | char *machine = new char[size]; | |
c390d3ab JF |
9789 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
9790 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
9791 | else | |
9792 | Machine_ = machine; | |
3178d79b | 9793 | |
c31d7cdc JF |
9794 | SerialNumber_ = (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber"); |
9795 | ChipID_ = [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString]; | |
9796 | BBSNum_ = CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false); | |
59dbe296 | 9797 | |
87f46a96 | 9798 | UniqueID_ = [[UIDevice currentDevice] uniqueIdentifier]; |
3178d79b | 9799 | |
567e3972 JF |
9800 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef); |
9801 | $CTSIMSupportCopyMobileSubscriberCountryCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9802 | CFStringRef mcc($CTSIMSupportCopyMobileSubscriberCountryCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault)); | |
9803 | ||
9804 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef); | |
9805 | $CTSIMSupportCopyMobileSubscriberNetworkCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9806 | CFStringRef mnc($CTSIMSupportCopyMobileSubscriberNetworkCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault)); | |
9807 | ||
9808 | if (mcc != NULL && mnc != NULL) | |
9809 | PLMN_ = [NSString stringWithFormat:@"%@%@", mcc, mnc]; | |
9810 | ||
9811 | if (mnc != NULL) | |
9812 | CFRelease(mnc); | |
9813 | if (mcc != NULL) | |
9814 | CFRelease(mcc); | |
9815 | ||
ad5d065e JF |
9816 | if (NSDictionary *system = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]) |
9817 | Build_ = [system objectForKey:@"ProductBuildVersion"]; | |
3e9c9e85 JF |
9818 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
9819 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
9820 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
9821 | } | |
d791dce4 | 9822 | /* }}} */ |
7376b55c | 9823 | /* Load Database {{{ */ |
d6dad1b4 | 9824 | _trace(); |
f79a4512 JF |
9825 | Metadata_ = [[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]; |
9826 | _trace(); | |
9827 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; | |
d6dad1b4 JF |
9828 | |
9829 | if (Metadata_ == NULL) | |
f79a4512 | 9830 | Metadata_ = [NSMutableDictionary dictionaryWithCapacity:2]; |
6d9712c4 | 9831 | else { |
2bdd73bd | 9832 | Settings_ = [Metadata_ objectForKey:@"Settings"]; |
7b0ce2da | 9833 | |
b4d89997 | 9834 | Packages_ = [Metadata_ objectForKey:@"Packages"]; |
6d9712c4 | 9835 | Sections_ = [Metadata_ objectForKey:@"Sections"]; |
7b0ce2da | 9836 | Sources_ = [Metadata_ objectForKey:@"Sources"]; |
ef055c6c JF |
9837 | |
9838 | Token_ = [Metadata_ objectForKey:@"Token"]; | |
7b0ce2da JF |
9839 | } |
9840 | ||
9841 | if (Settings_ != nil) | |
9842 | Role_ = [Settings_ objectForKey:@"Role"]; | |
9843 | ||
7b0ce2da JF |
9844 | if (Sections_ == nil) { |
9845 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; | |
9846 | [Metadata_ setObject:Sections_ forKey:@"Sections"]; | |
9847 | } | |
9848 | ||
9849 | if (Sources_ == nil) { | |
9850 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; | |
9851 | [Metadata_ setObject:Sources_ forKey:@"Sources"]; | |
6d9712c4 | 9852 | } |
7376b55c | 9853 | /* }}} */ |
b4d89997 | 9854 | |
94b0b3e5 JF |
9855 | _trace(); |
9856 | MetaFile_.Open("/var/lib/cydia/metadata.cb0"); | |
9857 | _trace(); | |
9858 | ||
9859 | if (Packages_ != nil) { | |
c65611b9 JF |
9860 | bool fail(false); |
9861 | CFDictionaryApplyFunction((CFDictionaryRef) Packages_, &PackageImport, &fail); | |
94b0b3e5 | 9862 | _trace(); |
c65611b9 JF |
9863 | |
9864 | if (!fail) { | |
9865 | [Metadata_ removeObjectForKey:@"Packages"]; | |
9866 | Packages_ = nil; | |
9867 | Changed_ = true; | |
9868 | } | |
94b0b3e5 JF |
9869 | } |
9870 | ||
d791dce4 JF |
9871 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
9872 | ||
d71f3a07 | 9873 | #define MobileSubstrate_(name) \ |
d13577cf JF |
9874 | if (substrate && access("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", F_OK) == 0) { \ |
9875 | void *handle(dlopen("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", RTLD_LAZY | RTLD_GLOBAL)); \ | |
9876 | if (handle == NULL) \ | |
9877 | NSLog(@"%s", dlerror()); \ | |
9878 | } | |
d71f3a07 JF |
9879 | |
9880 | MobileSubstrate_(Activator) | |
9881 | MobileSubstrate_(libstatusbar) | |
9882 | MobileSubstrate_(SimulatedKeyEvents) | |
9883 | MobileSubstrate_(WinterBoard) | |
9884 | ||
9dd60d81 JF |
9885 | /*if (substrate && access("/Library/MobileSubstrate/MobileSubstrate.dylib", F_OK) == 0) |
9886 | dlopen("/Library/MobileSubstrate/MobileSubstrate.dylib", RTLD_LAZY | RTLD_GLOBAL);*/ | |
dddbc481 | 9887 | |
01d93940 JF |
9888 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
9889 | ||
3b0f10b0 | 9890 | if (access("/tmp/.cydia.fw", F_OK) == 0) { |
ea173384 | 9891 | unlink("/tmp/.cydia.fw"); |
3b0f10b0 | 9892 | goto firmware; |
a4d8c84e | 9893 | } else if (access("/User", F_OK) != 0 || version < 4) { |
3b0f10b0 | 9894 | firmware: |
d6dad1b4 | 9895 | _trace(); |
26c2dd8c | 9896 | system("/usr/libexec/cydia/firmware.sh"); |
d6dad1b4 JF |
9897 | _trace(); |
9898 | } | |
9e98e020 | 9899 | |
87f46a96 JF |
9900 | _assert([[NSFileManager defaultManager] |
9901 | createDirectoryAtPath:@"/var/cache/apt/archives/partial" | |
9902 | withIntermediateDirectories:YES | |
9903 | attributes:nil | |
9904 | error:NULL | |
9905 | ]); | |
9906 | ||
7376b55c JF |
9907 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
9908 | if (unlink("/var/cache/apt/pkgcache.bin") == -1) | |
9909 | _assert(errno == ENOENT); | |
9910 | if (unlink("/var/cache/apt/srcpkgcache.bin") == -1) | |
9911 | _assert(errno == ENOENT); | |
9912 | } | |
9913 | ||
59dbe296 | 9914 | /* APT Initialization {{{ */ |
b1ce61ec JF |
9915 | _assert(pkgInitConfig(*_config)); |
9916 | _assert(pkgInitSystem(*_config, _system)); | |
9917 | ||
9918 | if (lang != NULL) | |
9919 | _config->Set("APT::Acquire::Translation", lang); | |
cb94ff21 JF |
9920 | |
9921 | // XXX: this timeout might be important :( | |
9922 | //_config->Set("Acquire::http::Timeout", 15); | |
9923 | ||
7623f855 | 9924 | _config->Set("Acquire::http::MaxParallel", 3); |
59dbe296 | 9925 | /* }}} */ |
7376b55c | 9926 | /* Color Choices {{{ */ |
36bb2ca2 JF |
9927 | space_ = CGColorSpaceCreateDeviceRGB(); |
9928 | ||
f641a0e5 | 9929 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
77fcccaf | 9930 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
36bb2ca2 | 9931 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
baf80942 | 9932 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
36bb2ca2 | 9933 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
7b0ce2da | 9934 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
3bd1c2a2 JF |
9935 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
9936 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
9937 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
59dbe296 | 9938 | |
dc63e78f JF |
9939 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
9940 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
7376b55c | 9941 | /* }}}*/ |
7376b55c | 9942 | /* UIKit Configuration {{{ */ |
f79a4512 JF |
9943 | void (*$GSFontSetUseLegacyFontMetrics)(BOOL)(reinterpret_cast<void (*)(BOOL)>(dlsym(RTLD_DEFAULT, "GSFontSetUseLegacyFontMetrics"))); |
9944 | if ($GSFontSetUseLegacyFontMetrics != NULL) | |
9945 | $GSFontSetUseLegacyFontMetrics(YES); | |
7b0ce2da | 9946 | |
600d005d JF |
9947 | // XXX: I have a feeling this was important |
9948 | //UIKeyboardDisableAutomaticAppearance(); | |
7376b55c | 9949 | /* }}} */ |
87f46a96 | 9950 | |
670a0494 | 9951 | Colon_ = UCLocalize("COLON_DELIMITED"); |
72fb3616 | 9952 | Elision_ = UCLocalize("ELISION"); |
670a0494 JF |
9953 | Error_ = UCLocalize("ERROR"); |
9954 | Warning_ = UCLocalize("WARNING"); | |
9955 | ||
d6dad1b4 | 9956 | _trace(); |
77df4f82 | 9957 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
36bb2ca2 JF |
9958 | |
9959 | CGColorSpaceRelease(space_); | |
199d0ba5 | 9960 | CFRelease(Locale_); |
36bb2ca2 | 9961 | |
d13edf44 | 9962 | [pool release]; |
36bb2ca2 | 9963 | return value; |
6d166849 | 9964 | } |