]> git.saurik.com Git - cydia.git/blob - Cydia.mm
I hate doing this.
[cydia.git] / Cydia.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008 Jay Freeman (saurik)
3 */
4
5 /*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /* #include Directives {{{ */
39 #include <objc/objc.h>
40 #include <objc/runtime.h>
41
42 #include <CoreGraphics/CoreGraphics.h>
43 #include <GraphicsServices/GraphicsServices.h>
44 #include <Foundation/Foundation.h>
45 #include <UIKit/UIKit.h>
46 #include <WebCore/DOMHTML.h>
47
48 #import "BrowserView.h"
49 #import "ResetView.h"
50 #import "UICaboodle.h"
51
52 #include <WebKit/WebFrame.h>
53 #include <WebKit/WebView.h>
54
55 #include <sstream>
56 #include <string>
57
58 #include <ext/stdio_filebuf.h>
59
60 #include <apt-pkg/acquire.h>
61 #include <apt-pkg/acquire-item.h>
62 #include <apt-pkg/algorithms.h>
63 #include <apt-pkg/cachefile.h>
64 #include <apt-pkg/clean.h>
65 #include <apt-pkg/configuration.h>
66 #include <apt-pkg/debmetaindex.h>
67 #include <apt-pkg/error.h>
68 #include <apt-pkg/init.h>
69 #include <apt-pkg/pkgrecords.h>
70 #include <apt-pkg/sourcelist.h>
71 #include <apt-pkg/sptr.h>
72
73 #include <sys/sysctl.h>
74 #include <notify.h>
75
76 extern "C" {
77 #include <mach-o/nlist.h>
78 }
79
80 #include <cstdio>
81 #include <cstdlib>
82 #include <cstring>
83
84 #include <errno.h>
85 #include <pcre.h>
86 /* }}} */
87
88 /* iPhoneOS 2.0 Compatibility {{{ */
89 #ifdef __OBJC2__
90 @interface UICGColor : NSObject {
91 }
92
93 - (id) initWithCGColor:(CGColorRef)color;
94 @end
95
96 @interface UIFont {
97 }
98
99 - (UIFont *) fontWithSize:(CGFloat)size;
100 @end
101
102 @interface NSObject (iPhoneOS)
103 - (CGColorRef) cgColor;
104 - (CGColorRef) CGColor;
105 - (void) set;
106 @end
107
108 @implementation NSObject (iPhoneOS)
109
110 - (CGColorRef) cgColor {
111 return [self CGColor];
112 }
113
114 - (CGColorRef) CGColor {
115 return (CGColorRef) self;
116 }
117
118 - (void) set {
119 [[[[objc_getClass("UICGColor") alloc] initWithCGColor:[self CGColor]] autorelease] set];
120 }
121
122 @end
123
124 @interface UITextView (iPhoneOS)
125 - (void) setTextSize:(float)size;
126 @end
127
128 @implementation UITextView (iPhoneOS)
129
130 - (void) setTextSize:(float)size {
131 [self setFont:[[self font] fontWithSize:size]];
132 }
133
134 @end
135 #endif
136 /* }}} */
137
138 #ifdef __OBJC2__
139 typedef enum {
140 kUIProgressIndicatorStyleMediumWhite = 1,
141 kUIProgressIndicatorStyleSmallWhite = 0,
142 kUIProgressIndicatorStyleSmallBlack = 4
143 } UIProgressIndicatorStyle;
144 #else
145 typedef enum {
146 kUIProgressIndicatorStyleMediumWhite = 0,
147 kUIProgressIndicatorStyleSmallWhite = 2,
148 kUIProgressIndicatorStyleSmallBlack = 3
149 } UIProgressIndicatorStyle;
150 #endif
151
152 typedef enum {
153 kUIControlEventMouseDown = 1 << 0,
154 kUIControlEventMouseMovedInside = 1 << 2, // mouse moved inside control target
155 kUIControlEventMouseMovedOutside = 1 << 3, // mouse moved outside control target
156 kUIControlEventMouseUpInside = 1 << 6, // mouse up inside control target
157 kUIControlEventMouseUpOutside = 1 << 7, // mouse up outside control target
158 kUIControlAllEvents = (kUIControlEventMouseDown | kUIControlEventMouseMovedInside | kUIControlEventMouseMovedOutside | kUIControlEventMouseUpInside | kUIControlEventMouseUpOutside)
159 } UIControlEventMasks;
160
161 @interface NSString (UIKit)
162 - (NSString *) stringByAddingPercentEscapes;
163 - (NSString *) stringByReplacingCharacter:(unsigned short)arg0 withCharacter:(unsigned short)arg1;
164 @end
165
166 @interface NSString (Cydia)
167 + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length;
168 - (NSComparisonResult) compareByPath:(NSString *)other;
169 @end
170
171 @implementation NSString (Cydia)
172
173 + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length {
174 char data[length + 1];
175 memcpy(data, bytes, length);
176 data[length] = '\0';
177 return [NSString stringWithUTF8String:data];
178 }
179
180 - (NSComparisonResult) compareByPath:(NSString *)other {
181 NSString *prefix = [self commonPrefixWithString:other options:0];
182 size_t length = [prefix length];
183
184 NSRange lrange = NSMakeRange(length, [self length] - length);
185 NSRange rrange = NSMakeRange(length, [other length] - length);
186
187 lrange = [self rangeOfString:@"/" options:0 range:lrange];
188 rrange = [other rangeOfString:@"/" options:0 range:rrange];
189
190 NSComparisonResult value;
191
192 if (lrange.location == NSNotFound && rrange.location == NSNotFound)
193 value = NSOrderedSame;
194 else if (lrange.location == NSNotFound)
195 value = NSOrderedAscending;
196 else if (rrange.location == NSNotFound)
197 value = NSOrderedDescending;
198 else
199 value = NSOrderedSame;
200
201 NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] :
202 [self substringWithRange:NSMakeRange(length, lrange.location - length)];
203 NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] :
204 [other substringWithRange:NSMakeRange(length, rrange.location - length)];
205
206 NSComparisonResult result = [lpath compare:rpath];
207 return result == NSOrderedSame ? value : result;
208 }
209
210 @end
211
212 /* Perl-Compatible RegEx {{{ */
213 class Pcre {
214 private:
215 pcre *code_;
216 pcre_extra *study_;
217 int capture_;
218 int *matches_;
219 const char *data_;
220
221 public:
222 Pcre(const char *regex) :
223 study_(NULL)
224 {
225 const char *error;
226 int offset;
227 code_ = pcre_compile(regex, 0, &error, &offset, NULL);
228
229 if (code_ == NULL) {
230 fprintf(stderr, "%d:%s\n", offset, error);
231 _assert(false);
232 }
233
234 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
235 matches_ = new int[(capture_ + 1) * 3];
236 }
237
238 ~Pcre() {
239 pcre_free(code_);
240 delete matches_;
241 }
242
243 NSString *operator [](size_t match) {
244 return [NSString stringWithUTF8Bytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2])];
245 }
246
247 bool operator ()(NSString *data) {
248 // XXX: length is for characters, not for bytes
249 return operator ()([data UTF8String], [data length]);
250 }
251
252 bool operator ()(const char *data, size_t size) {
253 data_ = data;
254 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
255 }
256 };
257 /* }}} */
258 /* Mime Addresses {{{ */
259 Pcre email_r("^\"?(.*)\"? <([^>]*)>$");
260
261 @interface Address : NSObject {
262 NSString *name_;
263 NSString *email_;
264 }
265
266 - (NSString *) name;
267 - (NSString *) email;
268
269 + (Address *) addressWithString:(NSString *)string;
270 - (Address *) initWithString:(NSString *)string;
271 @end
272
273 @implementation Address
274
275 - (void) dealloc {
276 [name_ release];
277 if (email_ != nil)
278 [email_ release];
279 [super dealloc];
280 }
281
282 - (NSString *) name {
283 return name_;
284 }
285
286 - (NSString *) email {
287 return email_;
288 }
289
290 + (Address *) addressWithString:(NSString *)string {
291 return [[[Address alloc] initWithString:string] autorelease];
292 }
293
294 - (Address *) initWithString:(NSString *)string {
295 if ((self = [super init]) != nil) {
296 const char *data = [string UTF8String];
297 size_t size = [string length];
298
299 if (email_r(data, size)) {
300 name_ = [email_r[1] retain];
301 email_ = [email_r[2] retain];
302 } else {
303 name_ = [[NSString alloc]
304 initWithBytes:data
305 length:size
306 encoding:kCFStringEncodingUTF8
307 ];
308
309 email_ = nil;
310 }
311 } return self;
312 }
313
314 @end
315 /* }}} */
316 /* CoreGraphics Primitives {{{ */
317 class CGColor {
318 private:
319 CGColorRef color_;
320
321 public:
322 CGColor() :
323 color_(NULL)
324 {
325 }
326
327 CGColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) :
328 color_(NULL)
329 {
330 Set(space, red, green, blue, alpha);
331 }
332
333 void Clear() {
334 if (color_ != NULL)
335 CGColorRelease(color_);
336 }
337
338 ~CGColor() {
339 Clear();
340 }
341
342 void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) {
343 Clear();
344 float color[] = {red, green, blue, alpha};
345 color_ = CGColorCreate(space, color);
346 }
347
348 operator CGColorRef() {
349 return color_;
350 }
351 };
352
353 class GSFont {
354 private:
355 GSFontRef font_;
356
357 public:
358 ~GSFont() {
359 CFRelease(font_);
360 }
361 };
362 /* }}} */
363 /* Right Alignment {{{ */
364 @interface UIRightTextLabel : UITextLabel {
365 float _savedRightEdgeX;
366 BOOL _sizedtofit_flag;
367 }
368
369 - (void) setFrame:(CGRect)frame;
370 - (void) setText:(NSString *)text;
371 - (void) realignText;
372 @end
373
374 @implementation UIRightTextLabel
375
376 - (void) setFrame:(CGRect)frame {
377 [super setFrame:frame];
378 if (_sizedtofit_flag == NO) {
379 _savedRightEdgeX = frame.origin.x;
380 [self realignText];
381 }
382 }
383
384 - (void) setText:(NSString *)text {
385 [super setText:text];
386 [self realignText];
387 }
388
389 - (void) realignText {
390 CGRect oldFrame = [self frame];
391
392 _sizedtofit_flag = YES;
393 [self sizeToFit]; // shrink down size so I can right align it
394
395 CGRect newFrame = [self frame];
396
397 oldFrame.origin.x = _savedRightEdgeX - newFrame.size.width;
398 oldFrame.size.width = newFrame.size.width;
399 [super setFrame:oldFrame];
400 _sizedtofit_flag = NO;
401 }
402
403 @end
404 /* }}} */
405
406 /* Random Global Variables {{{ */
407 static const int PulseInterval_ = 50000;
408 static const int ButtonBarHeight_ = 48;
409 static const float KeyboardTime_ = 0.4f;
410 static const char * const SpringBoard_ = "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist";
411
412 #ifndef Cydia_
413 #define Cydia_ ""
414 #endif
415
416 static CGColor Blueish_;
417 static CGColor Black_;
418 static CGColor Clear_;
419 static CGColor Red_;
420 static CGColor White_;
421
422 static NSString *Home_;
423 static BOOL Sounds_Keyboard_;
424
425 static BOOL Advanced_;
426 static BOOL Loaded_;
427 static BOOL Ignored_;
428
429 const char *Firmware_ = NULL;
430 const char *Machine_ = NULL;
431 const char *SerialNumber_ = NULL;
432
433 unsigned Major_;
434 unsigned Minor_;
435 unsigned BugFix_;
436
437 CFLocaleRef Locale_;
438 CGColorSpaceRef space_;
439
440 #define FW_LEAST(major, minor, bugfix) \
441 (major < Major_ || major == Major_ && \
442 (minor < Minor_ || minor == Minor_ && \
443 bugfix <= BugFix_))
444
445 bool bootstrap_;
446 bool reload_;
447
448 static NSMutableDictionary *Metadata_;
449 static NSMutableDictionary *Packages_;
450 static bool Changed_;
451 static NSDate *now_;
452
453 NSString *GetLastUpdate() {
454 NSDate *update = [Metadata_ objectForKey:@"LastUpdate"];
455
456 if (update == nil)
457 return @"Never or Unknown";
458
459 CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle);
460 CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update);
461
462 CFRelease(formatter);
463
464 return [(NSString *) formatted autorelease];
465 }
466 /* }}} */
467 /* Display Helpers {{{ */
468 inline float Interpolate(float begin, float end, float fraction) {
469 return (end - begin) * fraction + begin;
470 }
471
472 NSString *SizeString(double size) {
473 unsigned power = 0;
474 while (size > 1024) {
475 size /= 1024;
476 ++power;
477 }
478
479 static const char *powers_[] = {"B", "kB", "MB", "GB"};
480
481 return [NSString stringWithFormat:@"%.1f%s", size, powers_[power]];
482 }
483
484 NSString *StripVersion(NSString *version) {
485 NSRange colon = [version rangeOfString:@":"];
486 if (colon.location != NSNotFound)
487 version = [version substringFromIndex:(colon.location + 1)];
488 return version;
489 }
490
491 static const float TextViewOffset_ = 22;
492
493 UITextView *GetTextView(NSString *value, float left, bool html) {
494 UITextView *text([[[UITextView alloc] initWithFrame:CGRectMake(left, 3, 310 - left, 1000)] autorelease]);
495 [text setEditable:NO];
496 [text setTextSize:16];
497 /*if (html)
498 [text setHTML:value];
499 else*/
500 [text setText:value];
501 [text setEnabled:NO];
502
503 [text setBackgroundColor:Clear_];
504
505 CGRect frame = [text frame];
506 [text setFrame:frame];
507 CGRect rect = [text visibleTextRect];
508 frame.size.height = rect.size.height;
509 [text setFrame:frame];
510
511 return text;
512 }
513
514 NSString *Simplify(NSString *title) {
515 const char *data = [title UTF8String];
516 size_t size = [title length];
517
518 Pcre title_r("^(.*?)( \\(.*\\))?$");
519 if (title_r(data, size))
520 return title_r[1];
521 else
522 return title;
523 }
524 /* }}} */
525
526 /* Delegate Prototypes {{{ */
527 @class Package;
528 @class Source;
529
530 @interface NSObject (ProgressDelegate)
531 @end
532
533 @implementation NSObject(ProgressDelegate)
534
535 - (void) _setProgressError:(NSArray *)args {
536 [self performSelector:@selector(setProgressError:forPackage:)
537 withObject:[args objectAtIndex:0]
538 withObject:([args count] == 1 ? nil : [args objectAtIndex:1])
539 ];
540 }
541
542 @end
543
544 @protocol ProgressDelegate
545 - (void) setProgressError:(NSString *)error forPackage:(NSString *)id;
546 - (void) setProgressTitle:(NSString *)title;
547 - (void) setProgressPercent:(float)percent;
548 - (void) addProgressOutput:(NSString *)output;
549 @end
550
551 @protocol ConfigurationDelegate
552 - (void) repairWithSelector:(SEL)selector;
553 - (void) setConfigurationData:(NSString *)data;
554 @end
555
556 @protocol CydiaDelegate
557 - (void) installPackage:(Package *)package;
558 - (void) removePackage:(Package *)package;
559 - (void) slideUp:(UIAlertSheet *)alert;
560 - (void) distUpgrade;
561 @end
562 /* }}} */
563
564 /* Status Delegation {{{ */
565 class Status :
566 public pkgAcquireStatus
567 {
568 private:
569 _transient NSObject<ProgressDelegate> *delegate_;
570
571 public:
572 Status() :
573 delegate_(nil)
574 {
575 }
576
577 void setDelegate(id delegate) {
578 delegate_ = delegate;
579 }
580
581 virtual bool MediaChange(std::string media, std::string drive) {
582 return false;
583 }
584
585 virtual void IMSHit(pkgAcquire::ItemDesc &item) {
586 }
587
588 virtual void Fetch(pkgAcquire::ItemDesc &item) {
589 [delegate_ setProgressTitle:[NSString stringWithUTF8String:("Downloading " + item.ShortDesc).c_str()]];
590 }
591
592 virtual void Done(pkgAcquire::ItemDesc &item) {
593 }
594
595 virtual void Fail(pkgAcquire::ItemDesc &item) {
596 if (
597 item.Owner->Status == pkgAcquire::Item::StatIdle ||
598 item.Owner->Status == pkgAcquire::Item::StatDone
599 )
600 return;
601
602 [delegate_ performSelectorOnMainThread:@selector(_setProgressError:)
603 withObject:[NSArray arrayWithObjects:[NSString stringWithUTF8String:item.Owner->ErrorText.c_str()], nil]
604 waitUntilDone:YES
605 ];
606 }
607
608 virtual bool Pulse(pkgAcquire *Owner) {
609 bool value = pkgAcquireStatus::Pulse(Owner);
610
611 float percent(
612 double(CurrentBytes + CurrentItems) /
613 double(TotalBytes + TotalItems)
614 );
615
616 [delegate_ setProgressPercent:percent];
617 return value;
618 }
619
620 virtual void Start() {
621 }
622
623 virtual void Stop() {
624 }
625 };
626 /* }}} */
627 /* Progress Delegation {{{ */
628 class Progress :
629 public OpProgress
630 {
631 private:
632 _transient id<ProgressDelegate> delegate_;
633
634 protected:
635 virtual void Update() {
636 [delegate_ setProgressTitle:[NSString stringWithUTF8String:Op.c_str()]];
637 [delegate_ setProgressPercent:(Percent / 100)];
638 }
639
640 public:
641 Progress() :
642 delegate_(nil)
643 {
644 }
645
646 void setDelegate(id delegate) {
647 delegate_ = delegate;
648 }
649
650 virtual void Done() {
651 [delegate_ setProgressPercent:1];
652 }
653 };
654 /* }}} */
655
656 /* Database Interface {{{ */
657 @interface Database : NSObject {
658 pkgCacheFile cache_;
659 pkgDepCache::Policy *policy_;
660 pkgRecords *records_;
661 pkgProblemResolver *resolver_;
662 pkgAcquire *fetcher_;
663 FileFd *lock_;
664 SPtr<pkgPackageManager> manager_;
665 pkgSourceList *list_;
666
667 NSMutableDictionary *sources_;
668 NSMutableArray *packages_;
669
670 _transient NSObject<ConfigurationDelegate, ProgressDelegate> *delegate_;
671 Status status_;
672 Progress progress_;
673
674 int cydiafd_;
675 int statusfd_;
676 FILE *input_;
677 }
678
679 - (void) _readCydia:(NSNumber *)fd;
680 - (void) _readStatus:(NSNumber *)fd;
681 - (void) _readOutput:(NSNumber *)fd;
682
683 - (FILE *) input;
684
685 - (Package *) packageWithName:(NSString *)name;
686
687 - (Database *) init;
688 - (pkgCacheFile &) cache;
689 - (pkgDepCache::Policy *) policy;
690 - (pkgRecords *) records;
691 - (pkgProblemResolver *) resolver;
692 - (pkgAcquire &) fetcher;
693 - (NSArray *) packages;
694 - (void) reloadData;
695
696 - (void) configure;
697 - (void) prepare;
698 - (void) perform;
699 - (void) upgrade;
700 - (void) update;
701
702 - (void) updateWithStatus:(Status &)status;
703
704 - (void) setDelegate:(id)delegate;
705 - (Source *) getSource:(const pkgCache::PkgFileIterator &)file;
706 @end
707 /* }}} */
708
709 /* Source Class {{{ */
710 @interface Source : NSObject {
711 NSString *description_;
712 NSString *label_;
713 NSString *origin_;
714
715 NSString *uri_;
716 NSString *distribution_;
717 NSString *type_;
718 NSString *version_;
719
720 NSString *defaultIcon_;
721
722 BOOL trusted_;
723 }
724
725 - (Source *) initWithMetaIndex:(metaIndex *)index;
726
727 - (BOOL) trusted;
728
729 - (NSString *) uri;
730 - (NSString *) distribution;
731 - (NSString *) type;
732
733 - (NSString *) description;
734 - (NSString *) label;
735 - (NSString *) origin;
736 - (NSString *) version;
737
738 - (NSString *) defaultIcon;
739 @end
740
741 @implementation Source
742
743 - (void) dealloc {
744 [uri_ release];
745 [distribution_ release];
746 [type_ release];
747
748 if (description_ != nil)
749 [description_ release];
750 if (label_ != nil)
751 [label_ release];
752 if (origin_ != nil)
753 [origin_ release];
754 if (version_ != nil)
755 [version_ release];
756 if (defaultIcon_ != nil)
757 [defaultIcon_ release];
758
759 [super dealloc];
760 }
761
762 - (Source *) initWithMetaIndex:(metaIndex *)index {
763 if ((self = [super init]) != nil) {
764 trusted_ = index->IsTrusted();
765
766 uri_ = [[NSString stringWithUTF8String:index->GetURI().c_str()] retain];
767 distribution_ = [[NSString stringWithUTF8String:index->GetDist().c_str()] retain];
768 type_ = [[NSString stringWithUTF8String:index->GetType()] retain];
769
770 description_ = nil;
771 label_ = nil;
772 origin_ = nil;
773 version_ = nil;
774 defaultIcon_ = nil;
775
776 debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index));
777 if (dindex != NULL) {
778 std::ifstream release(dindex->MetaIndexFile("Release").c_str());
779 std::string line;
780 while (std::getline(release, line)) {
781 std::string::size_type colon(line.find(':'));
782 if (colon == std::string::npos)
783 continue;
784
785 std::string name(line.substr(0, colon));
786 std::string value(line.substr(colon + 1));
787 while (!value.empty() && value[0] == ' ')
788 value = value.substr(1);
789
790 if (name == "Default-Icon")
791 defaultIcon_ = [[NSString stringWithUTF8String:value.c_str()] retain];
792 else if (name == "Description")
793 description_ = [[NSString stringWithUTF8String:value.c_str()] retain];
794 else if (name == "Label")
795 label_ = [[NSString stringWithUTF8String:value.c_str()] retain];
796 else if (name == "Origin")
797 origin_ = [[NSString stringWithUTF8String:value.c_str()] retain];
798 else if (name == "Version")
799 version_ = [[NSString stringWithUTF8String:value.c_str()] retain];
800 }
801 }
802 } return self;
803 }
804
805 - (BOOL) trusted {
806 return trusted_;
807 }
808
809 - (NSString *) uri {
810 return uri_;
811 }
812
813 - (NSString *) distribution {
814 return distribution_;
815 }
816
817 - (NSString *) type {
818 return type_;
819 }
820
821 - (NSString *) description {
822 return description_;
823 }
824
825 - (NSString *) label {
826 return label_;
827 }
828
829 - (NSString *) origin {
830 return origin_;
831 }
832
833 - (NSString *) version {
834 return version_;
835 }
836
837 - (NSString *) defaultIcon {
838 return defaultIcon_;
839 }
840
841 @end
842 /* }}} */
843 /* Relationship Class {{{ */
844 @interface Relationship : NSObject {
845 NSString *type_;
846 NSString *id_;
847 }
848
849 - (NSString *) type;
850 - (NSString *) id;
851 - (NSString *) name;
852
853 @end
854
855 @implementation Relationship
856
857 - (void) dealloc {
858 [type_ release];
859 [id_ release];
860 [super dealloc];
861 }
862
863 - (NSString *) type {
864 return type_;
865 }
866
867 - (NSString *) id {
868 return id_;
869 }
870
871 - (NSString *) name {
872 _assert(false);
873 return nil;
874 }
875
876 @end
877 /* }}} */
878 /* Package Class {{{ */
879 NSString *Scour(const char *field, const char *begin, const char *end) {
880 size_t i(0), l(strlen(field));
881
882 for (;;) {
883 const char *name = begin + i;
884 const char *colon = name + l;
885 const char *value = colon + 1;
886
887 if (
888 value < end &&
889 *colon == ':' &&
890 memcmp(name, field, l) == 0
891 ) {
892 while (value != end && value[0] == ' ')
893 ++value;
894 const char *line = std::find(value, end, '\n');
895 while (line != value && line[-1] == ' ')
896 --line;
897
898 return [NSString stringWithUTF8Bytes:value length:(line - value)];
899 } else {
900 begin = std::find(begin, end, '\n');
901 if (begin == end)
902 return nil;
903 ++begin;
904 }
905 }
906 }
907
908 @interface Package : NSObject {
909 pkgCache::PkgIterator iterator_;
910 _transient Database *database_;
911 pkgCache::VerIterator version_;
912 pkgCache::VerFileIterator file_;
913
914 Source *source_;
915 bool cached_;
916
917 NSString *latest_;
918 NSString *installed_;
919
920 NSString *id_;
921 NSString *name_;
922 NSString *tagline_;
923 NSString *icon_;
924 NSString *website_;
925 Address *sponsor_;
926 Address *author_;
927
928 NSArray *relationships_;
929 }
930
931 - (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database;
932 + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database;
933
934 - (pkgCache::PkgIterator) iterator;
935
936 - (NSString *) section;
937 - (Address *) maintainer;
938 - (size_t) size;
939 - (NSString *) description;
940 - (NSString *) index;
941
942 - (NSDate *) seen;
943
944 - (NSString *) latest;
945 - (NSString *) installed;
946
947 - (BOOL) valid;
948 - (BOOL) upgradableAndEssential:(BOOL)essential;
949 - (BOOL) essential;
950 - (BOOL) broken;
951
952 - (BOOL) half;
953 - (BOOL) halfConfigured;
954 - (BOOL) halfInstalled;
955 - (BOOL) hasMode;
956 - (NSString *) mode;
957
958 - (NSString *) id;
959 - (NSString *) name;
960 - (NSString *) tagline;
961 - (NSString *) icon;
962 - (NSString *) website;
963 - (Address *) author;
964
965 - (NSArray *) relationships;
966
967 - (Source *) source;
968
969 - (BOOL) matches:(NSString *)text;
970
971 - (NSComparisonResult) compareByName:(Package *)package;
972 - (NSComparisonResult) compareBySection:(Package *)package;
973 - (NSComparisonResult) compareBySectionAndName:(Package *)package;
974 - (NSComparisonResult) compareForChanges:(Package *)package;
975
976 - (void) install;
977 - (void) remove;
978
979 - (NSNumber *) isSearchedForBy:(NSString *)search;
980 - (NSNumber *) isInstalledInSection:(NSString *)section;
981 - (NSNumber *) isUninstalledInSection:(NSString *)section;
982
983 @end
984
985 @implementation Package
986
987 - (void) dealloc {
988 if (source_ != nil)
989 [source_ release];
990
991 [latest_ release];
992 if (installed_ != nil)
993 [installed_ release];
994
995 [id_ release];
996 if (name_ != nil)
997 [name_ release];
998 [tagline_ release];
999 if (icon_ != nil)
1000 [icon_ release];
1001 if (website_ != nil)
1002 [website_ release];
1003 if (sponsor_ != nil)
1004 [sponsor_ release];
1005 if (author_ != nil)
1006 [author_ release];
1007
1008 if (relationships_ != nil)
1009 [relationships_ release];
1010
1011 [super dealloc];
1012 }
1013
1014 - (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database {
1015 if ((self = [super init]) != nil) {
1016 iterator_ = iterator;
1017 database_ = database;
1018
1019 version_ = [database_ policy]->GetCandidateVer(iterator_);
1020 latest_ = version_.end() ? nil : [StripVersion([NSString stringWithUTF8String:version_.VerStr()]) retain];
1021
1022 if (!version_.end())
1023 file_ = version_.FileList();
1024 else {
1025 pkgCache &cache([database_ cache]);
1026 file_ = pkgCache::VerFileIterator(cache, cache.VerFileP);
1027 }
1028
1029 pkgCache::VerIterator current = iterator_.CurrentVer();
1030 installed_ = current.end() ? nil : [StripVersion([NSString stringWithUTF8String:current.VerStr()]) retain];
1031
1032 id_ = [[[NSString stringWithUTF8String:iterator_.Name()] lowercaseString] retain];
1033
1034 if (!file_.end()) {
1035 pkgRecords::Parser *parser = &[database_ records]->Lookup(file_);
1036
1037 const char *begin, *end;
1038 parser->GetRec(begin, end);
1039
1040 name_ = Scour("Name", begin, end);
1041 if (name_ != nil)
1042 name_ = [name_ retain];
1043 tagline_ = [[NSString stringWithUTF8String:parser->ShortDesc().c_str()] retain];
1044 icon_ = Scour("Icon", begin, end);
1045 if (icon_ != nil)
1046 icon_ = [icon_ retain];
1047 website_ = Scour("Homepage", begin, end);
1048 if (website_ == nil)
1049 website_ = Scour("Website", begin, end);
1050 if (website_ != nil)
1051 website_ = [website_ retain];
1052 NSString *sponsor = Scour("Sponsor", begin, end);
1053 if (sponsor != nil)
1054 sponsor_ = [[Address addressWithString:sponsor] retain];
1055 NSString *author = Scour("Author", begin, end);
1056 if (author != nil)
1057 author_ = [[Address addressWithString:author] retain];
1058 }
1059
1060 NSMutableDictionary *metadata = [Packages_ objectForKey:id_];
1061 if (metadata == nil || [metadata count] == 0) {
1062 metadata = [NSMutableDictionary dictionaryWithObjectsAndKeys:
1063 now_, @"FirstSeen",
1064 nil];
1065
1066 [Packages_ setObject:metadata forKey:id_];
1067 Changed_ = true;
1068 }
1069 } return self;
1070 }
1071
1072 + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database {
1073 return [[[Package alloc]
1074 initWithIterator:iterator
1075 database:database
1076 ] autorelease];
1077 }
1078
1079 - (pkgCache::PkgIterator) iterator {
1080 return iterator_;
1081 }
1082
1083 - (NSString *) section {
1084 const char *section = iterator_.Section();
1085 return section == NULL ? nil : [[NSString stringWithUTF8String:section] stringByReplacingCharacter:'_' withCharacter:' '];
1086 }
1087
1088 - (Address *) maintainer {
1089 if (file_.end())
1090 return nil;
1091 pkgRecords::Parser *parser = &[database_ records]->Lookup(file_);
1092 return [Address addressWithString:[NSString stringWithUTF8String:parser->Maintainer().c_str()]];
1093 }
1094
1095 - (size_t) size {
1096 return version_.end() ? 0 : version_->InstalledSize;
1097 }
1098
1099 - (NSString *) description {
1100 if (file_.end())
1101 return nil;
1102 pkgRecords::Parser *parser = &[database_ records]->Lookup(file_);
1103 NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]);
1104
1105 NSArray *lines = [description componentsSeparatedByString:@"\n"];
1106 NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)];
1107 if ([lines count] < 2)
1108 return nil;
1109
1110 NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
1111 for (size_t i(1); i != [lines count]; ++i) {
1112 NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace];
1113 [trimmed addObject:trim];
1114 }
1115
1116 return [trimmed componentsJoinedByString:@"\n"];
1117 }
1118
1119 - (NSString *) index {
1120 NSString *index = [[[self name] substringToIndex:1] uppercaseString];
1121 return [index length] != 0 && isalpha([index characterAtIndex:0]) ? index : @"123";
1122 }
1123
1124 - (NSDate *) seen {
1125 return [[Packages_ objectForKey:id_] objectForKey:@"FirstSeen"];
1126 }
1127
1128 - (NSString *) latest {
1129 return latest_;
1130 }
1131
1132 - (NSString *) installed {
1133 return installed_;
1134 }
1135
1136 - (BOOL) valid {
1137 return !version_.end();
1138 }
1139
1140 - (BOOL) upgradableAndEssential:(BOOL)essential {
1141 pkgCache::VerIterator current = iterator_.CurrentVer();
1142
1143 if (current.end())
1144 return essential && [self essential];
1145 else {
1146 pkgCache::VerIterator candidate = [database_ policy]->GetCandidateVer(iterator_);
1147 return !candidate.end() && candidate != current;
1148 }
1149 }
1150
1151 - (BOOL) essential {
1152 return (iterator_->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES;
1153 }
1154
1155 - (BOOL) broken {
1156 return [database_ cache][iterator_].InstBroken();
1157 }
1158
1159 - (BOOL) half {
1160 unsigned char current = iterator_->CurrentState;
1161 return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled;
1162 }
1163
1164 - (BOOL) halfConfigured {
1165 return iterator_->CurrentState == pkgCache::State::HalfConfigured;
1166 }
1167
1168 - (BOOL) halfInstalled {
1169 return iterator_->CurrentState == pkgCache::State::HalfInstalled;
1170 }
1171
1172 - (BOOL) hasMode {
1173 pkgDepCache::StateCache &state([database_ cache][iterator_]);
1174 return state.Mode != pkgDepCache::ModeKeep;
1175 }
1176
1177 - (NSString *) mode {
1178 pkgDepCache::StateCache &state([database_ cache][iterator_]);
1179
1180 switch (state.Mode) {
1181 case pkgDepCache::ModeDelete:
1182 if ((state.iFlags & pkgDepCache::Purge) != 0)
1183 return @"Purge";
1184 else
1185 return @"Remove";
1186 _assert(false);
1187 case pkgDepCache::ModeKeep:
1188 if ((state.iFlags & pkgDepCache::AutoKept) != 0)
1189 return nil;
1190 else
1191 return nil;
1192 _assert(false);
1193 case pkgDepCache::ModeInstall:
1194 if ((state.iFlags & pkgDepCache::ReInstall) != 0)
1195 return @"Reinstall";
1196 else switch (state.Status) {
1197 case -1:
1198 return @"Downgrade";
1199 case 0:
1200 return @"Install";
1201 case 1:
1202 return @"Upgrade";
1203 case 2:
1204 return @"New Install";
1205 default:
1206 _assert(false);
1207 }
1208 default:
1209 _assert(false);
1210 }
1211 }
1212
1213 - (NSString *) id {
1214 return id_;
1215 }
1216
1217 - (NSString *) name {
1218 return name_ == nil ? id_ : name_;
1219 }
1220
1221 - (NSString *) tagline {
1222 return tagline_;
1223 }
1224
1225 - (NSString *) icon {
1226 return icon_;
1227 }
1228
1229 - (NSString *) website {
1230 return website_;
1231 }
1232
1233 - (Address *) sponsor {
1234 return sponsor_;
1235 }
1236
1237 - (Address *) author {
1238 return author_;
1239 }
1240
1241 - (NSArray *) relationships {
1242 return relationships_;
1243 }
1244
1245 - (Source *) source {
1246 if (!cached_) {
1247 source_ = file_.end() ? nil : [[database_ getSource:file_.File()] retain];
1248 cached_ = true;
1249 }
1250
1251 return source_;
1252 }
1253
1254 - (BOOL) matches:(NSString *)text {
1255 if (text == nil)
1256 return NO;
1257
1258 NSRange range;
1259
1260 range = [[self id] rangeOfString:text options:NSCaseInsensitiveSearch];
1261 if (range.location != NSNotFound)
1262 return YES;
1263
1264 range = [[self name] rangeOfString:text options:NSCaseInsensitiveSearch];
1265 if (range.location != NSNotFound)
1266 return YES;
1267
1268 range = [[self tagline] rangeOfString:text options:NSCaseInsensitiveSearch];
1269 if (range.location != NSNotFound)
1270 return YES;
1271
1272 return NO;
1273 }
1274
1275 - (NSComparisonResult) compareByName:(Package *)package {
1276 NSString *lhs = [self name];
1277 NSString *rhs = [package name];
1278
1279 if ([lhs length] != 0 && [rhs length] != 0) {
1280 unichar lhc = [lhs characterAtIndex:0];
1281 unichar rhc = [rhs characterAtIndex:0];
1282
1283 if (isalpha(lhc) && !isalpha(rhc))
1284 return NSOrderedAscending;
1285 else if (!isalpha(lhc) && isalpha(rhc))
1286 return NSOrderedDescending;
1287 }
1288
1289 return [lhs caseInsensitiveCompare:rhs];
1290 }
1291
1292 - (NSComparisonResult) compareBySection:(Package *)package {
1293 NSString *lhs = [self section];
1294 NSString *rhs = [package section];
1295
1296 if (lhs == NULL && rhs != NULL)
1297 return NSOrderedAscending;
1298 else if (lhs != NULL && rhs == NULL)
1299 return NSOrderedDescending;
1300 else if (lhs != NULL && rhs != NULL) {
1301 NSComparisonResult result = [lhs caseInsensitiveCompare:rhs];
1302 if (result != NSOrderedSame)
1303 return result;
1304 }
1305
1306 return NSOrderedSame;
1307 }
1308
1309 - (NSComparisonResult) compareBySectionAndName:(Package *)package {
1310 NSString *lhs = [self section];
1311 NSString *rhs = [package section];
1312
1313 if (lhs == NULL && rhs != NULL)
1314 return NSOrderedAscending;
1315 else if (lhs != NULL && rhs == NULL)
1316 return NSOrderedDescending;
1317 else if (lhs != NULL && rhs != NULL) {
1318 NSComparisonResult result = [lhs compare:rhs];
1319 if (result != NSOrderedSame)
1320 return result;
1321 }
1322
1323 return [self compareByName:package];
1324 }
1325
1326 - (NSComparisonResult) compareForChanges:(Package *)package {
1327 BOOL lhs = [self upgradableAndEssential:YES];
1328 BOOL rhs = [package upgradableAndEssential:YES];
1329
1330 if (lhs != rhs)
1331 return lhs ? NSOrderedAscending : NSOrderedDescending;
1332 else if (!lhs) {
1333 switch ([[self seen] compare:[package seen]]) {
1334 case NSOrderedAscending:
1335 return NSOrderedDescending;
1336 case NSOrderedSame:
1337 break;
1338 case NSOrderedDescending:
1339 return NSOrderedAscending;
1340 default:
1341 _assert(false);
1342 }
1343 }
1344
1345 return [self compareByName:package];
1346 }
1347
1348 - (void) install {
1349 pkgProblemResolver *resolver = [database_ resolver];
1350 resolver->Clear(iterator_);
1351 resolver->Protect(iterator_);
1352 pkgCacheFile &cache([database_ cache]);
1353 cache->MarkInstall(iterator_, false);
1354 pkgDepCache::StateCache &state((*cache)[iterator_]);
1355 if (!state.Install())
1356 cache->SetReInstall(iterator_, true);
1357 }
1358
1359 - (void) remove {
1360 pkgProblemResolver *resolver = [database_ resolver];
1361 resolver->Clear(iterator_);
1362 resolver->Protect(iterator_);
1363 resolver->Remove(iterator_);
1364 [database_ cache]->MarkDelete(iterator_, true);
1365 }
1366
1367 - (NSNumber *) isSearchedForBy:(NSString *)search {
1368 return [NSNumber numberWithBool:([self valid] && [self matches:search])];
1369 }
1370
1371 - (NSNumber *) isInstalledInSection:(NSString *)section {
1372 return [NSNumber numberWithBool:([self installed] != nil && (section == nil || [section isEqualToString:[self section]]))];
1373 }
1374
1375 - (NSNumber *) isUninstalledInSection:(NSString *)name {
1376 NSString *section = [self section];
1377
1378 return [NSNumber numberWithBool:([self valid] && [self installed] == nil && (
1379 (name == nil ||
1380 section == nil && [name length] == 0 ||
1381 [name isEqualToString:section])
1382 ))];
1383 }
1384
1385 @end
1386 /* }}} */
1387 /* Section Class {{{ */
1388 @interface Section : NSObject {
1389 NSString *name_;
1390 size_t row_;
1391 size_t count_;
1392 }
1393
1394 - (Section *) initWithName:(NSString *)name row:(size_t)row;
1395 - (NSString *) name;
1396 - (size_t) row;
1397 - (size_t) count;
1398 - (void) addToCount;
1399
1400 @end
1401
1402 @implementation Section
1403
1404 - (void) dealloc {
1405 [name_ release];
1406 [super dealloc];
1407 }
1408
1409 - (Section *) initWithName:(NSString *)name row:(size_t)row {
1410 if ((self = [super init]) != nil) {
1411 name_ = [name retain];
1412 row_ = row;
1413 } return self;
1414 }
1415
1416 - (NSString *) name {
1417 return name_;
1418 }
1419
1420 - (size_t) row {
1421 return row_;
1422 }
1423
1424 - (size_t) count {
1425 return count_;
1426 }
1427
1428 - (void) addToCount {
1429 ++count_;
1430 }
1431
1432 @end
1433 /* }}} */
1434
1435 /* Database Implementation {{{ */
1436 @implementation Database
1437
1438 - (void) dealloc {
1439 _assert(false);
1440 [super dealloc];
1441 }
1442
1443 - (void) _readCydia:(NSNumber *)fd {
1444 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1445
1446 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
1447 std::istream is(&ib);
1448 std::string line;
1449
1450 while (std::getline(is, line)) {
1451 const char *data(line.c_str());
1452 //size_t size = line.size();
1453 fprintf(stderr, "C:%s\n", data);
1454 }
1455
1456 [pool release];
1457 _assert(false);
1458 }
1459
1460 - (void) _readStatus:(NSNumber *)fd {
1461 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1462
1463 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
1464 std::istream is(&ib);
1465 std::string line;
1466
1467 Pcre conffile_r("^status: [^ ]* : conffile-prompt : (.*?) *$");
1468 Pcre pmstatus_r("^([^:]*):([^:]*):([^:]*):(.*)$");
1469
1470 while (std::getline(is, line)) {
1471 const char *data(line.c_str());
1472 size_t size = line.size();
1473 fprintf(stderr, "S:%s\n", data);
1474
1475 if (conffile_r(data, size)) {
1476 [delegate_ setConfigurationData:conffile_r[1]];
1477 } else if (strncmp(data, "status: ", 8) == 0) {
1478 NSString *string = [NSString stringWithUTF8String:(data + 8)];
1479 [delegate_ setProgressTitle:string];
1480 } else if (pmstatus_r(data, size)) {
1481 std::string type([pmstatus_r[1] UTF8String]);
1482 NSString *id = pmstatus_r[2];
1483
1484 float percent([pmstatus_r[3] floatValue]);
1485 [delegate_ setProgressPercent:(percent / 100)];
1486
1487 NSString *string = pmstatus_r[4];
1488
1489 if (type == "pmerror")
1490 [delegate_ performSelectorOnMainThread:@selector(_setProgressError:)
1491 withObject:[NSArray arrayWithObjects:string, id, nil]
1492 waitUntilDone:YES
1493 ];
1494 else if (type == "pmstatus")
1495 [delegate_ setProgressTitle:string];
1496 else if (type == "pmconffile")
1497 [delegate_ setConfigurationData:string];
1498 else _assert(false);
1499 } else _assert(false);
1500 }
1501
1502 [pool release];
1503 _assert(false);
1504 }
1505
1506 - (void) _readOutput:(NSNumber *)fd {
1507 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1508
1509 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
1510 std::istream is(&ib);
1511 std::string line;
1512
1513 while (std::getline(is, line)) {
1514 fprintf(stderr, "O:%s\n", line.c_str());
1515 [delegate_ addProgressOutput:[NSString stringWithUTF8String:line.c_str()]];
1516 }
1517
1518 [pool release];
1519 _assert(false);
1520 }
1521
1522 - (FILE *) input {
1523 return input_;
1524 }
1525
1526 - (Package *) packageWithName:(NSString *)name {
1527 if (static_cast<pkgDepCache *>(cache_) == NULL)
1528 return nil;
1529 pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String]));
1530 return iterator.end() ? nil : [Package packageWithIterator:iterator database:self];
1531 }
1532
1533 - (Database *) init {
1534 if ((self = [super init]) != nil) {
1535 policy_ = NULL;
1536 records_ = NULL;
1537 resolver_ = NULL;
1538 fetcher_ = NULL;
1539 lock_ = NULL;
1540
1541 sources_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain];
1542 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
1543
1544 int fds[2];
1545
1546 _assert(pipe(fds) != -1);
1547 cydiafd_ = fds[1];
1548
1549 _config->Set("APT::Keep-Fds::", cydiafd_);
1550 setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 0"] UTF8String], _not(int));
1551
1552 [NSThread
1553 detachNewThreadSelector:@selector(_readCydia:)
1554 toTarget:self
1555 withObject:[[NSNumber numberWithInt:fds[0]] retain]
1556 ];
1557
1558 _assert(pipe(fds) != -1);
1559 statusfd_ = fds[1];
1560
1561 [NSThread
1562 detachNewThreadSelector:@selector(_readStatus:)
1563 toTarget:self
1564 withObject:[[NSNumber numberWithInt:fds[0]] retain]
1565 ];
1566
1567 _assert(pipe(fds) != -1);
1568 _assert(dup2(fds[0], 0) != -1);
1569 _assert(close(fds[0]) != -1);
1570
1571 input_ = fdopen(fds[1], "a");
1572
1573 _assert(pipe(fds) != -1);
1574 _assert(dup2(fds[1], 1) != -1);
1575 _assert(close(fds[1]) != -1);
1576
1577 [NSThread
1578 detachNewThreadSelector:@selector(_readOutput:)
1579 toTarget:self
1580 withObject:[[NSNumber numberWithInt:fds[0]] retain]
1581 ];
1582 } return self;
1583 }
1584
1585 - (pkgCacheFile &) cache {
1586 return cache_;
1587 }
1588
1589 - (pkgDepCache::Policy *) policy {
1590 return policy_;
1591 }
1592
1593 - (pkgRecords *) records {
1594 return records_;
1595 }
1596
1597 - (pkgProblemResolver *) resolver {
1598 return resolver_;
1599 }
1600
1601 - (pkgAcquire &) fetcher {
1602 return *fetcher_;
1603 }
1604
1605 - (NSArray *) packages {
1606 return packages_;
1607 }
1608
1609 - (void) reloadData {
1610 _error->Discard();
1611
1612 delete list_;
1613 list_ = NULL;
1614 manager_ = NULL;
1615 delete lock_;
1616 lock_ = NULL;
1617 delete fetcher_;
1618 fetcher_ = NULL;
1619 delete resolver_;
1620 resolver_ = NULL;
1621 delete records_;
1622 records_ = NULL;
1623 delete policy_;
1624 policy_ = NULL;
1625
1626 cache_.Close();
1627
1628 if (!cache_.Open(progress_, true)) {
1629 std::string error;
1630 if (!_error->PopMessage(error))
1631 _assert(false);
1632 _error->Discard();
1633 fprintf(stderr, "cache_.Open():[%s]\n", error.c_str());
1634
1635 if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ")
1636 [delegate_ repairWithSelector:@selector(configure)];
1637 else if (error == "The package lists or status file could not be parsed or opened.")
1638 [delegate_ repairWithSelector:@selector(update)];
1639 // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)")
1640 // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)")
1641 // else if (error == "The list of sources could not be read.")
1642 else _assert(false);
1643
1644 return;
1645 }
1646
1647 now_ = [[NSDate date] retain];
1648
1649 policy_ = new pkgDepCache::Policy();
1650 records_ = new pkgRecords(cache_);
1651 resolver_ = new pkgProblemResolver(cache_);
1652 fetcher_ = new pkgAcquire(&status_);
1653 lock_ = NULL;
1654
1655 list_ = new pkgSourceList();
1656 _assert(list_->ReadMainList());
1657
1658 _assert(cache_->DelCount() == 0 && cache_->InstCount() == 0);
1659 _assert(pkgApplyStatus(cache_));
1660
1661 if (cache_->BrokenCount() != 0) {
1662 _assert(pkgFixBroken(cache_));
1663 _assert(cache_->BrokenCount() == 0);
1664 _assert(pkgMinimizeUpgrade(cache_));
1665 }
1666
1667 [sources_ removeAllObjects];
1668 for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) {
1669 std::vector<pkgIndexFile *> *indices = (*source)->GetIndexFiles();
1670 for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index)
1671 [sources_
1672 setObject:[[[Source alloc] initWithMetaIndex:*source] autorelease]
1673 forKey:[NSNumber numberWithLong:reinterpret_cast<uintptr_t>(*index)]
1674 ];
1675 }
1676
1677 [packages_ removeAllObjects];
1678 for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator)
1679 if (Package *package = [Package packageWithIterator:iterator database:self])
1680 [packages_ addObject:package];
1681
1682 [packages_ sortUsingSelector:@selector(compareByName:)];
1683 }
1684
1685 - (void) configure {
1686 NSString *dpkg = [NSString stringWithFormat:@"dpkg --configure -a --status-fd %u", statusfd_];
1687 system([dpkg UTF8String]);
1688 }
1689
1690 - (void) clean {
1691 if (lock_ != NULL)
1692 return;
1693
1694 FileFd Lock;
1695 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1696 _assert(!_error->PendingError());
1697
1698 pkgAcquire fetcher;
1699 fetcher.Clean(_config->FindDir("Dir::Cache::Archives"));
1700
1701 class LogCleaner :
1702 public pkgArchiveCleaner
1703 {
1704 protected:
1705 virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) {
1706 unlink(File);
1707 }
1708 } cleaner;
1709
1710 if (!cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)) {
1711 std::string error;
1712 while (_error->PopMessage(error))
1713 fprintf(stderr, "ArchiveCleaner: %s\n", error.c_str());
1714 }
1715 }
1716
1717 - (void) prepare {
1718 pkgRecords records(cache_);
1719
1720 lock_ = new FileFd();
1721 lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1722 _assert(!_error->PendingError());
1723
1724 pkgSourceList list;
1725 // XXX: explain this with an error message
1726 _assert(list.ReadMainList());
1727
1728 manager_ = (_system->CreatePM(cache_));
1729 _assert(manager_->GetArchives(fetcher_, &list, &records));
1730 _assert(!_error->PendingError());
1731 }
1732
1733 - (void) perform {
1734 NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; {
1735 pkgSourceList list;
1736 _assert(list.ReadMainList());
1737 for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source)
1738 [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]];
1739 }
1740
1741 if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) {
1742 _trace();
1743 return;
1744 }
1745
1746 bool failed = false;
1747 for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) {
1748 if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete)
1749 continue;
1750
1751 std::string uri = (*item)->DescURI();
1752 std::string error = (*item)->ErrorText;
1753
1754 fprintf(stderr, "pAf:%s:%s\n", uri.c_str(), error.c_str());
1755 failed = true;
1756
1757 [delegate_ performSelectorOnMainThread:@selector(_setProgressError:)
1758 withObject:[NSArray arrayWithObjects:[NSString stringWithUTF8String:error.c_str()], nil]
1759 waitUntilDone:YES
1760 ];
1761 }
1762
1763 if (failed) {
1764 _trace();
1765 return;
1766 }
1767
1768 _system->UnLock();
1769 pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_);
1770
1771 if (_error->PendingError()) {
1772 _trace();
1773 return;
1774 }
1775
1776 if (result == pkgPackageManager::Failed) {
1777 _trace();
1778 return;
1779 }
1780
1781 if (result != pkgPackageManager::Completed) {
1782 _trace();
1783 return;
1784 }
1785
1786 NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; {
1787 pkgSourceList list;
1788 _assert(list.ReadMainList());
1789 for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source)
1790 [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]];
1791 }
1792
1793 if (![before isEqualToArray:after])
1794 [self update];
1795 }
1796
1797 - (void) upgrade {
1798 _assert(pkgDistUpgrade(cache_));
1799 }
1800
1801 - (void) update {
1802 [self updateWithStatus:status_];
1803 }
1804
1805 - (void) updateWithStatus:(Status &)status {
1806 pkgSourceList list;
1807 _assert(list.ReadMainList());
1808
1809 FileFd lock;
1810 lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
1811 _assert(!_error->PendingError());
1812
1813 pkgAcquire fetcher(&status);
1814 _assert(list.GetIndexes(&fetcher));
1815
1816 if (fetcher.Run(PulseInterval_) != pkgAcquire::Failed) {
1817 bool failed = false;
1818 for (pkgAcquire::ItemIterator item = fetcher.ItemsBegin(); item != fetcher.ItemsEnd(); item++)
1819 if ((*item)->Status != pkgAcquire::Item::StatDone) {
1820 (*item)->Finished();
1821 failed = true;
1822 }
1823
1824 if (!failed && _config->FindB("APT::Get::List-Cleanup", true) == true) {
1825 _assert(fetcher.Clean(_config->FindDir("Dir::State::lists")));
1826 _assert(fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/"));
1827 }
1828
1829 [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"];
1830 Changed_ = true;
1831 }
1832 }
1833
1834 - (void) setDelegate:(id)delegate {
1835 delegate_ = delegate;
1836 status_.setDelegate(delegate);
1837 progress_.setDelegate(delegate);
1838 }
1839
1840 - (Source *) getSource:(const pkgCache::PkgFileIterator &)file {
1841 pkgIndexFile *index(NULL);
1842 list_->FindIndex(file, index);
1843 return [sources_ objectForKey:[NSNumber numberWithLong:reinterpret_cast<uintptr_t>(index)]];
1844 }
1845
1846 @end
1847 /* }}} */
1848
1849 /* Confirmation View {{{ */
1850 void AddTextView(NSMutableDictionary *fields, NSMutableArray *packages, NSString *key) {
1851 if ([packages count] == 0)
1852 return;
1853
1854 UITextView *text = GetTextView([packages count] == 0 ? @"n/a" : [packages componentsJoinedByString:@", "], 120, false);
1855 [fields setObject:text forKey:key];
1856
1857 CGColor blue(space_, 0, 0, 0.4, 1);
1858 [text setTextColor:blue];
1859 }
1860
1861 @protocol ConfirmationViewDelegate
1862 - (void) cancel;
1863 - (void) confirm;
1864 @end
1865
1866 @interface ConfirmationView : UIView {
1867 Database *database_;
1868 id delegate_;
1869 UITransitionView *transition_;
1870 UIView *overlay_;
1871 UINavigationBar *navbar_;
1872 UIPreferencesTable *table_;
1873 NSMutableDictionary *fields_;
1874 UIAlertSheet *essential_;
1875 }
1876
1877 - (void) cancel;
1878
1879 - (id) initWithView:(UIView *)view database:(Database *)database delegate:(id)delegate;
1880
1881 @end
1882
1883 @implementation ConfirmationView
1884
1885 - (void) dealloc {
1886 [navbar_ setDelegate:nil];
1887 [transition_ setDelegate:nil];
1888 [table_ setDataSource:nil];
1889
1890 [transition_ release];
1891 [overlay_ release];
1892 [navbar_ release];
1893 [table_ release];
1894 [fields_ release];
1895 if (essential_ != nil)
1896 [essential_ release];
1897 [super dealloc];
1898 }
1899
1900 - (void) cancel {
1901 [transition_ transition:7 toView:nil];
1902 [delegate_ cancel];
1903 }
1904
1905 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to {
1906 if (from != nil && to == nil)
1907 [self removeFromSuperview];
1908 }
1909
1910 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
1911 switch (button) {
1912 case 0:
1913 if (essential_ != nil)
1914 [essential_ popupAlertAnimated:YES];
1915 else
1916 [delegate_ confirm];
1917 break;
1918
1919 case 1:
1920 [self cancel];
1921 break;
1922 }
1923 }
1924
1925 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
1926 NSString *context = [sheet context];
1927
1928 if ([context isEqualToString:@"remove"])
1929 switch (button) {
1930 case 1:
1931 [self cancel];
1932 break;
1933 case 2:
1934 [delegate_ confirm];
1935 break;
1936 default:
1937 _assert(false);
1938 }
1939 else if ([context isEqualToString:@"unable"])
1940 [self cancel];
1941
1942 [sheet dismiss];
1943 }
1944
1945 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
1946 return 2;
1947 }
1948
1949 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group {
1950 switch (group) {
1951 case 0: return @"Statistics";
1952 case 1: return @"Modifications";
1953
1954 default: _assert(false);
1955 }
1956 }
1957
1958 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
1959 switch (group) {
1960 case 0: return 3;
1961 case 1: return [fields_ count];
1962
1963 default: _assert(false);
1964 }
1965 }
1966
1967 - (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed {
1968 if (group != 1 || row == -1)
1969 return proposed;
1970 else {
1971 _assert(size_t(row) < [fields_ count]);
1972 return [[[fields_ allValues] objectAtIndex:row] visibleTextRect].size.height + TextViewOffset_;
1973 }
1974 }
1975
1976 - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group {
1977 UIPreferencesTableCell *cell = [[[UIPreferencesTableCell alloc] init] autorelease];
1978 [cell setShowSelection:NO];
1979
1980 switch (group) {
1981 case 0: switch (row) {
1982 case 0: {
1983 [cell setTitle:@"Downloading"];
1984 [cell setValue:SizeString([database_ fetcher].FetchNeeded())];
1985 } break;
1986
1987 case 1: {
1988 [cell setTitle:@"Resuming At"];
1989 [cell setValue:SizeString([database_ fetcher].PartialPresent())];
1990 } break;
1991
1992 case 2: {
1993 double size([database_ cache]->UsrSize());
1994
1995 if (size < 0) {
1996 [cell setTitle:@"Disk Freeing"];
1997 [cell setValue:SizeString(-size)];
1998 } else {
1999 [cell setTitle:@"Disk Using"];
2000 [cell setValue:SizeString(size)];
2001 }
2002 } break;
2003
2004 default: _assert(false);
2005 } break;
2006
2007 case 1:
2008 _assert(size_t(row) < [fields_ count]);
2009 [cell setTitle:[[fields_ allKeys] objectAtIndex:row]];
2010 [cell addSubview:[[fields_ allValues] objectAtIndex:row]];
2011 break;
2012
2013 default: _assert(false);
2014 }
2015
2016 return cell;
2017 }
2018
2019 - (id) initWithView:(UIView *)view database:(Database *)database delegate:(id)delegate {
2020 if ((self = [super initWithFrame:[view bounds]]) != nil) {
2021 database_ = database;
2022 delegate_ = delegate;
2023
2024 transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]];
2025 [self addSubview:transition_];
2026
2027 overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]];
2028
2029 CGSize navsize = [UINavigationBar defaultSize];
2030 CGRect navrect = {{0, 0}, navsize};
2031 CGRect bounds = [overlay_ bounds];
2032
2033 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
2034 if (Advanced_)
2035 [navbar_ setBarStyle:1];
2036 [navbar_ setDelegate:self];
2037
2038 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Confirm"] autorelease];
2039 [navbar_ pushNavigationItem:navitem];
2040 [navbar_ showButtonsWithLeftTitle:@"Cancel" rightTitle:@"Confirm"];
2041
2042 fields_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain];
2043
2044 NSMutableArray *installing = [NSMutableArray arrayWithCapacity:16];
2045 NSMutableArray *reinstalling = [NSMutableArray arrayWithCapacity:16];
2046 NSMutableArray *upgrading = [NSMutableArray arrayWithCapacity:16];
2047 NSMutableArray *downgrading = [NSMutableArray arrayWithCapacity:16];
2048 NSMutableArray *removing = [NSMutableArray arrayWithCapacity:16];
2049
2050 bool remove(false);
2051
2052 pkgCacheFile &cache([database_ cache]);
2053 NSArray *packages = [database_ packages];
2054 for (size_t i(0), e = [packages count]; i != e; ++i) {
2055 Package *package = [packages objectAtIndex:i];
2056 pkgCache::PkgIterator iterator = [package iterator];
2057 pkgDepCache::StateCache &state(cache[iterator]);
2058
2059 NSString *name([package name]);
2060
2061 if (state.NewInstall())
2062 [installing addObject:name];
2063 else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
2064 [reinstalling addObject:name];
2065 else if (state.Upgrade())
2066 [upgrading addObject:name];
2067 else if (state.Downgrade())
2068 [downgrading addObject:name];
2069 else if (state.Delete()) {
2070 if ([package essential])
2071 remove = true;
2072 [removing addObject:name];
2073 }
2074 }
2075
2076 if (!remove)
2077 essential_ = nil;
2078 else if (Advanced_ || true) {
2079 essential_ = [[UIAlertSheet alloc]
2080 initWithTitle:@"Removing Essentials"
2081 buttons:[NSArray arrayWithObjects:
2082 @"Cancel Operation (Safe)",
2083 @"Force Removal (Unsafe)",
2084 nil]
2085 defaultButtonIndex:0
2086 delegate:self
2087 context:@"remove"
2088 ];
2089
2090 #ifndef __OBJC2__
2091 [essential_ setDestructiveButton:[[essential_ buttons] objectAtIndex:0]];
2092 #endif
2093 [essential_ setBodyText:@"This operation involves the removal of one or more packages that are required for the continued operation of either Cydia or iPhoneOS. If you continue, you may not be able to use Cydia to repair any damage."];
2094 } else {
2095 essential_ = [[UIAlertSheet alloc]
2096 initWithTitle:@"Unable to Comply"
2097 buttons:[NSArray arrayWithObjects:@"Okay", nil]
2098 defaultButtonIndex:0
2099 delegate:self
2100 context:@"unable"
2101 ];
2102
2103 [essential_ setBodyText:@"This operation requires the removal of one or more packages that are required for the continued operation of either Cydia or iPhoneOS. In order to continue and force this operation you will need to be activate the Advanced mode under to continue and force this operation you will need to be activate the Advanced mode under Settings."];
2104 }
2105
2106 AddTextView(fields_, installing, @"Installing");
2107 AddTextView(fields_, reinstalling, @"Reinstalling");
2108 AddTextView(fields_, upgrading, @"Upgrading");
2109 AddTextView(fields_, downgrading, @"Downgrading");
2110 AddTextView(fields_, removing, @"Removing");
2111
2112 table_ = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(
2113 0, navsize.height, bounds.size.width, bounds.size.height - navsize.height
2114 )];
2115
2116 [table_ setReusesTableCells:YES];
2117 [table_ setDataSource:self];
2118 [table_ reloadData];
2119
2120 [overlay_ addSubview:navbar_];
2121 [overlay_ addSubview:table_];
2122
2123 [view addSubview:self];
2124
2125 [transition_ setDelegate:self];
2126
2127 UIView *blank = [[[UIView alloc] initWithFrame:[transition_ bounds]] autorelease];
2128 [transition_ transition:0 toView:blank];
2129 [transition_ transition:3 toView:overlay_];
2130 } return self;
2131 }
2132
2133 @end
2134 /* }}} */
2135
2136 /* Progress Data {{{ */
2137 @interface ProgressData : NSObject {
2138 SEL selector_;
2139 id target_;
2140 id object_;
2141 }
2142
2143 - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object;
2144
2145 - (SEL) selector;
2146 - (id) target;
2147 - (id) object;
2148 @end
2149
2150 @implementation ProgressData
2151
2152 - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object {
2153 if ((self = [super init]) != nil) {
2154 selector_ = selector;
2155 target_ = target;
2156 object_ = object;
2157 } return self;
2158 }
2159
2160 - (SEL) selector {
2161 return selector_;
2162 }
2163
2164 - (id) target {
2165 return target_;
2166 }
2167
2168 - (id) object {
2169 return object_;
2170 }
2171
2172 @end
2173 /* }}} */
2174 /* Progress View {{{ */
2175 Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$");
2176
2177 @interface ProgressView : UIView <
2178 ConfigurationDelegate,
2179 ProgressDelegate
2180 > {
2181 _transient Database *database_;
2182 UIView *view_;
2183 UIView *background_;
2184 UITransitionView *transition_;
2185 UIView *overlay_;
2186 UINavigationBar *navbar_;
2187 UIProgressBar *progress_;
2188 UITextView *output_;
2189 UITextLabel *status_;
2190 UIPushButton *close_;
2191 id delegate_;
2192 BOOL running_;
2193 }
2194
2195 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to;
2196
2197 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database delegate:(id)delegate;
2198 - (void) setContentView:(UIView *)view;
2199 - (void) resetView;
2200
2201 - (void) _retachThread;
2202 - (void) _detachNewThreadData:(ProgressData *)data;
2203 - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title;
2204
2205 - (BOOL) isRunning;
2206
2207 @end
2208
2209 @protocol ProgressViewDelegate
2210 - (void) progressViewIsComplete:(ProgressView *)sender;
2211 @end
2212
2213 @implementation ProgressView
2214
2215 - (void) dealloc {
2216 [transition_ setDelegate:nil];
2217 [navbar_ setDelegate:nil];
2218
2219 [view_ release];
2220 if (background_ != nil)
2221 [background_ release];
2222 [transition_ release];
2223 [overlay_ release];
2224 [navbar_ release];
2225 [progress_ release];
2226 [output_ release];
2227 [status_ release];
2228 [close_ release];
2229 [super dealloc];
2230 }
2231
2232 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to {
2233 if (bootstrap_ && from == overlay_ && to == view_)
2234 exit(0);
2235 }
2236
2237 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database delegate:(id)delegate {
2238 if ((self = [super initWithFrame:frame]) != nil) {
2239 database_ = database;
2240 delegate_ = delegate;
2241
2242 transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]];
2243 [transition_ setDelegate:self];
2244
2245 overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]];
2246
2247 if (bootstrap_)
2248 [overlay_ setBackgroundColor:Black_];
2249 else {
2250 background_ = [[UIView alloc] initWithFrame:[self bounds]];
2251 [background_ setBackgroundColor:Black_];
2252 [self addSubview:background_];
2253 }
2254
2255 [self addSubview:transition_];
2256
2257 CGSize navsize = [UINavigationBar defaultSize];
2258 CGRect navrect = {{0, 0}, navsize};
2259
2260 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
2261 [overlay_ addSubview:navbar_];
2262
2263 [navbar_ setBarStyle:1];
2264 [navbar_ setDelegate:self];
2265
2266 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:nil] autorelease];
2267 [navbar_ pushNavigationItem:navitem];
2268
2269 CGRect bounds = [overlay_ bounds];
2270 CGSize prgsize = [UIProgressBar defaultSize];
2271
2272 CGRect prgrect = {{
2273 (bounds.size.width - prgsize.width) / 2,
2274 bounds.size.height - prgsize.height - 20
2275 }, prgsize};
2276
2277 progress_ = [[UIProgressBar alloc] initWithFrame:prgrect];
2278 [progress_ setStyle:0];
2279
2280 status_ = [[UITextLabel alloc] initWithFrame:CGRectMake(
2281 10,
2282 bounds.size.height - prgsize.height - 50,
2283 bounds.size.width - 20,
2284 24
2285 )];
2286
2287 [status_ setColor:White_];
2288 [status_ setBackgroundColor:Clear_];
2289
2290 [status_ setCentersHorizontally:YES];
2291 //[status_ setFont:font];
2292
2293 output_ = [[UITextView alloc] initWithFrame:CGRectMake(
2294 10,
2295 navrect.size.height + 20,
2296 bounds.size.width - 20,
2297 bounds.size.height - navsize.height - 62 - navrect.size.height
2298 )];
2299
2300 //[output_ setTextFont:@"Courier New"];
2301 [output_ setTextSize:12];
2302
2303 [output_ setTextColor:White_];
2304 [output_ setBackgroundColor:Clear_];
2305
2306 [output_ setMarginTop:0];
2307 [output_ setAllowsRubberBanding:YES];
2308 [output_ setEditable:NO];
2309
2310 [overlay_ addSubview:output_];
2311
2312 close_ = [[UIPushButton alloc] initWithFrame:CGRectMake(
2313 10,
2314 bounds.size.height - prgsize.height - 50,
2315 bounds.size.width - 20,
2316 32 + prgsize.height
2317 )];
2318
2319 [close_ setAutosizesToFit:NO];
2320 [close_ setDrawsShadow:YES];
2321 [close_ setStretchBackground:YES];
2322 [close_ setTitle:@"Close Window"];
2323 [close_ setEnabled:YES];
2324
2325 GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22);
2326 [close_ setTitleFont:bold];
2327 CFRelease(bold);
2328
2329 [close_ addTarget:self action:@selector(closeButtonPushed) forEvents:kUIControlEventMouseUpInside];
2330 [close_ setBackground:[UIImage applicationImageNamed:@"green-up.png"] forState:0];
2331 [close_ setBackground:[UIImage applicationImageNamed:@"green-dn.png"] forState:1];
2332 } return self;
2333 }
2334
2335 - (void) setContentView:(UIView *)view {
2336 view_ = [view retain];
2337 }
2338
2339 - (void) resetView {
2340 [transition_ transition:6 toView:view_];
2341 }
2342
2343 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
2344 NSString *context = [sheet context];
2345 if ([context isEqualToString:@"conffile"]) {
2346 FILE *input = [database_ input];
2347
2348 switch (button) {
2349 case 1:
2350 fprintf(input, "N\n");
2351 fflush(input);
2352 break;
2353 case 2:
2354 fprintf(input, "Y\n");
2355 fflush(input);
2356 break;
2357 default:
2358 _assert(false);
2359 }
2360 }
2361
2362 [sheet dismiss];
2363 }
2364
2365 - (void) closeButtonPushed {
2366 [delegate_ progressViewIsComplete:self];
2367 [self resetView];
2368 }
2369
2370 - (void) _retachThread {
2371 UINavigationItem *item = [navbar_ topItem];
2372 [item setTitle:@"Complete"];
2373
2374 [overlay_ addSubview:close_];
2375 [progress_ removeFromSuperview];
2376 [status_ removeFromSuperview];
2377
2378 [delegate_ setStatusBarShowsProgress:NO];
2379 //[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
2380
2381 running_ = NO;
2382 }
2383
2384 - (void) _detachNewThreadData:(ProgressData *)data {
2385 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2386
2387 [[data target] performSelector:[data selector] withObject:[data object]];
2388 [data release];
2389
2390 [self performSelectorOnMainThread:@selector(_retachThread) withObject:nil waitUntilDone:YES];
2391
2392 [pool release];
2393 }
2394
2395 - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title {
2396 UINavigationItem *item = [navbar_ topItem];
2397 [item setTitle:title];
2398
2399 [status_ setText:nil];
2400 [output_ setText:@""];
2401 [progress_ setProgress:0];
2402
2403 [close_ removeFromSuperview];
2404 [overlay_ addSubview:progress_];
2405 [overlay_ addSubview:status_];
2406
2407 [delegate_ setStatusBarShowsProgress:YES];
2408 running_ = YES;
2409
2410 [transition_ transition:6 toView:overlay_];
2411
2412 [NSThread
2413 detachNewThreadSelector:@selector(_detachNewThreadData:)
2414 toTarget:self
2415 withObject:[[ProgressData alloc]
2416 initWithSelector:selector
2417 target:target
2418 object:object
2419 ]
2420 ];
2421 }
2422
2423 - (void) repairWithSelector:(SEL)selector {
2424 [self
2425 detachNewThreadSelector:selector
2426 toTarget:database_
2427 withObject:nil
2428 title:@"Repairing..."
2429 ];
2430 }
2431
2432 - (void) setConfigurationData:(NSString *)data {
2433 [self
2434 performSelectorOnMainThread:@selector(_setConfigurationData:)
2435 withObject:data
2436 waitUntilDone:YES
2437 ];
2438 }
2439
2440 - (void) setProgressError:(NSString *)error forPackage:(NSString *)id {
2441 Package *package = id == nil ? nil : [database_ packageWithName:id];
2442
2443 UIAlertSheet *sheet = [[[UIAlertSheet alloc]
2444 initWithTitle:(package == nil ? @"Source Error" : [package name])
2445 buttons:[NSArray arrayWithObjects:@"Okay", nil]
2446 defaultButtonIndex:0
2447 delegate:self
2448 context:@"error"
2449 ] autorelease];
2450
2451 [sheet setBodyText:error];
2452 [sheet popupAlertAnimated:YES];
2453 }
2454
2455 - (void) setProgressTitle:(NSString *)title {
2456 [self
2457 performSelectorOnMainThread:@selector(_setProgressTitle:)
2458 withObject:title
2459 waitUntilDone:YES
2460 ];
2461 }
2462
2463 - (void) setProgressPercent:(float)percent {
2464 [self
2465 performSelectorOnMainThread:@selector(_setProgressPercent:)
2466 withObject:[NSNumber numberWithFloat:percent]
2467 waitUntilDone:YES
2468 ];
2469 }
2470
2471 - (void) addProgressOutput:(NSString *)output {
2472 [self
2473 performSelectorOnMainThread:@selector(_addProgressOutput:)
2474 withObject:output
2475 waitUntilDone:YES
2476 ];
2477 }
2478
2479 - (void) _setConfigurationData:(NSString *)data {
2480 _assert(conffile_r(data));
2481
2482 NSString *ofile = conffile_r[1];
2483 //NSString *nfile = conffile_r[2];
2484
2485 UIAlertSheet *sheet = [[[UIAlertSheet alloc]
2486 initWithTitle:@"Configuration Upgrade"
2487 buttons:[NSArray arrayWithObjects:
2488 @"Keep My Old Copy",
2489 @"Accept The New Copy",
2490 // XXX: @"See What Changed",
2491 nil]
2492 defaultButtonIndex:0
2493 delegate:self
2494 context:@"conffile"
2495 ] autorelease];
2496
2497 [sheet setBodyText:[NSString stringWithFormat:
2498 @"The following file has been changed by both the package maintainer and by you (or for you by a script).\n\n%@"
2499 , ofile]];
2500
2501 [sheet popupAlertAnimated:YES];
2502 }
2503
2504 - (void) _setProgressTitle:(NSString *)title {
2505 [status_ setText:[title stringByAppendingString:@"..."]];
2506 }
2507
2508 - (void) _setProgressPercent:(NSNumber *)percent {
2509 [progress_ setProgress:[percent floatValue]];
2510 }
2511
2512 - (void) _addProgressOutput:(NSString *)output {
2513 [output_ setText:[NSString stringWithFormat:@"%@\n%@", [output_ text], output]];
2514 CGSize size = [output_ contentSize];
2515 CGRect rect = {{0, size.height}, {size.width, 0}};
2516 [output_ scrollRectToVisible:rect animated:YES];
2517 }
2518
2519 - (BOOL) isRunning {
2520 return NO;
2521 }
2522
2523 @end
2524 /* }}} */
2525
2526 /* Package Cell {{{ */
2527 @interface PackageCell : UITableCell {
2528 UIImageView *icon_;
2529 UITextLabel *name_;
2530 UITextLabel *description_;
2531 UITextLabel *source_;
2532 //UIImageView *trusted_;
2533 #ifdef USE_BADGES
2534 UIImageView *badge_;
2535 UITextLabel *status_;
2536 #endif
2537 BOOL setup_;
2538 }
2539
2540 - (PackageCell *) init;
2541 - (void) setPackage:(Package *)package;
2542
2543 - (void) _setSelected:(float)fraction;
2544 - (void) setSelected:(BOOL)selected;
2545 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade;
2546 - (void) _setSelectionFadeFraction:(float)fraction;
2547
2548 + (int) heightForPackage:(Package *)package;
2549
2550 @end
2551
2552 @implementation PackageCell
2553
2554 - (void) dealloc {
2555 [icon_ release];
2556 [name_ release];
2557 [description_ release];
2558 [source_ release];
2559 #ifdef USE_BADGES
2560 [badge_ release];
2561 [status_ release];
2562 #endif
2563 //[trusted_ release];
2564 [super dealloc];
2565 }
2566
2567 - (PackageCell *) init {
2568 if ((self = [super init]) != nil) {
2569 GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 20);
2570 GSFontRef large = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 12);
2571 GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 14);
2572
2573 icon_ = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
2574
2575 name_ = [[UITextLabel alloc] initWithFrame:CGRectMake(48, 8, 240, 25)];
2576 [name_ setBackgroundColor:Clear_];
2577 [name_ setFont:bold];
2578
2579 source_ = [[UITextLabel alloc] initWithFrame:CGRectMake(58, 28, 225, 20)];
2580 [source_ setBackgroundColor:Clear_];
2581 [source_ setFont:large];
2582
2583 description_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 46, 280, 20)];
2584 [description_ setBackgroundColor:Clear_];
2585 [description_ setFont:small];
2586
2587 /*trusted_ = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 16, 16)];
2588 [trusted_ setImage:[UIImage applicationImageNamed:@"trusted.png"]];*/
2589
2590 #ifdef USE_BADGES
2591 badge_ = [[UIImageView alloc] initWithFrame:CGRectMake(17, 70, 16, 16)];
2592
2593 status_ = [[UITextLabel alloc] initWithFrame:CGRectMake(48, 68, 280, 20)];
2594 [status_ setBackgroundColor:Clear_];
2595 [status_ setFont:small];
2596 #endif
2597
2598 /*[icon_ setImage:[UIImage applicationImageNamed:@"unknown.png"]];
2599 [icon_ zoomToScale:0.5];
2600 [icon_ setFrame:CGRectMake(10, 10, 30, 30)];*/
2601
2602 [self addSubview:icon_];
2603 [self addSubview:name_];
2604 [self addSubview:description_];
2605 [self addSubview:source_];
2606
2607 CFRelease(small);
2608 CFRelease(large);
2609 CFRelease(bold);
2610 } return self;
2611 }
2612
2613 - (void) setPackage:(Package *)package {
2614 /*if (setup_)
2615 return;
2616 else
2617 setup_ = YES;*/
2618
2619 Source *source = [package source];
2620
2621 UIImage *image = nil;
2622 if (NSString *icon = [package icon])
2623 image = [UIImage imageAtPath:[icon substringFromIndex:6]];
2624 if (image == nil) if (NSString *icon = [source defaultIcon])
2625 image = [UIImage imageAtPath:[icon substringFromIndex:6]];
2626 if (image == nil)
2627 image = [UIImage applicationImageNamed:@"unknown.png"];
2628 [icon_ setImage:image];
2629
2630 /*if (image != nil) {
2631 CGSize size = [image size];
2632 float scale = 30 / std::max(size.width, size.height);
2633 [icon_ zoomToScale:scale];
2634 }*/
2635
2636 [icon_ setFrame:CGRectMake(10, 10, 30, 30)];
2637
2638 [name_ setText:[package name]];
2639 [description_ setText:[package tagline]];
2640
2641 NSString *label = nil;
2642 bool trusted = false;
2643
2644 if (source != nil) {
2645 label = [source label];
2646 trusted = [source trusted];
2647 } else if ([[package id] isEqualToString:@"firmware"])
2648 label = @"Apple";
2649
2650 if (label == nil)
2651 label = @"Unknown/Local";
2652
2653 NSString *from = [NSString stringWithFormat:@"from %@", label];
2654
2655 NSString *section = Simplify([package section]);
2656 if (section != nil && ![section isEqualToString:label])
2657 from = [from stringByAppendingString:[NSString stringWithFormat:@" (%@)", section]];
2658
2659 [source_ setText:from];
2660
2661 #ifdef USE_BADGES
2662 [badge_ removeFromSuperview];
2663 [status_ removeFromSuperview];
2664
2665 if (NSString *mode = [package mode]) {
2666 [badge_ setImage:[UIImage applicationImageNamed:
2667 [mode isEqualToString:@"Remove"] || [mode isEqualToString:@"Purge"] ? @"removing.png" : @"installing.png"
2668 ]];
2669
2670 [status_ setText:[NSString stringWithFormat:@"Queued for %@", mode]];
2671 [status_ setColor:Blueish_];
2672 } else if ([package half]) {
2673 [badge_ setImage:[UIImage applicationImageNamed:@"damaged.png"]];
2674 [status_ setText:@"Package Damaged"];
2675 [status_ setColor:Red_];
2676 } else {
2677 [badge_ setImage:nil];
2678 [status_ setText:nil];
2679 goto done;
2680 }
2681
2682 [self addSubview:badge_];
2683 [self addSubview:status_];
2684 done:;
2685 #endif
2686 }
2687
2688 - (void) _setSelected:(float)fraction {
2689 CGColor black(space_,
2690 Interpolate(0.0, 1.0, fraction),
2691 Interpolate(0.0, 1.0, fraction),
2692 Interpolate(0.0, 1.0, fraction),
2693 1.0);
2694
2695 CGColor gray(space_,
2696 Interpolate(0.4, 1.0, fraction),
2697 Interpolate(0.4, 1.0, fraction),
2698 Interpolate(0.4, 1.0, fraction),
2699 1.0);
2700
2701 [name_ setColor:black];
2702 [description_ setColor:gray];
2703 [source_ setColor:black];
2704 }
2705
2706 - (void) setSelected:(BOOL)selected {
2707 [self _setSelected:(selected ? 1.0 : 0.0)];
2708 [super setSelected:selected];
2709 }
2710
2711 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade {
2712 if (!fade)
2713 [self _setSelected:(selected ? 1.0 : 0.0)];
2714 [super setSelected:selected withFade:fade];
2715 }
2716
2717 - (void) _setSelectionFadeFraction:(float)fraction {
2718 [self _setSelected:fraction];
2719 [super _setSelectionFadeFraction:fraction];
2720 }
2721
2722 + (int) heightForPackage:(Package *)package {
2723 #ifdef USE_BADGES
2724 if ([package hasMode] || [package half])
2725 return 96;
2726 else
2727 #endif
2728 return 73;
2729 }
2730
2731 @end
2732 /* }}} */
2733 /* Section Cell {{{ */
2734 @interface SectionCell : UITableCell {
2735 UITextLabel *name_;
2736 UITextLabel *count_;
2737 }
2738
2739 - (id) init;
2740 - (void) setSection:(Section *)section;
2741
2742 - (void) _setSelected:(float)fraction;
2743 - (void) setSelected:(BOOL)selected;
2744 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade;
2745 - (void) _setSelectionFadeFraction:(float)fraction;
2746
2747 @end
2748
2749 @implementation SectionCell
2750
2751 - (void) dealloc {
2752 [name_ release];
2753 [count_ release];
2754 [super dealloc];
2755 }
2756
2757 - (id) init {
2758 if ((self = [super init]) != nil) {
2759 GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22);
2760 GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 12);
2761
2762 name_ = [[UITextLabel alloc] initWithFrame:CGRectMake(48, 9, 250, 25)];
2763 [name_ setBackgroundColor:Clear_];
2764 [name_ setFont:bold];
2765
2766 count_ = [[UITextLabel alloc] initWithFrame:CGRectMake(11, 7, 29, 32)];
2767 [count_ setCentersHorizontally:YES];
2768 [count_ setBackgroundColor:Clear_];
2769 [count_ setFont:small];
2770 [count_ setColor:White_];
2771
2772 UIImageView *folder = [[[UIImageView alloc] initWithFrame:CGRectMake(8, 7, 32, 32)] autorelease];
2773 [folder setImage:[UIImage applicationImageNamed:@"folder.png"]];
2774
2775 [self addSubview:folder];
2776 [self addSubview:name_];
2777 [self addSubview:count_];
2778
2779 [self _setSelected:0];
2780
2781 CFRelease(small);
2782 CFRelease(bold);
2783 } return self;
2784 }
2785
2786 - (void) setSection:(Section *)section {
2787 if (section == nil) {
2788 [name_ setText:@"All Packages"];
2789 [count_ setText:nil];
2790 } else {
2791 NSString *name = [section name];
2792 [name_ setText:(name == nil ? @"(No Section)" : name)];
2793 [count_ setText:[NSString stringWithFormat:@"%d", [section count]]];
2794 }
2795 }
2796
2797 - (void) _setSelected:(float)fraction {
2798 CGColor black(space_,
2799 Interpolate(0.0, 1.0, fraction),
2800 Interpolate(0.0, 1.0, fraction),
2801 Interpolate(0.0, 1.0, fraction),
2802 1.0);
2803
2804 [name_ setColor:black];
2805 }
2806
2807 - (void) setSelected:(BOOL)selected {
2808 [self _setSelected:(selected ? 1.0 : 0.0)];
2809 [super setSelected:selected];
2810 }
2811
2812 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade {
2813 if (!fade)
2814 [self _setSelected:(selected ? 1.0 : 0.0)];
2815 [super setSelected:selected withFade:fade];
2816 }
2817
2818 - (void) _setSelectionFadeFraction:(float)fraction {
2819 [self _setSelected:fraction];
2820 [super _setSelectionFadeFraction:fraction];
2821 }
2822
2823 @end
2824 /* }}} */
2825
2826 /* File Table {{{ */
2827 @interface FileTable : RVPage {
2828 _transient Database *database_;
2829 Package *package_;
2830 NSString *name_;
2831 NSMutableArray *files_;
2832 UITable *list_;
2833 }
2834
2835 - (id) initWithBook:(RVBook *)book database:(Database *)database;
2836 - (void) setPackage:(Package *)package;
2837
2838 @end
2839
2840 @implementation FileTable
2841
2842 - (void) dealloc {
2843 if (package_ != nil)
2844 [package_ release];
2845 if (name_ != nil)
2846 [name_ release];
2847 [files_ release];
2848 [list_ release];
2849 [super dealloc];
2850 }
2851
2852 - (int) numberOfRowsInTable:(UITable *)table {
2853 return files_ == nil ? 0 : [files_ count];
2854 }
2855
2856 - (float) table:(UITable *)table heightForRow:(int)row {
2857 return 24;
2858 }
2859
2860 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
2861 if (reusing == nil) {
2862 reusing = [[[UIImageAndTextTableCell alloc] init] autorelease];
2863 GSFontRef font = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 16);
2864 [[(UIImageAndTextTableCell *)reusing titleTextLabel] setFont:font];
2865 CFRelease(font);
2866 }
2867 [(UIImageAndTextTableCell *)reusing setTitle:[files_ objectAtIndex:row]];
2868 return reusing;
2869 }
2870
2871 - (BOOL) table:(UITable *)table canSelectRow:(int)row {
2872 return NO;
2873 }
2874
2875 - (id) initWithBook:(RVBook *)book database:(Database *)database {
2876 if ((self = [super initWithBook:book]) != nil) {
2877 database_ = database;
2878
2879 files_ = [[NSMutableArray arrayWithCapacity:32] retain];
2880
2881 list_ = [[UITable alloc] initWithFrame:[self bounds]];
2882 [self addSubview:list_];
2883
2884 UITableColumn *column = [[[UITableColumn alloc]
2885 initWithTitle:@"Name"
2886 identifier:@"name"
2887 width:[self frame].size.width
2888 ] autorelease];
2889
2890 [list_ setDataSource:self];
2891 [list_ setSeparatorStyle:1];
2892 [list_ addTableColumn:column];
2893 [list_ setDelegate:self];
2894 [list_ setReusesTableCells:YES];
2895 } return self;
2896 }
2897
2898 - (void) setPackage:(Package *)package {
2899 if (package_ != nil) {
2900 [package_ autorelease];
2901 package_ = nil;
2902 }
2903
2904 if (name_ != nil) {
2905 [name_ release];
2906 name_ = nil;
2907 }
2908
2909 [files_ removeAllObjects];
2910
2911 if (package != nil) {
2912 package_ = [package retain];
2913 name_ = [[package id] retain];
2914
2915 NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", name_];
2916
2917 {
2918 std::ifstream fin([path UTF8String]);
2919 std::string line;
2920 while (std::getline(fin, line))
2921 [files_ addObject:[NSString stringWithUTF8String:line.c_str()]];
2922 }
2923
2924 if ([files_ count] != 0) {
2925 if ([[files_ objectAtIndex:0] isEqualToString:@"/."])
2926 [files_ removeObjectAtIndex:0];
2927 [files_ sortUsingSelector:@selector(compareByPath:)];
2928
2929 NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8];
2930 [stack addObject:@"/"];
2931
2932 for (int i(0), e([files_ count]); i != e; ++i) {
2933 NSString *file = [files_ objectAtIndex:i];
2934 while (![file hasPrefix:[stack lastObject]])
2935 [stack removeLastObject];
2936 NSString *directory = [stack lastObject];
2937 [stack addObject:[file stringByAppendingString:@"/"]];
2938 [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@",
2939 ([stack count] - 2) * 3, "",
2940 [file substringFromIndex:[directory length]]
2941 ]];
2942 }
2943 }
2944 }
2945
2946 [list_ reloadData];
2947 }
2948
2949 - (void) resetViewAnimated:(BOOL)animated {
2950 [list_ resetViewAnimated:animated];
2951 }
2952
2953 - (void) reloadData {
2954 [self setPackage:[database_ packageWithName:name_]];
2955 [self reloadButtons];
2956 }
2957
2958 - (NSString *) title {
2959 return @"Installed Files";
2960 }
2961
2962 - (NSString *) backButtonTitle {
2963 return @"Files";
2964 }
2965
2966 @end
2967 /* }}} */
2968 /* Package View {{{ */
2969 @protocol PackageViewDelegate
2970 - (void) performPackage:(Package *)package;
2971 @end
2972
2973 @interface PackageView : RVPage {
2974 _transient Database *database_;
2975 UIPreferencesTable *table_;
2976 Package *package_;
2977 NSString *name_;
2978 UITextView *description_;
2979 NSMutableArray *buttons_;
2980 }
2981
2982 - (id) initWithBook:(RVBook *)book database:(Database *)database;
2983 - (void) setPackage:(Package *)package;
2984
2985 @end
2986
2987 @implementation PackageView
2988
2989 - (void) dealloc {
2990 [table_ setDataSource:nil];
2991 [table_ setDelegate:nil];
2992
2993 if (package_ != nil)
2994 [package_ release];
2995 if (name_ != nil)
2996 [name_ release];
2997 if (description_ != nil)
2998 [description_ release];
2999 [table_ release];
3000 [buttons_ release];
3001 [super dealloc];
3002 }
3003
3004 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
3005 int number = 2;
3006 if ([package_ installed] != nil)
3007 ++number;
3008 if ([package_ source] != nil)
3009 ++number;
3010 return number;
3011 }
3012
3013 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group {
3014 if (group-- == 0)
3015 return nil;
3016 else if ([package_ installed] != nil && group-- == 0)
3017 return @"Installed Package";
3018 else if (group-- == 0)
3019 return @"Package Details";
3020 else if ([package_ source] != nil && group-- == 0)
3021 return @"Source Information";
3022 else _assert(false);
3023 }
3024
3025 - (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed {
3026 if (description_ == nil || group != 0 || row != 1)
3027 return proposed;
3028 else
3029 return [description_ visibleTextRect].size.height + TextViewOffset_;
3030 }
3031
3032 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
3033 if (group-- == 0) {
3034 int number = 1;
3035 if ([package_ author] != nil)
3036 ++number;
3037 if (description_ != nil)
3038 ++number;
3039 if ([package_ website] != nil)
3040 ++number;
3041 return number;
3042 } else if ([package_ installed] != nil && group-- == 0)
3043 return 2;
3044 else if (group-- == 0) {
3045 int number = 2;
3046 if ([package_ size] != 0)
3047 ++number;
3048 if ([package_ maintainer] != nil)
3049 ++number;
3050 if ([package_ sponsor] != nil)
3051 ++number;
3052 if ([package_ relationships] != nil)
3053 ++number;
3054 if ([[package_ source] trusted])
3055 ++number;
3056 return number;
3057 } else if ([package_ source] != nil && group-- == 0) {
3058 Source *source = [package_ source];
3059 NSString *description = [source description];
3060 int number = 1;
3061 if (description != nil && ![description isEqualToString:[source label]])
3062 ++number;
3063 if ([source origin] != nil)
3064 ++number;
3065 return number;
3066 } else _assert(false);
3067 }
3068
3069 - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group {
3070 UIPreferencesTableCell *cell = [[[UIPreferencesTableCell alloc] init] autorelease];
3071 [cell setShowSelection:NO];
3072
3073 if (group-- == 0) {
3074 if (false) {
3075 } else if (row-- == 0) {
3076 [cell setTitle:[package_ name]];
3077 [cell setValue:[package_ latest]];
3078 } else if ([package_ author] != nil && row-- == 0) {
3079 [cell setTitle:@"Author"];
3080 [cell setValue:[[package_ author] name]];
3081 [cell setShowDisclosure:YES];
3082 [cell setShowSelection:YES];
3083 } else if (description_ != nil && row-- == 0) {
3084 [cell addSubview:description_];
3085 } else if ([package_ website] != nil && row-- == 0) {
3086 [cell setTitle:@"More Information"];
3087 [cell setShowDisclosure:YES];
3088 [cell setShowSelection:YES];
3089 } else _assert(false);
3090 } else if ([package_ installed] != nil && group-- == 0) {
3091 if (false) {
3092 } else if (row-- == 0) {
3093 [cell setTitle:@"Version"];
3094 NSString *installed([package_ installed]);
3095 [cell setValue:(installed == nil ? @"n/a" : installed)];
3096 } else if (row-- == 0) {
3097 [cell setTitle:@"Filesystem Content"];
3098 [cell setShowDisclosure:YES];
3099 [cell setShowSelection:YES];
3100 } else _assert(false);
3101 } else if (group-- == 0) {
3102 if (false) {
3103 } else if (row-- == 0) {
3104 [cell setTitle:@"Identifier"];
3105 [cell setValue:[package_ id]];
3106 } else if (row-- == 0) {
3107 [cell setTitle:@"Section"];
3108 NSString *section([package_ section]);
3109 [cell setValue:(section == nil ? @"n/a" : section)];
3110 } else if ([package_ size] != 0 && row-- == 0) {
3111 [cell setTitle:@"Expanded Size"];
3112 [cell setValue:SizeString([package_ size])];
3113 } else if ([package_ maintainer] != nil && row-- == 0) {
3114 [cell setTitle:@"Maintainer"];
3115 [cell setValue:[[package_ maintainer] name]];
3116 [cell setShowDisclosure:YES];
3117 [cell setShowSelection:YES];
3118 } else if ([package_ sponsor] != nil && row-- == 0) {
3119 [cell setTitle:@"Sponsor"];
3120 [cell setValue:[[package_ sponsor] name]];
3121 [cell setShowDisclosure:YES];
3122 [cell setShowSelection:YES];
3123 } else if ([package_ relationships] != nil && row-- == 0) {
3124 [cell setTitle:@"Package Relationships"];
3125 [cell setShowDisclosure:YES];
3126 [cell setShowSelection:YES];
3127 } else if ([[package_ source] trusted] && row-- == 0) {
3128 [cell setIcon:[UIImage applicationImageNamed:@"trusted.png"]];
3129 [cell setValue:@"This package has been signed."];
3130 } else _assert(false);
3131 } else if ([package_ source] != nil && group-- == 0) {
3132 Source *source = [package_ source];
3133 NSString *description = [source description];
3134
3135 if (false) {
3136 } else if (row-- == 0) {
3137 NSString *label = [source label];
3138 if (label == nil)
3139 label = [source uri];
3140 [cell setTitle:label];
3141 [cell setValue:[source version]];
3142 } else if (description != nil && ![description isEqualToString:[source label]] && row-- == 0) {
3143 [cell setValue:description];
3144 } else if ([source origin] != nil && row-- == 0) {
3145 [cell setTitle:@"Origin"];
3146 [cell setValue:[source origin]];
3147 } else _assert(false);
3148 } else _assert(false);
3149
3150 return cell;
3151 }
3152
3153 - (BOOL) canSelectRow:(int)row {
3154 return YES;
3155 }
3156
3157 - (void) tableRowSelected:(NSNotification *)notification {
3158 int row = [table_ selectedRow];
3159 if (row == INT_MAX)
3160 return;
3161
3162 #define _else else goto _label; return; } _label:
3163
3164 if (true) {
3165 if (row-- == 0) {
3166 } else if (row-- == 0) {
3167 } else if ([package_ author] != nil && row-- == 0) {
3168 [delegate_ openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?subject=%@",
3169 [[package_ author] email],
3170 [[NSString stringWithFormat:@"regarding apt package \"%@\"",
3171 [package_ name]
3172 ] stringByAddingPercentEscapes]
3173 ]]];
3174 } else if (description_ != nil && row-- == 0) {
3175 } else if ([package_ website] != nil && row-- == 0) {
3176 NSURL *url = [NSURL URLWithString:[package_ website]];
3177 BrowserView *browser = [[[BrowserView alloc] initWithBook:book_ database:database_] autorelease];
3178 [browser setDelegate:delegate_];
3179 [book_ pushPage:browser];
3180 [browser loadURL:url];
3181 } _else if ([package_ installed] != nil) {
3182 if (row-- == 0) {
3183 } else if (row-- == 0) {
3184 } else if (row-- == 0) {
3185 FileTable *files = [[[FileTable alloc] initWithBook:book_ database:database_] autorelease];
3186 [files setDelegate:delegate_];
3187 [files setPackage:package_];
3188 [book_ pushPage:files];
3189 } _else if (true) {
3190 if (row-- == 0) {
3191 } else if (row-- == 0) {
3192 } else if (row-- == 0) {
3193 } else if ([package_ size] != 0 && row-- == 0) {
3194 } else if ([package_ maintainer] != nil && row-- == 0) {
3195 [delegate_ openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?subject=%@",
3196 [[package_ maintainer] email],
3197 [[NSString stringWithFormat:@"regarding apt package \"%@\"",
3198 [package_ name]
3199 ] stringByAddingPercentEscapes]
3200 ]]];
3201 } else if ([package_ sponsor] != nil && row-- == 0) {
3202 NSURL *url = [NSURL URLWithString:[[package_ sponsor] email]];
3203 BrowserView *browser = [[[BrowserView alloc] initWithBook:book_ database:database_] autorelease];
3204 [browser setDelegate:delegate_];
3205 [book_ pushPage:browser];
3206 [browser loadURL:url];
3207 } else if ([package_ relationships] != nil && row-- == 0) {
3208 } else if ([[package_ source] trusted] && row-- == 0) {
3209 } _else if ([package_ source] != nil) {
3210 Source *source = [package_ source];
3211 NSString *description = [source description];
3212
3213 if (row-- == 0) {
3214 } else if (row-- == 0) {
3215 } else if (description != nil && ![description isEqualToString:[source label]] && row-- == 0) {
3216 } else if ([source origin] != nil && row-- == 0) {
3217 } _else _assert(false);
3218
3219 #undef _else
3220 }
3221
3222 - (void) _clickButtonWithName:(NSString *)name {
3223 if ([name isEqualToString:@"Install"])
3224 [delegate_ installPackage:package_];
3225 else if ([name isEqualToString:@"Reinstall"])
3226 [delegate_ installPackage:package_];
3227 else if ([name isEqualToString:@"Remove"])
3228 [delegate_ removePackage:package_];
3229 else if ([name isEqualToString:@"Upgrade"])
3230 [delegate_ installPackage:package_];
3231 else _assert(false);
3232 }
3233
3234 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
3235 int count = [buttons_ count];
3236 _assert(count != 0);
3237 _assert(button <= count + 1);
3238
3239 if (count != button - 1)
3240 [self _clickButtonWithName:[buttons_ objectAtIndex:(button - 1)]];
3241
3242 [sheet dismiss];
3243 }
3244
3245 - (void) _rightButtonClicked {
3246 int count = [buttons_ count];
3247 _assert(count != 0);
3248
3249 if (count == 1)
3250 [self _clickButtonWithName:[buttons_ objectAtIndex:0]];
3251 else {
3252 NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:(count + 1)];
3253 [buttons addObjectsFromArray:buttons_];
3254 [buttons addObject:@"Cancel"];
3255
3256 [delegate_ slideUp:[[[UIAlertSheet alloc]
3257 initWithTitle:nil
3258 buttons:buttons
3259 defaultButtonIndex:2
3260 delegate:self
3261 context:@"manage"
3262 ] autorelease]];
3263 }
3264 }
3265
3266 - (NSString *) rightButtonTitle {
3267 int count = [buttons_ count];
3268 return count == 0 ? nil : count != 1 ? @"Modify" : [buttons_ objectAtIndex:0];
3269 }
3270
3271 - (NSString *) title {
3272 return @"Details";
3273 }
3274
3275 - (id) initWithBook:(RVBook *)book database:(Database *)database {
3276 if ((self = [super initWithBook:book]) != nil) {
3277 database_ = database;
3278
3279 table_ = [[UIPreferencesTable alloc] initWithFrame:[self bounds]];
3280 [self addSubview:table_];
3281
3282 [table_ setDataSource:self];
3283 [table_ setDelegate:self];
3284
3285 buttons_ = [[NSMutableArray alloc] initWithCapacity:4];
3286 } return self;
3287 }
3288
3289 - (void) setPackage:(Package *)package {
3290 if (package_ != nil) {
3291 [package_ autorelease];
3292 package_ = nil;
3293 }
3294
3295 if (name_ != nil) {
3296 [name_ release];
3297 name_ = nil;
3298 }
3299
3300 if (description_ != nil) {
3301 [description_ release];
3302 description_ = nil;
3303 }
3304
3305 [buttons_ removeAllObjects];
3306
3307 if (package != nil) {
3308 package_ = [package retain];
3309 name_ = [[package id] retain];
3310
3311 NSString *description([package description]);
3312 if (description == nil)
3313 description = [package tagline];
3314 if (description != nil) {
3315 description_ = [GetTextView(description, 12, true) retain];
3316 [description_ setTextColor:Black_];
3317 }
3318
3319 [table_ reloadData];
3320
3321 if ([package_ source] == nil);
3322 else if ([package_ upgradableAndEssential:NO])
3323 [buttons_ addObject:@"Upgrade"];
3324 else if ([package_ installed] == nil)
3325 [buttons_ addObject:@"Install"];
3326 else
3327 [buttons_ addObject:@"Reinstall"];
3328 if ([package_ installed] != nil)
3329 [buttons_ addObject:@"Remove"];
3330 }
3331 }
3332
3333 - (void) resetViewAnimated:(BOOL)animated {
3334 [table_ resetViewAnimated:animated];
3335 }
3336
3337 - (void) reloadData {
3338 [self setPackage:[database_ packageWithName:name_]];
3339 [self reloadButtons];
3340 }
3341
3342 @end
3343 /* }}} */
3344 /* Package Table {{{ */
3345 @interface PackageTable : RVPage {
3346 _transient Database *database_;
3347 NSString *title_;
3348 SEL filter_;
3349 id object_;
3350 NSMutableArray *packages_;
3351 NSMutableArray *sections_;
3352 UISectionList *list_;
3353 }
3354
3355 - (id) initWithBook:(RVBook *)book database:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object;
3356
3357 - (void) setDelegate:(id)delegate;
3358 - (void) setObject:(id)object;
3359
3360 - (void) reloadData;
3361 - (void) resetCursor;
3362
3363 - (UISectionList *) list;
3364
3365 - (void) setShouldHideHeaderInShortLists:(BOOL)hide;
3366
3367 @end
3368
3369 @implementation PackageTable
3370
3371 - (void) dealloc {
3372 [list_ setDataSource:nil];
3373
3374 [title_ release];
3375 if (object_ != nil)
3376 [object_ release];
3377 [packages_ release];
3378 [sections_ release];
3379 [list_ release];
3380 [super dealloc];
3381 }
3382
3383 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
3384 return [sections_ count];
3385 }
3386
3387 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
3388 return [[sections_ objectAtIndex:section] name];
3389 }
3390
3391 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
3392 return [[sections_ objectAtIndex:section] row];
3393 }
3394
3395 - (int) numberOfRowsInTable:(UITable *)table {
3396 return [packages_ count];
3397 }
3398
3399 - (float) table:(UITable *)table heightForRow:(int)row {
3400 return [PackageCell heightForPackage:[packages_ objectAtIndex:row]];
3401 }
3402
3403 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
3404 if (reusing == nil)
3405 reusing = [[[PackageCell alloc] init] autorelease];
3406 [(PackageCell *)reusing setPackage:[packages_ objectAtIndex:row]];
3407 return reusing;
3408 }
3409
3410 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
3411 return NO;
3412 }
3413
3414 - (void) tableRowSelected:(NSNotification *)notification {
3415 int row = [[notification object] selectedRow];
3416 if (row == INT_MAX)
3417 return;
3418
3419 Package *package = [packages_ objectAtIndex:row];
3420 PackageView *view = [[[PackageView alloc] initWithBook:book_ database:database_] autorelease];
3421 [view setDelegate:delegate_];
3422 [view setPackage:package];
3423 [book_ pushPage:view];
3424 }
3425
3426 - (id) initWithBook:(RVBook *)book database:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object {
3427 if ((self = [super initWithBook:book]) != nil) {
3428 database_ = database;
3429 title_ = [title retain];
3430 filter_ = filter;
3431 object_ = object == nil ? nil : [object retain];
3432
3433 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
3434 sections_ = [[NSMutableArray arrayWithCapacity:16] retain];
3435
3436 list_ = [[UISectionList alloc] initWithFrame:[self bounds] showSectionIndex:YES];
3437 [list_ setDataSource:self];
3438
3439 UITableColumn *column = [[[UITableColumn alloc]
3440 initWithTitle:@"Name"
3441 identifier:@"name"
3442 width:[self frame].size.width
3443 ] autorelease];
3444
3445 UITable *table = [list_ table];
3446 [table setSeparatorStyle:1];
3447 [table addTableColumn:column];
3448 [table setDelegate:self];
3449 [table setReusesTableCells:YES];
3450
3451 [self addSubview:list_];
3452 [self reloadData];
3453 } return self;
3454 }
3455
3456 - (void) setDelegate:(id)delegate {
3457 delegate_ = delegate;
3458 }
3459
3460 - (void) setObject:(id)object {
3461 if (object_ != nil)
3462 [object_ release];
3463 if (object == nil)
3464 object_ = nil;
3465 else
3466 object_ = [object retain];
3467 }
3468
3469 - (void) reloadData {
3470 NSArray *packages = [database_ packages];
3471
3472 [packages_ removeAllObjects];
3473 [sections_ removeAllObjects];
3474
3475 for (size_t i(0); i != [packages count]; ++i) {
3476 Package *package([packages objectAtIndex:i]);
3477 if ([[package performSelector:filter_ withObject:object_] boolValue])
3478 [packages_ addObject:package];
3479 }
3480
3481 Section *section = nil;
3482
3483 for (size_t offset(0); offset != [packages_ count]; ++offset) {
3484 Package *package = [packages_ objectAtIndex:offset];
3485 NSString *name = [package index];
3486
3487 if (section == nil || ![[section name] isEqualToString:name]) {
3488 section = [[[Section alloc] initWithName:name row:offset] autorelease];
3489 [sections_ addObject:section];
3490 }
3491
3492 [section addToCount];
3493 }
3494
3495 [list_ reloadData];
3496 }
3497
3498 - (NSString *) title {
3499 return title_;
3500 }
3501
3502 - (void) resetViewAnimated:(BOOL)animated {
3503 [list_ resetViewAnimated:animated];
3504 }
3505
3506 - (void) resetCursor {
3507 [[list_ table] scrollPointVisibleAtTopLeft:CGPointMake(0, 0) animated:NO];
3508 }
3509
3510 - (UISectionList *) list {
3511 return list_;
3512 }
3513
3514 - (void) setShouldHideHeaderInShortLists:(BOOL)hide {
3515 [list_ setShouldHideHeaderInShortLists:hide];
3516 }
3517
3518 @end
3519 /* }}} */
3520
3521 /* Browser Implementation {{{ */
3522 @implementation BrowserView
3523
3524 - (void) dealloc {
3525 WebView *webview = [webview_ webView];
3526 [webview setFrameLoadDelegate:nil];
3527 [webview setResourceLoadDelegate:nil];
3528 [webview setUIDelegate:nil];
3529
3530 [scroller_ setDelegate:nil];
3531 [webview_ setDelegate:nil];
3532
3533 [scroller_ release];
3534 [webview_ release];
3535 [urls_ release];
3536 [indicator_ release];
3537 if (title_ != nil)
3538 [title_ release];
3539 [super dealloc];
3540 }
3541
3542 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
3543 NSMutableURLRequest *request = [NSMutableURLRequest
3544 requestWithURL:url
3545 cachePolicy:policy
3546 timeoutInterval:30.0
3547 ];
3548
3549 [request addValue:[NSString stringWithUTF8String:Firmware_] forHTTPHeaderField:@"X-Firmware"];
3550 [request addValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
3551 [request addValue:[NSString stringWithUTF8String:SerialNumber_] forHTTPHeaderField:@"X-Serial-Number"];
3552
3553 [self loadRequest:request];
3554 }
3555
3556
3557 - (void) loadURL:(NSURL *)url {
3558 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
3559 }
3560
3561 // XXX: this needs to add the headers
3562 - (NSURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
3563 return request;
3564 }
3565
3566 - (void) loadRequest:(NSURLRequest *)request {
3567 pushed_ = true;
3568 [webview_ loadRequest:request];
3569 }
3570
3571 - (void) reloadURL {
3572 NSURL *url = [[[urls_ lastObject] retain] autorelease];
3573 [urls_ removeLastObject];
3574 [self loadURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData];
3575 }
3576
3577 - (WebView *) webView {
3578 return [webview_ webView];
3579 }
3580
3581 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
3582 [scroller_ setContentSize:frame.size];
3583 }
3584
3585 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
3586 [self view:sender didSetFrame:frame];
3587 }
3588
3589 - (void) getAppTapp:(NSString *)href {
3590 if ([href hasPrefix:@"apptapp://package/"]) {
3591 NSString *name = [href substringFromIndex:18];
3592 Package *package = [database_ packageWithName:name];
3593 if (package == nil) {
3594 UIAlertSheet *sheet = [[[UIAlertSheet alloc]
3595 initWithTitle:@"Cannot Locate Package"
3596 buttons:[NSArray arrayWithObjects:@"Close", nil]
3597 defaultButtonIndex:0
3598 delegate:self
3599 context:@"missing"
3600 ] autorelease];
3601
3602 [sheet setBodyText:[NSString stringWithFormat:
3603 @"The package %@ cannot be found in your current sources. I might recommend installing more sources."
3604 , name]];
3605
3606 [sheet popupAlertAnimated:YES];
3607 } else {
3608 [self setBackButtonTitle:title_];
3609 PackageView *view = [[[PackageView alloc] initWithBook:book_ database:database_] autorelease];
3610 [view setDelegate:delegate_];
3611 [view setPackage:package];
3612 [book_ pushPage:view];
3613 }
3614 }
3615 }
3616
3617 - (void) webView:(WebView *)sender willClickElement:(id)element {
3618 if (![element respondsToSelector:@selector(href)])
3619 return;
3620 NSString *href = [element href];
3621 if (href == nil)
3622 return;
3623 if ([href hasPrefix:@"apptapp://package/"])
3624 [self getAppTapp:href];
3625 }
3626
3627 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource {
3628 if ([[[request URL] scheme] isEqualToString:@"apptapp"]) {
3629 [self getAppTapp:[[request URL] absoluteString]];
3630 return nil;
3631 }
3632
3633 if (!pushed_) {
3634 pushed_ = true;
3635 [book_ pushPage:self];
3636 }
3637
3638 return [self _addHeadersToRequest:request];
3639 }
3640
3641 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
3642 if (request != nil && [[[request URL] scheme] isEqualToString:@"apptapp"])
3643 return nil;
3644 else {
3645 [self setBackButtonTitle:title_];
3646
3647 BrowserView *browser = [[[BrowserView alloc] initWithBook:book_ database:database_] autorelease];
3648 [browser setDelegate:delegate_];
3649
3650 if (request != nil) {
3651 [browser loadRequest:[self _addHeadersToRequest:request]];
3652 [book_ pushPage:browser];
3653 }
3654
3655 return [browser webView];
3656 }
3657 }
3658
3659 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
3660 title_ = [title retain];
3661 [self setTitle:title];
3662 }
3663
3664 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
3665 if ([frame parentFrame] != nil)
3666 return;
3667
3668 reloading_ = false;
3669 loading_ = true;
3670 [indicator_ startAnimation];
3671 [self reloadButtons];
3672
3673 if (title_ != nil) {
3674 [title_ release];
3675 title_ = nil;
3676 }
3677
3678 [self setTitle:@"Loading..."];
3679
3680 WebView *webview = [webview_ webView];
3681 NSString *href = [webview mainFrameURL];
3682 [urls_ addObject:[NSURL URLWithString:href]];
3683
3684 CGRect webrect = [scroller_ frame];
3685 webrect.size.height = 0;
3686 [webview_ setFrame:webrect];
3687 }
3688
3689 - (void) _finishLoading {
3690 if (!reloading_) {
3691 loading_ = false;
3692 [indicator_ stopAnimation];
3693 [self reloadButtons];
3694 }
3695 }
3696
3697 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
3698 if ([frame parentFrame] != nil)
3699 return;
3700 [self _finishLoading];
3701 }
3702
3703 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
3704 if ([frame parentFrame] != nil)
3705 return;
3706 [self setTitle:[error localizedDescription]];
3707 [self _finishLoading];
3708 }
3709
3710 - (id) initWithBook:(RVBook *)book database:(Database *)database {
3711 if ((self = [super initWithBook:book]) != nil) {
3712 database_ = database;
3713 loading_ = false;
3714
3715 struct CGRect bounds = [self bounds];
3716
3717 UIImageView *pinstripe = [[[UIImageView alloc] initWithFrame:bounds] autorelease];
3718 [pinstripe setImage:[UIImage applicationImageNamed:@"pinstripe.png"]];
3719 [self addSubview:pinstripe];
3720
3721 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
3722 [self addSubview:scroller_];
3723
3724 [scroller_ setScrollingEnabled:YES];
3725 [scroller_ setAdjustForContentSizeChange:YES];
3726 [scroller_ setClipsSubviews:YES];
3727 [scroller_ setAllowsRubberBanding:YES];
3728 [scroller_ setScrollDecelerationFactor:0.99];
3729 [scroller_ setDelegate:self];
3730
3731 CGRect webrect = [scroller_ bounds];
3732 webrect.size.height = 0;
3733
3734 webview_ = [[UIWebView alloc] initWithFrame:webrect];
3735 [scroller_ addSubview:webview_];
3736
3737 [webview_ setTilingEnabled:YES];
3738 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
3739 [webview_ setAutoresizes:YES];
3740 [webview_ setDelegate:self];
3741 //[webview_ setEnabledGestures:2];
3742
3743 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:kUIProgressIndicatorStyleMediumWhite];
3744 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 42, indsize.width, indsize.height)];
3745 [indicator_ setStyle:kUIProgressIndicatorStyleMediumWhite];
3746
3747 Package *package([database_ packageWithName:@"cydia"]);
3748 NSString *application = package == nil ? @"Cydia" : [NSString
3749 stringWithFormat:@"Cydia/%@",
3750 [package installed]
3751 ];
3752
3753 WebView *webview = [webview_ webView];
3754 [webview setApplicationNameForUserAgent:application];
3755 [webview setFrameLoadDelegate:self];
3756 [webview setResourceLoadDelegate:self];
3757 [webview setUIDelegate:self];
3758
3759 urls_ = [[NSMutableArray alloc] initWithCapacity:16];
3760 } return self;
3761 }
3762
3763 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
3764 [sheet dismiss];
3765 }
3766
3767 - (void) _leftButtonClicked {
3768 UIAlertSheet *sheet = [[[UIAlertSheet alloc]
3769 initWithTitle:@"About Cydia Installer"
3770 buttons:[NSArray arrayWithObjects:@"Close", nil]
3771 defaultButtonIndex:0
3772 delegate:self
3773 context:@"about"
3774 ] autorelease];
3775
3776 [sheet setBodyText:
3777 @"Copyright (C) 2008\n"
3778 "Jay Freeman (saurik)\n"
3779 "saurik@saurik.com\n"
3780 "http://www.saurik.com/\n"
3781 "\n"
3782 "The Okori Group\n"
3783 "http://www.theokorigroup.com/\n"
3784 "\n"
3785 "College of Creative Studies,\n"
3786 "University of California,\n"
3787 "Santa Barbara\n"
3788 "http://www.ccs.ucsb.edu/"
3789 ];
3790
3791 [sheet popupAlertAnimated:YES];
3792 }
3793
3794 - (void) _rightButtonClicked {
3795 reloading_ = true;
3796 [self reloadURL];
3797 }
3798
3799 - (NSString *) leftButtonTitle {
3800 return @"About";
3801 }
3802
3803 - (NSString *) rightButtonTitle {
3804 return loading_ ? @"" : @"Reload";
3805 }
3806
3807 - (NSString *) title {
3808 return nil;
3809 }
3810
3811 - (NSString *) backButtonTitle {
3812 return @"Browser";
3813 }
3814
3815 - (void) setPageActive:(BOOL)active {
3816 if (active)
3817 [book_ addSubview:indicator_];
3818 else
3819 [indicator_ removeFromSuperview];
3820 }
3821
3822 - (void) resetViewAnimated:(BOOL)animated {
3823 }
3824
3825 - (void) setPushed:(bool)pushed {
3826 pushed_ = pushed;
3827 }
3828
3829 @end
3830 /* }}} */
3831
3832 @interface CYBook : RVBook <
3833 ProgressDelegate
3834 > {
3835 _transient Database *database_;
3836 UIView *overlay_;
3837 UIProgressIndicator *indicator_;
3838 UITextLabel *prompt_;
3839 UIProgressBar *progress_;
3840 bool updating_;
3841 }
3842
3843 - (id) initWithFrame:(CGRect)frame database:(Database *)database;
3844 - (void) update;
3845 - (BOOL) updating;
3846
3847 @end
3848
3849 /* Source Cell {{{ */
3850 @interface SourceCell : UITableCell {
3851 UITextLabel *description_;
3852 UIRightTextLabel *label_;
3853 UITextLabel *origin_;
3854 }
3855
3856 - (void) dealloc;
3857
3858 - (SourceCell *) initWithSource:(Source *)source;
3859
3860 - (void) _setSelected:(float)fraction;
3861 - (void) setSelected:(BOOL)selected;
3862 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade;
3863 - (void) _setSelectionFadeFraction:(float)fraction;
3864
3865 @end
3866
3867 @implementation SourceCell
3868
3869 - (void) dealloc {
3870 [description_ release];
3871 [label_ release];
3872 [origin_ release];
3873 [super dealloc];
3874 }
3875
3876 - (SourceCell *) initWithSource:(Source *)source {
3877 if ((self = [super init]) != nil) {
3878 GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 20);
3879 GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 14);
3880
3881 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
3882 float clear[] = {0, 0, 0, 0};
3883
3884 NSString *description = [source description];
3885 if (description == nil)
3886 description = [source uri];
3887
3888 description_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 7, 270, 25)];
3889 [description_ setBackgroundColor:CGColorCreate(space, clear)];
3890 [description_ setFont:bold];
3891 [description_ setText:description];
3892
3893 NSString *label = [source label];
3894 if (label == nil)
3895 label = [source type];
3896
3897 label_ = [[UIRightTextLabel alloc] initWithFrame:CGRectMake(290, 32, 90, 25)];
3898 [label_ setBackgroundColor:CGColorCreate(space, clear)];
3899 [label_ setFont:small];
3900 [label_ setText:label];
3901
3902 NSString *origin = [source origin];
3903 if (origin == nil)
3904 origin = [source distribution];
3905
3906 origin_ = [[UITextLabel alloc] initWithFrame:CGRectMake(13, 35, 315, 20)];
3907 [origin_ setBackgroundColor:CGColorCreate(space, clear)];
3908 [origin_ setFont:small];
3909 [origin_ setText:origin];
3910
3911 [self addSubview:description_];
3912 [self addSubview:label_];
3913 [self addSubview:origin_];
3914
3915 CFRelease(small);
3916 CFRelease(bold);
3917 } return self;
3918 }
3919
3920 - (void) _setSelected:(float)fraction {
3921 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
3922
3923 float black[] = {
3924 Interpolate(0.0, 1.0, fraction),
3925 Interpolate(0.0, 1.0, fraction),
3926 Interpolate(0.0, 1.0, fraction),
3927 1.0};
3928
3929 float blue[] = {
3930 Interpolate(0.2, 1.0, fraction),
3931 Interpolate(0.2, 1.0, fraction),
3932 Interpolate(1.0, 1.0, fraction),
3933 1.0};
3934
3935 float gray[] = {
3936 Interpolate(0.4, 1.0, fraction),
3937 Interpolate(0.4, 1.0, fraction),
3938 Interpolate(0.4, 1.0, fraction),
3939 1.0};
3940
3941 [description_ setColor:CGColorCreate(space, black)];
3942 [label_ setColor:CGColorCreate(space, blue)];
3943 [origin_ setColor:CGColorCreate(space, gray)];
3944 }
3945
3946 - (void) setSelected:(BOOL)selected {
3947 [self _setSelected:(selected ? 1.0 : 0.0)];
3948 [super setSelected:selected];
3949 }
3950
3951 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade {
3952 if (!fade)
3953 [self _setSelected:(selected ? 1.0 : 0.0)];
3954 [super setSelected:selected withFade:fade];
3955 }
3956
3957 - (void) _setSelectionFadeFraction:(float)fraction {
3958 [self _setSelected:fraction];
3959 [super _setSelectionFadeFraction:fraction];
3960 }
3961
3962 @end
3963 /* }}} */
3964 /* Source Table {{{ */
3965 @interface SourceTable : RVPage {
3966 _transient Database *database_;
3967 UISectionList *list_;
3968 NSMutableArray *sources_;
3969 UIAlertSheet *alert_;
3970 }
3971
3972 - (id) initWithBook:(RVBook *)book database:(Database *)database;
3973
3974 @end
3975
3976 @implementation SourceTable
3977
3978 - (void) dealloc {
3979 [list_ setDataSource:nil];
3980
3981 if (sources_ != nil)
3982 [sources_ release];
3983 [list_ release];
3984 [super dealloc];
3985 }
3986
3987 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
3988 return 1;
3989 }
3990
3991 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
3992 return @"Sources";
3993 }
3994
3995 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
3996 return 0;
3997 }
3998
3999 - (int) numberOfRowsInTable:(UITable *)table {
4000 return [sources_ count];
4001 }
4002
4003 - (float) table:(UITable *)table heightForRow:(int)row {
4004 return 64;
4005 }
4006
4007 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col {
4008 return [[[SourceCell alloc] initWithSource:[sources_ objectAtIndex:row]] autorelease];
4009 }
4010
4011 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
4012 return NO;
4013 }
4014
4015 - (void) tableRowSelected:(NSNotification*)notification {
4016 UITable *table([list_ table]);
4017 int row([table selectedRow]);
4018 if (row == INT_MAX)
4019 return;
4020
4021 [table selectRow:-1 byExtendingSelection:NO withFade:YES];
4022 }
4023
4024 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4025 if ((self = [super initWithBook:book]) != nil) {
4026 database_ = database;
4027 sources_ = nil;
4028
4029 list_ = [[UISectionList alloc] initWithFrame:[self bounds]];
4030
4031 [self addSubview:list_];
4032
4033 [list_ setDataSource:self];
4034 [list_ setShouldHideHeaderInShortLists:NO];
4035
4036 UITableColumn *column = [[UITableColumn alloc]
4037 initWithTitle:@"Name"
4038 identifier:@"name"
4039 width:[self frame].size.width
4040 ];
4041
4042 UITable *table = [list_ table];
4043 [table setSeparatorStyle:1];
4044 [table addTableColumn:column];
4045 [table setDelegate:self];
4046 } return self;
4047 }
4048
4049 - (void) reloadData {
4050 pkgSourceList list;
4051 _assert(list.ReadMainList());
4052
4053 if (sources_ != nil)
4054 [sources_ release];
4055
4056 sources_ = [[NSMutableArray arrayWithCapacity:16] retain];
4057 for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source)
4058 [sources_ addObject:[[[Source alloc] initWithMetaIndex:*source] autorelease]];
4059
4060 [list_ reloadData];
4061 }
4062
4063 - (void) resetViewAnimated:(BOOL)animated {
4064 [list_ resetViewAnimated:animated];
4065 }
4066
4067 - (NSString *) leftTitle {
4068 return @"Refresh All";
4069 }
4070
4071 - (NSString *) rightTitle {
4072 return @"Edit";
4073 }
4074
4075 @end
4076 /* }}} */
4077
4078 /* Install View {{{ */
4079 @interface InstallView : RVPage {
4080 _transient Database *database_;
4081 NSMutableArray *packages_;
4082 NSMutableArray *sections_;
4083 UITable *list_;
4084 UIView *accessory_;
4085 }
4086
4087 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4088 - (void) reloadData;
4089
4090 @end
4091
4092 @implementation InstallView
4093
4094 - (void) dealloc {
4095 [list_ setDataSource:nil];
4096 [list_ setDelegate:nil];
4097
4098 [packages_ release];
4099 [sections_ release];
4100 [list_ release];
4101 [accessory_ release];
4102 [super dealloc];
4103 }
4104
4105 - (int) numberOfRowsInTable:(UITable *)table {
4106 return [sections_ count] + 1;
4107 }
4108
4109 - (float) table:(UITable *)table heightForRow:(int)row {
4110 return 45;
4111 }
4112
4113 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
4114 if (reusing == nil)
4115 reusing = [[[SectionCell alloc] init] autorelease];
4116 [(SectionCell *)reusing setSection:(row == 0 ? nil : [sections_ objectAtIndex:(row - 1)])];
4117 return reusing;
4118 }
4119
4120 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
4121 return YES;
4122 }
4123
4124 - (void) tableRowSelected:(NSNotification *)notification {
4125 int row = [[notification object] selectedRow];
4126 if (row == INT_MAX)
4127 return;
4128
4129 Section *section;
4130 NSString *name;
4131 NSString *title;
4132
4133 if (row == 0) {
4134 section = nil;
4135 name = nil;
4136 title = @"All Packages";
4137 } else {
4138 section = [sections_ objectAtIndex:(row - 1)];
4139 name = [section name];
4140
4141 if (name != nil)
4142 title = name;
4143 else {
4144 name = @"";
4145 title = @"(No Section)";
4146 }
4147 }
4148
4149 PackageTable *table = [[[PackageTable alloc]
4150 initWithBook:book_
4151 database:database_
4152 title:title
4153 filter:@selector(isUninstalledInSection:)
4154 with:name
4155 ] autorelease];
4156
4157 [table setDelegate:delegate_];
4158
4159 [book_ pushPage:table];
4160 }
4161
4162 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4163 if ((self = [super initWithBook:book]) != nil) {
4164 database_ = database;
4165
4166 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
4167 sections_ = [[NSMutableArray arrayWithCapacity:16] retain];
4168
4169 list_ = [[UITable alloc] initWithFrame:[self bounds]];
4170 [self addSubview:list_];
4171
4172 UITableColumn *column = [[[UITableColumn alloc]
4173 initWithTitle:@"Name"
4174 identifier:@"name"
4175 width:[self frame].size.width
4176 ] autorelease];
4177
4178 [list_ setDataSource:self];
4179 [list_ setSeparatorStyle:1];
4180 [list_ addTableColumn:column];
4181 [list_ setDelegate:self];
4182 [list_ setReusesTableCells:YES];
4183
4184 [self reloadData];
4185 } return self;
4186 }
4187
4188 - (void) reloadData {
4189 NSArray *packages = [database_ packages];
4190
4191 [packages_ removeAllObjects];
4192 [sections_ removeAllObjects];
4193
4194 for (size_t i(0); i != [packages count]; ++i) {
4195 Package *package([packages objectAtIndex:i]);
4196 if ([package valid] && [package installed] == nil)
4197 [packages_ addObject:package];
4198 }
4199
4200 [packages_ sortUsingSelector:@selector(compareBySection:)];
4201
4202 Section *section = nil;
4203 for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) {
4204 Package *package = [packages_ objectAtIndex:offset];
4205 NSString *name = [package section];
4206
4207 if (section == nil || name != nil && ![[section name] isEqualToString:name]) {
4208 section = [[[Section alloc] initWithName:name row:offset] autorelease];
4209 [sections_ addObject:section];
4210 }
4211
4212 [section addToCount];
4213 }
4214
4215 [list_ reloadData];
4216 }
4217
4218 - (void) resetViewAnimated:(BOOL)animated {
4219 [list_ resetViewAnimated:animated];
4220 }
4221
4222 - (NSString *) title {
4223 return @"Install";
4224 }
4225
4226 - (NSString *) backButtonTitle {
4227 return @"Sections";
4228 }
4229
4230 - (UIView *) accessoryView {
4231 return accessory_;
4232 }
4233
4234 @end
4235 /* }}} */
4236 /* Changes View {{{ */
4237 @interface ChangesView : RVPage {
4238 _transient Database *database_;
4239 NSMutableArray *packages_;
4240 NSMutableArray *sections_;
4241 UISectionList *list_;
4242 unsigned upgrades_;
4243 }
4244
4245 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4246 - (void) reloadData;
4247
4248 @end
4249
4250 @implementation ChangesView
4251
4252 - (void) dealloc {
4253 [[list_ table] setDelegate:nil];
4254 [list_ setDataSource:nil];
4255
4256 [packages_ release];
4257 [sections_ release];
4258 [list_ release];
4259 [super dealloc];
4260 }
4261
4262 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
4263 return [sections_ count];
4264 }
4265
4266 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
4267 return [[sections_ objectAtIndex:section] name];
4268 }
4269
4270 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
4271 return [[sections_ objectAtIndex:section] row];
4272 }
4273
4274 - (int) numberOfRowsInTable:(UITable *)table {
4275 return [packages_ count];
4276 }
4277
4278 - (float) table:(UITable *)table heightForRow:(int)row {
4279 return [PackageCell heightForPackage:[packages_ objectAtIndex:row]];
4280 }
4281
4282 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
4283 if (reusing == nil)
4284 reusing = [[[PackageCell alloc] init] autorelease];
4285 [(PackageCell *)reusing setPackage:[packages_ objectAtIndex:row]];
4286 return reusing;
4287 }
4288
4289 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
4290 return NO;
4291 }
4292
4293 - (void) tableRowSelected:(NSNotification *)notification {
4294 int row = [[notification object] selectedRow];
4295 if (row == INT_MAX)
4296 return;
4297 Package *package = [packages_ objectAtIndex:row];
4298 PackageView *view = [[[PackageView alloc] initWithBook:book_ database:database_] autorelease];
4299 [view setDelegate:delegate_];
4300 [view setPackage:package];
4301 [book_ pushPage:view];
4302 }
4303
4304 - (void) _leftButtonClicked {
4305 [(CYBook *)book_ update];
4306 [self reloadButtons];
4307 }
4308
4309 - (void) _rightButtonClicked {
4310 [delegate_ distUpgrade];
4311 }
4312
4313 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4314 if ((self = [super initWithBook:book]) != nil) {
4315 database_ = database;
4316
4317 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
4318 sections_ = [[NSMutableArray arrayWithCapacity:16] retain];
4319
4320 list_ = [[UISectionList alloc] initWithFrame:[self bounds] showSectionIndex:NO];
4321 [self addSubview:list_];
4322
4323 [list_ setShouldHideHeaderInShortLists:NO];
4324 [list_ setDataSource:self];
4325 //[list_ setSectionListStyle:1];
4326
4327 UITableColumn *column = [[[UITableColumn alloc]
4328 initWithTitle:@"Name"
4329 identifier:@"name"
4330 width:[self frame].size.width
4331 ] autorelease];
4332
4333 UITable *table = [list_ table];
4334 [table setSeparatorStyle:1];
4335 [table addTableColumn:column];
4336 [table setDelegate:self];
4337 [table setReusesTableCells:YES];
4338
4339 [self reloadData];
4340 } return self;
4341 }
4342
4343 - (void) reloadData {
4344 NSArray *packages = [database_ packages];
4345
4346 [packages_ removeAllObjects];
4347 [sections_ removeAllObjects];
4348
4349 for (size_t i(0); i != [packages count]; ++i) {
4350 Package *package([packages objectAtIndex:i]);
4351 if ([package installed] == nil && [package valid] || [package upgradableAndEssential:NO])
4352 [packages_ addObject:package];
4353 }
4354
4355 [packages_ sortUsingSelector:@selector(compareForChanges:)];
4356
4357 Section *upgradable = [[[Section alloc] initWithName:@"Available Upgrades" row:0] autorelease];
4358 Section *section = nil;
4359
4360 upgrades_ = 0;
4361 bool unseens = false;
4362
4363 CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle);
4364
4365 for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) {
4366 Package *package = [packages_ objectAtIndex:offset];
4367
4368 if ([package upgradableAndEssential:YES]) {
4369 ++upgrades_;
4370 [upgradable addToCount];
4371 } else {
4372 unseens = true;
4373 NSDate *seen = [package seen];
4374
4375 NSString *name;
4376
4377 if (seen == nil)
4378 name = [@"n/a ?" retain];
4379 else {
4380 name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) seen);
4381 }
4382
4383 if (section == nil || ![[section name] isEqualToString:name]) {
4384 section = [[[Section alloc] initWithName:name row:offset] autorelease];
4385 [sections_ addObject:section];
4386 }
4387
4388 [name release];
4389 [section addToCount];
4390 }
4391 }
4392
4393 CFRelease(formatter);
4394
4395 if (unseens) {
4396 Section *last = [sections_ lastObject];
4397 size_t count = [last count];
4398 [packages_ removeObjectsInRange:NSMakeRange([packages_ count] - count, count)];
4399 [sections_ removeLastObject];
4400 }
4401
4402 if (upgrades_ != 0)
4403 [sections_ insertObject:upgradable atIndex:0];
4404
4405 [list_ reloadData];
4406 [self reloadButtons];
4407 }
4408
4409 - (void) resetViewAnimated:(BOOL)animated {
4410 [list_ resetViewAnimated:animated];
4411 }
4412
4413 - (NSString *) leftButtonTitle {
4414 return [(CYBook *)book_ updating] ? nil : @"Refresh";
4415 }
4416
4417 - (NSString *) rightButtonTitle {
4418 return upgrades_ == 0 ? nil : [NSString stringWithFormat:@"Upgrade All (%u)", upgrades_];
4419 }
4420
4421 - (NSString *) title {
4422 return @"Changes";
4423 }
4424
4425 @end
4426 /* }}} */
4427 /* Manage View {{{ */
4428 @interface ManageView : RVPage {
4429 _transient Database *database_;
4430 PackageTable *packages_;
4431 SourceTable *sources_;
4432 }
4433
4434 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4435
4436 @end
4437
4438 @implementation ManageView
4439
4440 - (void) dealloc {
4441 [packages_ release];
4442 [sources_ release];
4443 [super dealloc];
4444 }
4445
4446 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4447 if ((self = [super initWithBook:book]) != nil) {
4448 database_ = database;
4449
4450 packages_ = [[PackageTable alloc]
4451 initWithBook:book
4452 database:database
4453 title:nil
4454 filter:@selector(isInstalledInSection:)
4455 with:nil
4456 ];
4457
4458 sources_ = [[SourceTable alloc]
4459 initWithBook:book
4460 database:database
4461 ];
4462
4463 [self addSubview:packages_];
4464 } return self;
4465 }
4466
4467 - (void) resetViewAnimated:(BOOL)animated {
4468 [packages_ resetViewAnimated:animated];
4469 [sources_ resetViewAnimated:animated];
4470 }
4471
4472 - (void) reloadData {
4473 [packages_ reloadData];
4474 [sources_ reloadData];
4475 }
4476
4477 - (NSString *) title {
4478 return @"Installed Packages";
4479 }
4480
4481 - (NSString *) backButtonTitle {
4482 return @"Packages";
4483 }
4484
4485 - (void) setDelegate:(id)delegate {
4486 [super setDelegate:delegate];
4487 [packages_ setDelegate:delegate];
4488 [sources_ setDelegate:delegate];
4489 }
4490
4491 @end
4492 /* }}} */
4493 /* Search View {{{ */
4494 @protocol SearchViewDelegate
4495 - (void) showKeyboard:(BOOL)show;
4496 @end
4497
4498 @interface SearchView : RVPage {
4499 UIView *accessory_;
4500 UISearchField *field_;
4501 UITransitionView *transition_;
4502 PackageTable *table_;
4503 UIPreferencesTable *advanced_;
4504 UIView *dimmed_;
4505 bool flipped_;
4506 bool reload_;
4507 }
4508
4509 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4510 - (void) reloadData;
4511
4512 @end
4513
4514 @implementation SearchView
4515
4516 - (void) dealloc {
4517 #ifndef __OBJC2__
4518 [[field_ textTraits] setEditingDelegate:nil];
4519 #endif
4520 [field_ setDelegate:nil];
4521
4522 [accessory_ release];
4523 [field_ release];
4524 [transition_ release];
4525 [table_ release];
4526 [advanced_ release];
4527 [dimmed_ release];
4528 [super dealloc];
4529 }
4530
4531 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
4532 return 1;
4533 }
4534
4535 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group {
4536 switch (group) {
4537 case 0: return @"Advanced Search (Coming Soon!)";
4538
4539 default: _assert(false);
4540 }
4541 }
4542
4543 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
4544 switch (group) {
4545 case 0: return 0;
4546
4547 default: _assert(false);
4548 }
4549 }
4550
4551 - (void) _showKeyboard:(BOOL)show {
4552 CGSize keysize = [UIKeyboard defaultSize];
4553 CGRect keydown = [book_ pageBounds];
4554 CGRect keyup = keydown;
4555 keyup.size.height -= keysize.height - ButtonBarHeight_;
4556
4557 float delay = KeyboardTime_ * ButtonBarHeight_ / keysize.height;
4558
4559 UIFrameAnimation *animation = [[[UIFrameAnimation alloc] initWithTarget:[table_ list]] autorelease];
4560 [animation setSignificantRectFields:8];
4561
4562 if (show) {
4563 [animation setStartFrame:keydown];
4564 [animation setEndFrame:keyup];
4565 } else {
4566 [animation setStartFrame:keyup];
4567 [animation setEndFrame:keydown];
4568 }
4569
4570 UIAnimator *animator = [UIAnimator sharedAnimator];
4571
4572 [animator
4573 addAnimations:[NSArray arrayWithObjects:animation, nil]
4574 withDuration:(KeyboardTime_ - delay)
4575 start:!show
4576 ];
4577
4578 if (show)
4579 [animator performSelector:@selector(startAnimation:) withObject:animation afterDelay:delay];
4580
4581 #ifndef __OBJC2__
4582 [delegate_ showKeyboard:show];
4583 #endif
4584 }
4585
4586 - (void) textFieldDidBecomeFirstResponder:(UITextField *)field {
4587 [self _showKeyboard:YES];
4588 }
4589
4590 - (void) textFieldDidResignFirstResponder:(UITextField *)field {
4591 [self _showKeyboard:NO];
4592 }
4593
4594 - (void) keyboardInputChanged:(UIFieldEditor *)editor {
4595 if (reload_) {
4596 NSString *text([field_ text]);
4597 [field_ setClearButtonStyle:(text == nil || [text length] == 0 ? 0 : 2)];
4598 [self reloadData];
4599 reload_ = false;
4600 }
4601 }
4602
4603 - (void) textFieldClearButtonPressed:(UITextField *)field {
4604 reload_ = true;
4605 }
4606
4607 - (void) keyboardInputShouldDelete:(id)input {
4608 reload_ = true;
4609 }
4610
4611 - (BOOL) keyboardInput:(id)input shouldInsertText:(NSString *)text isMarkedText:(int)marked {
4612 if ([text length] != 1 || [text characterAtIndex:0] != '\n') {
4613 reload_ = true;
4614 return YES;
4615 } else {
4616 [field_ resignFirstResponder];
4617 return NO;
4618 }
4619 }
4620
4621 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4622 if ((self = [super initWithBook:book]) != nil) {
4623 CGRect pageBounds = [book_ pageBounds];
4624
4625 /*UIImageView *pinstripe = [[[UIImageView alloc] initWithFrame:pageBounds] autorelease];
4626 [pinstripe setImage:[UIImage applicationImageNamed:@"pinstripe.png"]];
4627 [self addSubview:pinstripe];*/
4628
4629 transition_ = [[UITransitionView alloc] initWithFrame:pageBounds];
4630 [self addSubview:transition_];
4631
4632 advanced_ = [[UIPreferencesTable alloc] initWithFrame:pageBounds];
4633
4634 [advanced_ setReusesTableCells:YES];
4635 [advanced_ setDataSource:self];
4636 [advanced_ reloadData];
4637
4638 dimmed_ = [[UIView alloc] initWithFrame:pageBounds];
4639 CGColor dimmed(space_, 0, 0, 0, 0.5);
4640 [dimmed_ setBackgroundColor:dimmed];
4641
4642 table_ = [[PackageTable alloc]
4643 initWithBook:book
4644 database:database
4645 title:nil
4646 filter:@selector(isSearchedForBy:)
4647 with:nil
4648 ];
4649
4650 [table_ setShouldHideHeaderInShortLists:NO];
4651 [transition_ transition:0 toView:table_];
4652
4653 CGRect cnfrect = {{
4654 #ifdef __OBJC2__
4655 6 +
4656 #endif
4657 1, 38}, {17, 18}};
4658
4659 CGRect area;
4660 area.origin.x = cnfrect.origin.x + cnfrect.size.width + 14;
4661 area.origin.y = 30;
4662
4663 area.size.width =
4664 #ifdef __OBJC2__
4665 8 +
4666 #endif
4667 [self bounds].size.width - area.origin.x - 18;
4668
4669 area.size.height = [UISearchField defaultHeight];
4670
4671 field_ = [[UISearchField alloc] initWithFrame:area];
4672
4673 GSFontRef font = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 16);
4674 [field_ setFont:font];
4675 CFRelease(font);
4676
4677 [field_ setPlaceholder:@"Package Names & Descriptions"];
4678 [field_ setDelegate:self];
4679
4680 #ifdef __OBJC2__
4681 [field_ setPaddingTop:3];
4682 #else
4683 [field_ setPaddingTop:5];
4684 #endif
4685
4686 #ifndef __OBJC2__
4687 UITextTraits *traits = [field_ textTraits];
4688 [traits setEditingDelegate:self];
4689 [traits setReturnKeyType:6];
4690 [traits setAutoCapsType:0];
4691 [traits setAutoCorrectionType:1];
4692 #endif
4693
4694 CGRect accrect = {{0, 6}, {6 + cnfrect.size.width + 6 + area.size.width + 6, area.size.height + 30}};
4695
4696 accessory_ = [[UIView alloc] initWithFrame:accrect];
4697 [accessory_ addSubview:field_];
4698
4699 UIPushButton *configure = [[[UIPushButton alloc] initWithFrame:cnfrect] autorelease];
4700 [configure setShowPressFeedback:YES];
4701 [configure setImage:[UIImage applicationImageNamed:@"advanced.png"]];
4702 [configure addTarget:self action:@selector(configurePushed) forEvents:1];
4703 [accessory_ addSubview:configure];
4704 } return self;
4705 }
4706
4707 - (void) flipPage {
4708 #ifndef __OBJC2__
4709 LKAnimation *animation = [LKTransition animation];
4710 [animation setType:@"oglFlip"];
4711 [animation setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
4712 [animation setFillMode:@"extended"];
4713 [animation setTransitionFlags:3];
4714 [animation setDuration:10];
4715 [animation setSpeed:0.35];
4716 [animation setSubtype:(flipped_ ? @"fromLeft" : @"fromRight")];
4717 [[transition_ _layer] addAnimation:animation forKey:0];
4718 [transition_ transition:0 toView:(flipped_ ? (UIView *) table_ : (UIView *) advanced_)];
4719 flipped_ = !flipped_;
4720 #endif
4721 }
4722
4723 - (void) configurePushed {
4724 [field_ resignFirstResponder];
4725 [self flipPage];
4726 }
4727
4728 - (void) resetViewAnimated:(BOOL)animated {
4729 if (flipped_)
4730 [self flipPage];
4731 [table_ resetViewAnimated:animated];
4732 }
4733
4734 - (void) reloadData {
4735 if (flipped_)
4736 [self flipPage];
4737 [table_ setObject:[field_ text]];
4738 [table_ reloadData];
4739 [table_ resetCursor];
4740 }
4741
4742 - (UIView *) accessoryView {
4743 return accessory_;
4744 }
4745
4746 - (NSString *) title {
4747 return nil;
4748 }
4749
4750 - (NSString *) backButtonTitle {
4751 return @"Search";
4752 }
4753
4754 - (void) setDelegate:(id)delegate {
4755 [table_ setDelegate:delegate];
4756 [super setDelegate:delegate];
4757 }
4758
4759 @end
4760 /* }}} */
4761
4762 @implementation CYBook
4763
4764 - (void) dealloc {
4765 [overlay_ release];
4766 [indicator_ release];
4767 [prompt_ release];
4768 [progress_ release];
4769 [super dealloc];
4770 }
4771
4772 - (NSString *) getTitleForPage:(RVPage *)page {
4773 return Simplify([super getTitleForPage:page]);
4774 }
4775
4776 - (BOOL) updating {
4777 return updating_;
4778 }
4779
4780 - (void) update {
4781 [navbar_ setPrompt:@""];
4782 [navbar_ addSubview:overlay_];
4783 [indicator_ startAnimation];
4784 [prompt_ setText:@"Updating Database..."];
4785 [progress_ setProgress:0];
4786
4787 updating_ = true;
4788
4789 [NSThread
4790 detachNewThreadSelector:@selector(_update)
4791 toTarget:self
4792 withObject:nil
4793 ];
4794 }
4795
4796 - (void) _update_ {
4797 updating_ = false;
4798
4799 [overlay_ removeFromSuperview];
4800 [indicator_ stopAnimation];
4801 [delegate_ reloadData];
4802
4803 [self setPrompt:[NSString stringWithFormat:@"Last Updated: %@", GetLastUpdate()]];
4804 }
4805
4806 - (id) initWithFrame:(CGRect)frame database:(Database *)database {
4807 if ((self = [super initWithFrame:frame]) != nil) {
4808 database_ = database;
4809
4810 if (Advanced_)
4811 [navbar_ setBarStyle:1];
4812
4813 CGRect ovrrect = [navbar_ bounds];
4814 ovrrect.size.height = ([UINavigationBar defaultSizeWithPrompt].height - [UINavigationBar defaultSize].height);
4815
4816 overlay_ = [[UIView alloc] initWithFrame:ovrrect];
4817
4818 UIProgressIndicatorStyle style = Advanced_ ?
4819 kUIProgressIndicatorStyleSmallWhite :
4820 kUIProgressIndicatorStyleSmallBlack;
4821
4822 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:style];
4823 unsigned indoffset = (ovrrect.size.height - indsize.height) / 2;
4824 CGRect indrect = {{indoffset, indoffset}, indsize};
4825
4826 indicator_ = [[UIProgressIndicator alloc] initWithFrame:indrect];
4827 [indicator_ setStyle:style];
4828 [overlay_ addSubview:indicator_];
4829
4830 CGSize prmsize = {200, indsize.width + 4};
4831
4832 CGRect prmrect = {{
4833 indoffset * 2 + indsize.width,
4834 #ifdef __OBJC2__
4835 -1 +
4836 #endif
4837 (ovrrect.size.height - prmsize.height) / 2
4838 }, prmsize};
4839
4840 GSFontRef font = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 12);
4841
4842 prompt_ = [[UITextLabel alloc] initWithFrame:prmrect];
4843
4844 [prompt_ setColor:(Advanced_ ? White_ : Blueish_)];
4845 [prompt_ setBackgroundColor:Clear_];
4846 [prompt_ setFont:font];
4847
4848 CFRelease(font);
4849
4850 [overlay_ addSubview:prompt_];
4851
4852 CGSize prgsize = {75, 100};
4853
4854 CGRect prgrect = {{
4855 ovrrect.size.width - prgsize.width - 10,
4856 (ovrrect.size.height - prgsize.height) / 2
4857 } , prgsize};
4858
4859 progress_ = [[UIProgressBar alloc] initWithFrame:prgrect];
4860 [progress_ setStyle:0];
4861 [overlay_ addSubview:progress_];
4862 } return self;
4863 }
4864
4865 - (void) _update {
4866 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4867
4868 Status status;
4869 status.setDelegate(self);
4870
4871 [database_ updateWithStatus:status];
4872
4873 [self
4874 performSelectorOnMainThread:@selector(_update_)
4875 withObject:nil
4876 waitUntilDone:NO
4877 ];
4878
4879 [pool release];
4880 }
4881
4882 - (void) setProgressError:(NSString *)error forPackage:(NSString *)id {
4883 [prompt_ setText:[NSString stringWithFormat:@"Error: %@", error]];
4884 }
4885
4886 - (void) setProgressTitle:(NSString *)title {
4887 [self
4888 performSelectorOnMainThread:@selector(_setProgressTitle:)
4889 withObject:title
4890 waitUntilDone:YES
4891 ];
4892 }
4893
4894 - (void) setProgressPercent:(float)percent {
4895 }
4896
4897 - (void) addProgressOutput:(NSString *)output {
4898 [self
4899 performSelectorOnMainThread:@selector(_addProgressOutput:)
4900 withObject:output
4901 waitUntilDone:YES
4902 ];
4903 }
4904
4905 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
4906 [sheet dismiss];
4907 }
4908
4909 - (void) _setProgressTitle:(NSString *)title {
4910 [prompt_ setText:[title stringByAppendingString:@"..."]];
4911 }
4912
4913 - (void) _addProgressOutput:(NSString *)output {
4914 }
4915
4916 @end
4917
4918 @interface Cydia : UIApplication <
4919 ConfirmationViewDelegate,
4920 ProgressViewDelegate,
4921 SearchViewDelegate,
4922 CydiaDelegate
4923 > {
4924 UIWindow *window_;
4925
4926 UIView *underlay_;
4927 UIView *overlay_;
4928 CYBook *book_;
4929 UIButtonBar *buttonbar_;
4930
4931 ConfirmationView *confirm_;
4932
4933 NSMutableArray *essential_;
4934 NSMutableArray *broken_;
4935
4936 Database *database_;
4937 ProgressView *progress_;
4938
4939 unsigned tag_;
4940
4941 UIKeyboard *keyboard_;
4942 UIProgressHUD *hud_;
4943
4944 InstallView *install_;
4945 ChangesView *changes_;
4946 ManageView *manage_;
4947 SearchView *search_;
4948 }
4949
4950 @end
4951
4952 @implementation Cydia
4953
4954 - (void) _loaded {
4955 if ([broken_ count] != 0) {
4956 int count = [broken_ count];
4957
4958 UIAlertSheet *sheet = [[[UIAlertSheet alloc]
4959 initWithTitle:[NSString stringWithFormat:@"%d Half-Installed Package%@", count, (count == 1 ? @"" : @"s")]
4960 buttons:[NSArray arrayWithObjects:
4961 @"Forcibly Clear",
4962 @"Ignore (Temporary)",
4963 nil]
4964 defaultButtonIndex:0
4965 delegate:self
4966 context:@"fixhalf"
4967 ] autorelease];
4968
4969 [sheet setBodyText:@"When the shell scripts associated with packages fail, they are left in a bad state known as either half-configured or half-installed. These errors don't go away and instead continue to cause issues. These scripts can be deleted and the packages forcibly removed."];
4970 [sheet popupAlertAnimated:YES];
4971 } else if (!Ignored_ && [essential_ count] != 0) {
4972 int count = [essential_ count];
4973
4974 UIAlertSheet *sheet = [[[UIAlertSheet alloc]
4975 initWithTitle:[NSString stringWithFormat:@"%d Essential Upgrade%@", count, (count == 1 ? @"" : @"s")]
4976 buttons:[NSArray arrayWithObjects:@"Upgrade Essential", @"Ignore (Temporary)", nil]
4977 defaultButtonIndex:0
4978 delegate:self
4979 context:@"upgrade"
4980 ] autorelease];
4981
4982 [sheet setBodyText:@"One or more essential packages are currently out of date. If these upgrades are not performed you are likely to encounter errors."];
4983 [sheet popupAlertAnimated:YES];
4984 }
4985 }
4986
4987 - (void) _reloadData {
4988 /*UIProgressHUD *hud = [[UIProgressHUD alloc] initWithWindow:window_];
4989 [hud setText:@"Reloading Data"];
4990 [overlay_ addSubview:hud];
4991 [hud show:YES];*/
4992
4993 [database_ reloadData];
4994
4995 if (Packages_ == nil) {
4996 Packages_ = [[NSMutableDictionary alloc] initWithCapacity:128];
4997 [Metadata_ setObject:Packages_ forKey:@"Packages"];
4998 }
4999
5000 size_t changes(0);
5001
5002 [essential_ removeAllObjects];
5003 [broken_ removeAllObjects];
5004
5005 NSArray *packages = [database_ packages];
5006 for (int i(0), e([packages count]); i != e; ++i) {
5007 Package *package = [packages objectAtIndex:i];
5008 if ([package half])
5009 [broken_ addObject:package];
5010 if ([package upgradableAndEssential:NO]) {
5011 if ([package essential])
5012 [essential_ addObject:package];
5013 ++changes;
5014 }
5015 }
5016
5017 if (changes != 0) {
5018 NSString *badge([[NSNumber numberWithInt:changes] stringValue]);
5019 [buttonbar_ setBadgeValue:badge forButton:3];
5020 if ([buttonbar_ respondsToSelector:@selector(setBadgeAnimated:forButton:)])
5021 [buttonbar_ setBadgeAnimated:YES forButton:3];
5022 [self setApplicationBadge:badge];
5023 } else {
5024 [buttonbar_ setBadgeValue:nil forButton:3];
5025 if ([buttonbar_ respondsToSelector:@selector(setBadgeAnimated:forButton:)])
5026 [buttonbar_ setBadgeAnimated:NO forButton:3];
5027 [self removeApplicationBadge];
5028 }
5029
5030 if (Changed_) {
5031 _assert([Metadata_ writeToFile:@"/var/lib/cydia/metadata.plist" atomically:YES] == YES);
5032 Changed_ = false;
5033 }
5034
5035 /* XXX: this is just stupid */
5036 if (tag_ != 2)
5037 [install_ reloadData];
5038 if (tag_ != 3)
5039 [changes_ reloadData];
5040 if (tag_ != 4)
5041 [manage_ reloadData];
5042 if (tag_ != 5)
5043 [search_ reloadData];
5044
5045 [book_ reloadData];
5046
5047 if ([packages count] == 0);
5048 else if (Loaded_)
5049 [self _loaded];
5050 else {
5051 Loaded_ = YES;
5052 [book_ update];
5053 }
5054
5055 /*[hud show:NO];
5056 [hud removeFromSuperview];*/
5057 }
5058
5059 - (void) reloadData {
5060 @synchronized (self) {
5061 if (confirm_ == nil)
5062 [self _reloadData];
5063 }
5064 }
5065
5066 - (void) resolve {
5067 pkgProblemResolver *resolver = [database_ resolver];
5068
5069 resolver->InstallProtect();
5070 if (!resolver->Resolve(true))
5071 _error->Discard();
5072 }
5073
5074 - (void) perform {
5075 [database_ prepare];
5076
5077 if ([database_ cache]->BrokenCount() == 0)
5078 confirm_ = [[ConfirmationView alloc] initWithView:underlay_ database:database_ delegate:self];
5079 else {
5080 NSMutableArray *broken = [NSMutableArray arrayWithCapacity:16];
5081 NSArray *packages = [database_ packages];
5082
5083 for (size_t i(0); i != [packages count]; ++i) {
5084 Package *package = [packages objectAtIndex:i];
5085 if ([package broken])
5086 [broken addObject:[package name]];
5087 }
5088
5089 UIAlertSheet *sheet = [[[UIAlertSheet alloc]
5090 initWithTitle:[NSString stringWithFormat:@"%d Broken Packages", [database_ cache]->BrokenCount()]
5091 buttons:[NSArray arrayWithObjects:@"Okay", nil]
5092 defaultButtonIndex:0
5093 delegate:self
5094 context:@"broken"
5095 ] autorelease];
5096
5097 [sheet setBodyText:[NSString stringWithFormat:@"The following packages have unmet dependencies:\n\n%@", [broken componentsJoinedByString:@"\n"]]];
5098 [sheet popupAlertAnimated:YES];
5099
5100 [self _reloadData];
5101 }
5102 }
5103
5104 - (void) installPackage:(Package *)package {
5105 @synchronized (self) {
5106 [package install];
5107 [self resolve];
5108 [self perform];
5109 }
5110 }
5111
5112 - (void) removePackage:(Package *)package {
5113 @synchronized (self) {
5114 [package remove];
5115 [self resolve];
5116 [self perform];
5117 }
5118 }
5119
5120 - (void) distUpgrade {
5121 @synchronized (self) {
5122 [database_ upgrade];
5123 [self perform];
5124 }
5125 }
5126
5127 - (void) cancel {
5128 @synchronized (self) {
5129 [confirm_ release];
5130 confirm_ = nil;
5131 [self _reloadData];
5132 }
5133 }
5134
5135 - (void) confirm {
5136 [overlay_ removeFromSuperview];
5137 reload_ = true;
5138
5139 [progress_
5140 detachNewThreadSelector:@selector(perform)
5141 toTarget:database_
5142 withObject:nil
5143 title:@"Running..."
5144 ];
5145 }
5146
5147 - (void) bootstrap_ {
5148 [database_ update];
5149 [database_ upgrade];
5150 [database_ prepare];
5151 [database_ perform];
5152 }
5153
5154 - (void) bootstrap {
5155 [progress_
5156 detachNewThreadSelector:@selector(bootstrap_)
5157 toTarget:self
5158 withObject:nil
5159 title:@"Bootstrap Install..."
5160 ];
5161 }
5162
5163 - (void) progressViewIsComplete:(ProgressView *)progress {
5164 @synchronized (self) {
5165 [self _reloadData];
5166
5167 if (confirm_ != nil) {
5168 [underlay_ addSubview:overlay_];
5169 [confirm_ removeFromSuperview];
5170 [confirm_ release];
5171 confirm_ = nil;
5172 }
5173 }
5174 }
5175
5176 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
5177 NSString *context = [sheet context];
5178 if ([context isEqualToString:@"fixhalf"])
5179 switch (button) {
5180 case 1:
5181 @synchronized (self) {
5182 for (int i = 0, e = [broken_ count]; i != e; ++i) {
5183 Package *broken = [broken_ objectAtIndex:i];
5184 [broken remove];
5185
5186 NSString *id = [broken id];
5187 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]);
5188 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]);
5189 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]);
5190 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]);
5191 }
5192
5193 [self resolve];
5194 [self perform];
5195 }
5196 break;
5197
5198 case 2:
5199 [broken_ removeAllObjects];
5200 [self _loaded];
5201 break;
5202
5203 default:
5204 _assert(false);
5205 }
5206 else if ([context isEqualToString:@"upgrade"])
5207 switch (button) {
5208 case 1:
5209 @synchronized (self) {
5210 for (int i = 0, e = [essential_ count]; i != e; ++i) {
5211 Package *essential = [essential_ objectAtIndex:i];
5212 [essential install];
5213 }
5214
5215 [self resolve];
5216 [self perform];
5217 }
5218 break;
5219
5220 case 2:
5221 Ignored_ = YES;
5222 break;
5223
5224 default:
5225 _assert(false);
5226 }
5227
5228 [sheet dismiss];
5229 }
5230
5231 - (void) setPage:(RVPage *)page {
5232 [page resetViewAnimated:NO];
5233 [page setDelegate:self];
5234 [book_ setPage:page];
5235 }
5236
5237 - (RVPage *) _setHomePage {
5238 BrowserView *browser = [[[BrowserView alloc] initWithBook:book_ database:database_] autorelease];
5239 [self setPage:browser];
5240 [browser loadURL:[NSURL URLWithString:@"http://cydia.saurik.com/"]];
5241 return browser;
5242 }
5243
5244 - (void) buttonBarItemTapped:(id)sender {
5245 unsigned tag = [sender tag];
5246 if (tag == tag_) {
5247 [book_ resetViewAnimated:YES];
5248 return;
5249 }
5250
5251 switch (tag) {
5252 case 1: [self _setHomePage]; break;
5253
5254 case 2: [self setPage:install_]; break;
5255 case 3: [self setPage:changes_]; break;
5256 case 4: [self setPage:manage_]; break;
5257 case 5: [self setPage:search_]; break;
5258
5259 default: _assert(false);
5260 }
5261
5262 tag_ = tag;
5263 }
5264
5265 - (void) applicationWillSuspend {
5266 [database_ clean];
5267
5268 if (reload_) {
5269 #ifdef __OBJC2__
5270 notify_post("com.apple.mobile.application_installed");
5271 notify_post("com.apple.mobile.application_uninstalled");
5272 #else
5273 pid_t pid = ExecFork();
5274 if (pid == 0) {
5275 #ifndef __OBJC2__
5276 sleep(1);
5277 #endif
5278
5279 if (pid_t child = fork()) {
5280 waitpid(child, NULL, 0);
5281 } else {
5282 execlp("launchctl", "launchctl", "unload", SpringBoard_, NULL);
5283 perror("launchctl unload");
5284 exit(0);
5285 }
5286
5287 execlp("launchctl", "launchctl", "load", SpringBoard_, NULL);
5288 perror("launchctl load");
5289 exit(0);
5290 }
5291 #endif
5292 }
5293
5294 [super applicationWillSuspend];
5295 }
5296
5297 - (void) finish {
5298 if (hud_ != nil) {
5299 [self setStatusBarShowsProgress:NO];
5300
5301 [hud_ show:NO];
5302 [hud_ removeFromSuperview];
5303 [hud_ autorelease];
5304 hud_ = nil;
5305
5306 reload_ = true;
5307 [self suspendWithAnimation:YES];
5308 return;
5309 }
5310
5311 overlay_ = [[UIView alloc] initWithFrame:[underlay_ bounds]];
5312
5313 CGRect screenrect = [UIHardware fullScreenApplicationContentRect];
5314 book_ = [[CYBook alloc] initWithFrame:CGRectMake(
5315 0, 0, screenrect.size.width, screenrect.size.height - 48
5316 ) database:database_];
5317
5318 [book_ setDelegate:self];
5319
5320 [overlay_ addSubview:book_];
5321
5322 NSArray *buttonitems = [NSArray arrayWithObjects:
5323 [NSDictionary dictionaryWithObjectsAndKeys:
5324 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
5325 @"home-up.png", kUIButtonBarButtonInfo,
5326 @"home-dn.png", kUIButtonBarButtonSelectedInfo,
5327 [NSNumber numberWithInt:1], kUIButtonBarButtonTag,
5328 self, kUIButtonBarButtonTarget,
5329 @"Home", kUIButtonBarButtonTitle,
5330 @"0", kUIButtonBarButtonType,
5331 nil],
5332
5333 [NSDictionary dictionaryWithObjectsAndKeys:
5334 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
5335 @"install-up.png", kUIButtonBarButtonInfo,
5336 @"install-dn.png", kUIButtonBarButtonSelectedInfo,
5337 [NSNumber numberWithInt:2], kUIButtonBarButtonTag,
5338 self, kUIButtonBarButtonTarget,
5339 @"Install", kUIButtonBarButtonTitle,
5340 @"0", kUIButtonBarButtonType,
5341 nil],
5342
5343 [NSDictionary dictionaryWithObjectsAndKeys:
5344 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
5345 @"changes-up.png", kUIButtonBarButtonInfo,
5346 @"changes-dn.png", kUIButtonBarButtonSelectedInfo,
5347 [NSNumber numberWithInt:3], kUIButtonBarButtonTag,
5348 self, kUIButtonBarButtonTarget,
5349 @"Changes", kUIButtonBarButtonTitle,
5350 @"0", kUIButtonBarButtonType,
5351 nil],
5352
5353 [NSDictionary dictionaryWithObjectsAndKeys:
5354 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
5355 @"manage-up.png", kUIButtonBarButtonInfo,
5356 @"manage-dn.png", kUIButtonBarButtonSelectedInfo,
5357 [NSNumber numberWithInt:4], kUIButtonBarButtonTag,
5358 self, kUIButtonBarButtonTarget,
5359 @"Manage", kUIButtonBarButtonTitle,
5360 @"0", kUIButtonBarButtonType,
5361 nil],
5362
5363 [NSDictionary dictionaryWithObjectsAndKeys:
5364 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
5365 @"search-up.png", kUIButtonBarButtonInfo,
5366 @"search-dn.png", kUIButtonBarButtonSelectedInfo,
5367 [NSNumber numberWithInt:5], kUIButtonBarButtonTag,
5368 self, kUIButtonBarButtonTarget,
5369 @"Search", kUIButtonBarButtonTitle,
5370 @"0", kUIButtonBarButtonType,
5371 nil],
5372 nil];
5373
5374 buttonbar_ = [[UIButtonBar alloc]
5375 initInView:overlay_
5376 withFrame:CGRectMake(
5377 0, screenrect.size.height - ButtonBarHeight_,
5378 screenrect.size.width, ButtonBarHeight_
5379 )
5380 withItemList:buttonitems
5381 ];
5382
5383 [buttonbar_ setDelegate:self];
5384 [buttonbar_ setBarStyle:1];
5385 [buttonbar_ setButtonBarTrackingMode:2];
5386
5387 int buttons[5] = {1, 2, 3, 4, 5};
5388 [buttonbar_ registerButtonGroup:0 withButtons:buttons withCount:5];
5389 [buttonbar_ showButtonGroup:0 withDuration:0];
5390
5391 for (int i = 0; i != 5; ++i)
5392 [[buttonbar_ viewWithTag:(i + 1)] setFrame:CGRectMake(
5393 i * 64 + 2, 1, 60, ButtonBarHeight_
5394 )];
5395
5396 [buttonbar_ showSelectionForButton:1];
5397 [overlay_ addSubview:buttonbar_];
5398
5399 [UIKeyboard initImplementationNow];
5400 CGSize keysize = [UIKeyboard defaultSize];
5401 CGRect keyrect = {{0, [overlay_ bounds].size.height}, keysize};
5402 keyboard_ = [[UIKeyboard alloc] initWithFrame:keyrect];
5403 [[UIKeyboardImpl sharedInstance] setSoundsEnabled:(Sounds_Keyboard_ ? YES : NO)];
5404 [overlay_ addSubview:keyboard_];
5405
5406 install_ = [[InstallView alloc] initWithBook:book_ database:database_];
5407 changes_ = [[ChangesView alloc] initWithBook:book_ database:database_];
5408 manage_ = [[ManageView alloc] initWithBook:book_ database:database_];
5409 search_ = [[SearchView alloc] initWithBook:book_ database:database_];
5410
5411 if (!bootstrap_)
5412 [underlay_ addSubview:overlay_];
5413
5414 [self reloadData];
5415
5416 if (bootstrap_)
5417 [self bootstrap];
5418 else
5419 [self _setHomePage];
5420 }
5421
5422 - (void) reorganize {
5423 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
5424 system("/usr/libexec/cydia/free.sh");
5425 [self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:NO];
5426 [pool release];
5427 }
5428
5429 - (void) applicationSuspend:(__GSEvent *)event {
5430 if (hud_ == nil && ![progress_ isRunning])
5431 [super applicationSuspend:event];
5432 }
5433
5434 - (void) applicationDidFinishLaunching:(id)unused {
5435 _assert(pkgInitConfig(*_config));
5436 _assert(pkgInitSystem(*_config, _system));
5437
5438 confirm_ = nil;
5439 tag_ = 1;
5440
5441 essential_ = [[NSMutableArray alloc] initWithCapacity:4];
5442 broken_ = [[NSMutableArray alloc] initWithCapacity:4];
5443
5444 CGRect screenrect = [UIHardware fullScreenApplicationContentRect];
5445 window_ = [[UIWindow alloc] initWithContentRect:screenrect];
5446
5447 [window_ orderFront: self];
5448 [window_ makeKey: self];
5449 [window_ _setHidden: NO];
5450
5451 database_ = [[Database alloc] init];
5452 progress_ = [[ProgressView alloc] initWithFrame:[window_ bounds] database:database_ delegate:self];
5453 [database_ setDelegate:progress_];
5454 [window_ setContentView:progress_];
5455
5456 underlay_ = [[UIView alloc] initWithFrame:[progress_ bounds]];
5457 [progress_ setContentView:underlay_];
5458
5459 [progress_ resetView];
5460
5461 if (
5462 readlink("/Applications", NULL, 0) == -1 && errno == EINVAL ||
5463 readlink("/usr/share", NULL, 0) == -1 && errno == EINVAL ||
5464 readlink("/Library/Ringtones", NULL, 0) == -1 && errno == EINVAL ||
5465 readlink("/Library/Wallpapers", NULL, 0) == -1 && errno == EINVAL
5466 ) {
5467 hud_ = [[UIProgressHUD alloc] initWithWindow:window_];
5468 [hud_ setText:@"Reorganizing\nOne Minute!\nPlease Wait...\nDO NOT STOP"];
5469 [hud_ show:YES];
5470 [underlay_ addSubview:hud_];
5471
5472 [self setStatusBarShowsProgress:YES];
5473
5474 [NSThread
5475 detachNewThreadSelector:@selector(reorganize)
5476 toTarget:self
5477 withObject:nil
5478 ];
5479 } else
5480 [self finish];
5481 }
5482
5483 - (void) showKeyboard:(BOOL)show {
5484 CGSize keysize = [UIKeyboard defaultSize];
5485 CGRect keydown = {{0, [overlay_ bounds].size.height}, keysize};
5486 CGRect keyup = keydown;
5487 keyup.origin.y -= keysize.height;
5488
5489 UIFrameAnimation *animation = [[[UIFrameAnimation alloc] initWithTarget:keyboard_] autorelease];
5490 [animation setSignificantRectFields:2];
5491
5492 if (show) {
5493 [animation setStartFrame:keydown];
5494 [animation setEndFrame:keyup];
5495 [keyboard_ activate];
5496 } else {
5497 [animation setStartFrame:keyup];
5498 [animation setEndFrame:keydown];
5499 [keyboard_ deactivate];
5500 }
5501
5502 [[UIAnimator sharedAnimator]
5503 addAnimations:[NSArray arrayWithObjects:animation, nil]
5504 withDuration:KeyboardTime_
5505 start:YES
5506 ];
5507 }
5508
5509 - (void) slideUp:(UIAlertSheet *)alert {
5510 if (Advanced_)
5511 [alert presentSheetFromButtonBar:buttonbar_];
5512 else
5513 [alert presentSheetInView:overlay_];
5514 }
5515
5516 @end
5517
5518 void AddPreferences(NSString *plist) {
5519 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
5520
5521 NSMutableDictionary *settings = [[[NSMutableDictionary alloc] initWithContentsOfFile:plist] autorelease];
5522 _assert(settings != NULL);
5523 NSMutableArray *items = [settings objectForKey:@"items"];
5524
5525 bool cydia(false);
5526
5527 for (size_t i(0); i != [items count]; ++i) {
5528 NSMutableDictionary *item([items objectAtIndex:i]);
5529 NSString *label = [item objectForKey:@"label"];
5530 if (label != nil && [label isEqualToString:@"Cydia"]) {
5531 cydia = true;
5532 break;
5533 }
5534 }
5535
5536 if (!cydia) {
5537 for (size_t i(0); i != [items count]; ++i) {
5538 NSDictionary *item([items objectAtIndex:i]);
5539 NSString *label = [item objectForKey:@"label"];
5540 if (label != nil && [label isEqualToString:@"General"]) {
5541 [items insertObject:[NSDictionary dictionaryWithObjectsAndKeys:
5542 @"CydiaSettings", @"bundle",
5543 @"PSLinkCell", @"cell",
5544 [NSNumber numberWithBool:YES], @"hasIcon",
5545 [NSNumber numberWithBool:YES], @"isController",
5546 @"Cydia", @"label",
5547 nil] atIndex:(i + 1)];
5548
5549 break;
5550 }
5551 }
5552
5553 _assert([settings writeToFile:plist atomically:YES] == YES);
5554 }
5555
5556 [pool release];
5557 }
5558
5559 /*IMP alloc_;
5560 id Alloc_(id self, SEL selector) {
5561 id object = alloc_(self, selector);
5562 fprintf(stderr, "[%s]A-%p\n", self->isa->name, object);
5563 return object;
5564 }*/
5565
5566 /*IMP dealloc_;
5567 id Dealloc_(id self, SEL selector) {
5568 id object = dealloc_(self, selector);
5569 fprintf(stderr, "[%s]D-%p\n", self->isa->name, object);
5570 return object;
5571 }*/
5572
5573 int main(int argc, char *argv[]) {
5574 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
5575
5576 bootstrap_ = argc > 1 && strcmp(argv[1], "--bootstrap") == 0;
5577
5578 Home_ = NSHomeDirectory();
5579
5580 {
5581 NSString *plist = [Home_ stringByAppendingString:@"/Library/Preferences/com.apple.preferences.sounds.plist"];
5582 if (NSDictionary *sounds = [NSDictionary dictionaryWithContentsOfFile:plist])
5583 if (NSNumber *keyboard = [sounds objectForKey:@"keyboard"])
5584 Sounds_Keyboard_ = [keyboard boolValue];
5585 }
5586
5587 setuid(0);
5588 setgid(0);
5589
5590 if (unlink("/var/cache/apt/pkgcache.bin") == -1)
5591 _assert(errno == ENOENT);
5592 if (unlink("/var/cache/apt/srcpkgcache.bin") == -1)
5593 _assert(errno == ENOENT);
5594
5595 /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc));
5596 alloc_ = alloc->method_imp;
5597 alloc->method_imp = (IMP) &Alloc_;*/
5598
5599 /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc));
5600 dealloc_ = dealloc->method_imp;
5601 dealloc->method_imp = (IMP) &Dealloc_;*/
5602
5603 if (NSDictionary *sysver = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]) {
5604 if (NSString *prover = [sysver valueForKey:@"ProductVersion"]) {
5605 Firmware_ = strdup([prover UTF8String]);
5606 NSArray *versions = [prover componentsSeparatedByString:@"."];
5607 int count = [versions count];
5608 Major_ = count > 0 ? [[versions objectAtIndex:0] intValue] : 0;
5609 Minor_ = count > 1 ? [[versions objectAtIndex:1] intValue] : 0;
5610 BugFix_ = count > 2 ? [[versions objectAtIndex:2] intValue] : 0;
5611 }
5612 }
5613
5614 size_t size;
5615 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
5616 char *machine = new char[size];
5617 sysctlbyname("hw.machine", machine, &size, NULL, 0);
5618 Machine_ = machine;
5619
5620 if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice"))
5621 if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) {
5622 if (CFTypeRef serial = IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0)) {
5623 SerialNumber_ = strdup(CFStringGetCStringPtr((CFStringRef) serial, CFStringGetSystemEncoding()));
5624 CFRelease(serial);
5625 }
5626
5627 IOObjectRelease(service);
5628 }
5629
5630 /*AddPreferences(@"/Applications/Preferences.app/Settings-iPhone.plist");
5631 AddPreferences(@"/Applications/Preferences.app/Settings-iPod.plist");*/
5632
5633 if ((Metadata_ = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"]) == NULL)
5634 Metadata_ = [[NSMutableDictionary alloc] initWithCapacity:2];
5635 else
5636 Packages_ = [Metadata_ objectForKey:@"Packages"];
5637
5638 if (access("/User", F_OK) != 0)
5639 system("/usr/libexec/cydia/firmware.sh");
5640
5641 Locale_ = CFLocaleCopyCurrent();
5642 space_ = CGColorSpaceCreateDeviceRGB();
5643
5644 Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0);
5645 Black_.Set(space_, 0.0, 0.0, 0.0, 1.0);
5646 Clear_.Set(space_, 0.0, 0.0, 0.0, 0.0);
5647 Red_.Set(space_, 1.0, 0.0, 0.0, 1.0);
5648 White_.Set(space_, 1.0, 1.0, 1.0, 1.0);
5649
5650 int value = UIApplicationMain(argc, argv, [Cydia class]);
5651
5652 CGColorSpaceRelease(space_);
5653 CFRelease(Locale_);
5654
5655 [pool release];
5656 return value;
5657 }