1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008 Jay Freeman (saurik)
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
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
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.
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.
38 // XXX: wtf/FastMalloc.h... wtf?
39 #define USE_SYSTEM_MALLOC 1
41 /* #include Directives {{{ */
42 #import "UICaboodle.h"
44 #include <objc/message.h>
45 #include <objc/objc.h>
46 #include <objc/runtime.h>
48 #include <CoreGraphics/CoreGraphics.h>
49 #include <GraphicsServices/GraphicsServices.h>
50 #include <Foundation/Foundation.h>
52 #import <QuartzCore/CALayer.h>
54 #import <UIKit/UIKit.h>
57 #import <MessageUI/MailComposeController.h>
63 #include <ext/stdio_filebuf.h>
65 #include <apt-pkg/acquire.h>
66 #include <apt-pkg/acquire-item.h>
67 #include <apt-pkg/algorithms.h>
68 #include <apt-pkg/cachefile.h>
69 #include <apt-pkg/clean.h>
70 #include <apt-pkg/configuration.h>
71 #include <apt-pkg/debmetaindex.h>
72 #include <apt-pkg/error.h>
73 #include <apt-pkg/init.h>
74 #include <apt-pkg/mmap.h>
75 #include <apt-pkg/pkgrecords.h>
76 #include <apt-pkg/sha1.h>
77 #include <apt-pkg/sourcelist.h>
78 #include <apt-pkg/sptr.h>
79 #include <apt-pkg/strutl.h>
81 #include <sys/types.h>
83 #include <sys/sysctl.h>
84 #include <sys/param.h>
85 #include <sys/mount.h>
91 #include <mach-o/nlist.h>
101 #import "BrowserView.h"
102 #import "ResetView.h"
104 #import "substrate.h"
107 //#define _finline __attribute__((force_inline))
108 #define _finline inline
113 #define _limit(count) do { \
114 static size_t _count(0); \
115 if (++_count == count) \
119 #define _timestamp ({ \
121 gettimeofday(&tv, NULL); \
122 tv.tv_sec * 1000000 + tv.tv_usec; \
125 typedef std::vector<class ProfileTime *> TimeList;
135 ProfileTime(const char *name) :
139 times_.push_back(this);
142 void AddTime(uint64_t time) {
149 std::cerr << std::setw(5) << count_ << ", " << std::setw(7) << total_ << " : " << name_ << std::endl;
161 ProfileTimer(ProfileTime &time) :
168 time_.AddTime(_timestamp - start_);
173 for (TimeList::const_iterator i(times_.begin()); i != times_.end(); ++i)
175 std::cerr << "========" << std::endl;
178 #define _profile(name) { \
179 static ProfileTime name(#name); \
180 ProfileTimer _ ## name(name);
184 /* Objective-C Handle<> {{{ */
185 template <typename Type_>
187 typedef _H<Type_> This_;
192 _finline void Retain_() {
197 _finline void Clear_() {
203 _finline _H(Type_ *value = NULL, bool mended = false) :
214 _finline This_ &operator =(Type_ *value) {
215 if (value_ != value) {
224 #define _pooled _H<NSAutoreleasePool> _pool([[NSAutoreleasePool alloc] init], true);
226 void NSLogPoint(const char *fix, const CGPoint &point) {
227 NSLog(@"%s(%g,%g)", fix, point.x, point.y);
230 void NSLogRect(const char *fix, const CGRect &rect) {
231 NSLog(@"%s(%g,%g)+(%g,%g)", fix, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
234 @interface NSObject (Cydia)
235 - (id) yieldToSelector:(SEL)selector withObject:(id)object;
236 - (id) yieldToSelector:(SEL)selector;
239 @implementation NSObject (Cydia)
244 - (void) _yieldToContext:(NSMutableArray *)context { _pooled
245 SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
246 id object([[context objectAtIndex:1] nonretainedObjectValue]);
247 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
249 /* XXX: deal with exceptions */
250 id value([self performSelector:selector withObject:object]);
252 [context removeAllObjects];
254 [context addObject:value];
259 performSelectorOnMainThread:@selector(doNothing)
265 - (id) yieldToSelector:(SEL)selector withObject:(id)object {
266 /*return [self performSelector:selector withObject:object];*/
268 volatile bool stopped(false);
270 NSMutableArray *context([NSMutableArray arrayWithObjects:
271 [NSValue valueWithPointer:selector],
272 [NSValue valueWithNonretainedObject:object],
273 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
276 NSThread *thread([[[NSThread alloc]
278 selector:@selector(_yieldToContext:)
284 NSRunLoop *loop([NSRunLoop currentRunLoop]);
285 NSDate *future([NSDate distantFuture]);
287 while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
289 return [context count] == 0 ? nil : [context objectAtIndex:0];
292 - (id) yieldToSelector:(SEL)selector {
293 return [self yieldToSelector:selector withObject:nil];
298 /* NSForcedOrderingSearch doesn't work on the iPhone */
299 static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch;
300 static const NSStringCompareOptions BaseCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch;
301 static const NSStringCompareOptions ForcedCompareOptions_ = BaseCompareOptions_;
302 static const NSStringCompareOptions LaxCompareOptions_ = BaseCompareOptions_ | NSCaseInsensitiveSearch;
304 /* iPhoneOS 2.0 Compatibility {{{ */
306 @interface UITextView (iPhoneOS)
307 - (void) setTextSize:(float)size;
310 @implementation UITextView (iPhoneOS)
312 - (void) setTextSize:(float)size {
313 [self setFont:[[self font] fontWithSize:size]];
320 extern NSString * const kCAFilterNearest;
322 /* Information Dictionaries {{{ */
323 @interface NSMutableArray (Cydia)
324 - (void) addInfoDictionary:(NSDictionary *)info;
327 @implementation NSMutableArray (Cydia)
329 - (void) addInfoDictionary:(NSDictionary *)info {
330 [self addObject:info];
335 @interface NSMutableDictionary (Cydia)
336 - (void) addInfoDictionary:(NSDictionary *)info;
339 @implementation NSMutableDictionary (Cydia)
341 - (void) addInfoDictionary:(NSDictionary *)info {
342 NSString *bundle = [info objectForKey:@"CFBundleIdentifier"];
343 [self setObject:info forKey:bundle];
348 /* Pop Transitions {{{ */
349 @interface PopTransitionView : UITransitionView {
354 @implementation PopTransitionView
356 - (void) transitionViewDidComplete:(UITransitionView *)view fromView:(UIView *)from toView:(UIView *)to {
357 if (from != nil && to == nil)
358 [self removeFromSuperview];
363 @implementation UIView (PopUpView)
365 - (void) popFromSuperviewAnimated:(BOOL)animated {
366 [[self superview] transition:(animated ? UITransitionPushFromTop : UITransitionNone) toView:nil];
369 - (void) popSubview:(UIView *)view {
370 UITransitionView *transition([[[PopTransitionView alloc] initWithFrame:[self bounds]] autorelease]);
371 [transition setDelegate:transition];
372 [self addSubview:transition];
374 UIView *blank = [[[UIView alloc] initWithFrame:[transition bounds]] autorelease];
375 [transition transition:UITransitionNone toView:blank];
376 [transition transition:UITransitionPushFromBottom toView:view];
382 #define lprintf(args...) fprintf(stderr, args)
385 #define ForSaurik (1 && !ForRelease)
386 #define LogBrowser (0 && !ForRelease)
387 #define ManualRefresh (1 && !ForRelease)
388 #define ShowInternals (1 && !ForRelease)
389 #define IgnoreInstall (0 && !ForRelease)
390 #define RecycleWebViews 0
391 #define AlwaysReload (0 && !ForRelease)
395 #define _trace(args...)
397 #define _profile(name) {
400 #define PrintTimes() do {} while (false)
404 @interface NSMutableArray (Radix)
405 - (void) radixSortUsingSelector:(SEL)selector withObject:(id)object;
408 @implementation NSMutableArray (Radix)
410 - (void) radixSortUsingSelector:(SEL)selector withObject:(id)object {
411 NSInvocation *invocation([NSInvocation invocationWithMethodSignature:[NSMethodSignature signatureWithObjCTypes:"L12@0:4@8"]]);
412 [invocation setSelector:selector];
413 [invocation setArgument:&object atIndex:2];
415 size_t count([self count]);
420 } *swap(new RadixItem[count * 2]), *lhs(swap), *rhs(swap + count);
422 for (size_t i(0); i != count; ++i) {
423 RadixItem &item(lhs[i]);
426 id object([self objectAtIndex:i]);
427 [invocation setTarget:object];
430 [invocation getReturnValue:&item.key];
433 static const size_t width = 32;
434 static const size_t bits = 11;
435 static const size_t slots = 1 << bits;
436 static const size_t passes = (width + (bits - 1)) / bits;
438 size_t *hist(new size_t[slots]);
440 for (size_t pass(0); pass != passes; ++pass) {
441 memset(hist, 0, sizeof(size_t) * slots);
443 for (size_t i(0); i != count; ++i) {
444 uint32_t key(lhs[i].key);
446 key &= _not(uint32_t) >> width - bits;
451 for (size_t i(0); i != slots; ++i) {
452 size_t local(offset);
457 for (size_t i(0); i != count; ++i) {
458 uint32_t key(lhs[i].key);
460 key &= _not(uint32_t) >> width - bits;
461 rhs[hist[key]++] = lhs[i];
471 NSMutableArray *values([NSMutableArray arrayWithCapacity:count]);
472 for (size_t i(0); i != count; ++i)
473 [values addObject:[self objectAtIndex:lhs[i].index]];
474 [self setArray:values];
482 /* Apple Bug Fixes {{{ */
483 @implementation UIWebDocumentView (Cydia)
485 - (void) _setScrollerOffset:(CGPoint)offset {
486 UIScroller *scroller([self _scroller]);
488 CGSize size([scroller contentSize]);
489 CGSize bounds([scroller bounds].size);
492 max.x = size.width - bounds.width;
493 max.y = size.height - bounds.height;
501 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
502 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
504 [scroller setOffset:offset];
511 kUIControlEventMouseDown = 1 << 0,
512 kUIControlEventMouseMovedInside = 1 << 2, // mouse moved inside control target
513 kUIControlEventMouseMovedOutside = 1 << 3, // mouse moved outside control target
514 kUIControlEventMouseUpInside = 1 << 6, // mouse up inside control target
515 kUIControlEventMouseUpOutside = 1 << 7, // mouse up outside control target
516 kUIControlAllEvents = (kUIControlEventMouseDown | kUIControlEventMouseMovedInside | kUIControlEventMouseMovedOutside | kUIControlEventMouseUpInside | kUIControlEventMouseUpOutside)
517 } UIControlEventMasks;
519 NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) {
520 size_t length([self length] - state->state);
523 else if (length > count)
525 for (size_t i(0); i != length; ++i)
526 objects[i] = [self item:state->state++];
527 state->itemsPtr = objects;
528 state->mutationsPtr = (unsigned long *) self;
532 @interface NSString (UIKit)
533 - (NSString *) stringByAddingPercentEscapes;
534 - (NSString *) stringByReplacingCharacter:(unsigned short)arg0 withCharacter:(unsigned short)arg1;
537 @interface NSString (Cydia)
538 + (NSString *) stringWithUTF8BytesNoCopy:(const char *)bytes length:(int)length;
539 + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length;
540 - (NSComparisonResult) compareByPath:(NSString *)other;
541 - (NSString *) stringByCachingURLWithCurrentCDN;
542 - (NSString *) stringByAddingPercentEscapesIncludingReserved;
545 @implementation NSString (Cydia)
547 + (NSString *) stringWithUTF8BytesNoCopy:(const char *)bytes length:(int)length {
548 return [[[NSString alloc] initWithBytesNoCopy:const_cast<char *>(bytes) length:length encoding:NSUTF8StringEncoding freeWhenDone:NO] autorelease];
551 + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length {
552 return [[[NSString alloc] initWithBytes:bytes length:length encoding:NSUTF8StringEncoding] autorelease];
555 - (NSComparisonResult) compareByPath:(NSString *)other {
556 NSString *prefix = [self commonPrefixWithString:other options:0];
557 size_t length = [prefix length];
559 NSRange lrange = NSMakeRange(length, [self length] - length);
560 NSRange rrange = NSMakeRange(length, [other length] - length);
562 lrange = [self rangeOfString:@"/" options:0 range:lrange];
563 rrange = [other rangeOfString:@"/" options:0 range:rrange];
565 NSComparisonResult value;
567 if (lrange.location == NSNotFound && rrange.location == NSNotFound)
568 value = NSOrderedSame;
569 else if (lrange.location == NSNotFound)
570 value = NSOrderedAscending;
571 else if (rrange.location == NSNotFound)
572 value = NSOrderedDescending;
574 value = NSOrderedSame;
576 NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] :
577 [self substringWithRange:NSMakeRange(length, lrange.location - length)];
578 NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] :
579 [other substringWithRange:NSMakeRange(length, rrange.location - length)];
581 NSComparisonResult result = [lpath compare:rpath];
582 return result == NSOrderedSame ? value : result;
585 - (NSString *) stringByCachingURLWithCurrentCDN {
587 stringByReplacingOccurrencesOfString:@"://"
588 withString:@"://ne.edgecastcdn.net/8003A4/"
590 /* XXX: this is somewhat inaccurate */
591 range:NSMakeRange(0, 10)
595 - (NSString *) stringByAddingPercentEscapesIncludingReserved {
596 return [(id)CFURLCreateStringByAddingPercentEscapes(
601 kCFStringEncodingUTF8
607 /* Perl-Compatible RegEx {{{ */
617 Pcre(const char *regex) :
622 code_ = pcre_compile(regex, 0, &error, &offset, NULL);
625 lprintf("%d:%s\n", offset, error);
629 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
630 matches_ = new int[(capture_ + 1) * 3];
638 NSString *operator [](size_t match) {
639 return [NSString stringWithUTF8Bytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2])];
642 bool operator ()(NSString *data) {
643 // XXX: length is for characters, not for bytes
644 return operator ()([data UTF8String], [data length]);
647 bool operator ()(const char *data, size_t size) {
649 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
653 /* Mime Addresses {{{ */
654 @interface Address : NSObject {
660 - (NSString *) address;
662 + (Address *) addressWithString:(NSString *)string;
663 - (Address *) initWithString:(NSString *)string;
666 @implementation Address
675 - (NSString *) name {
679 - (NSString *) address {
683 + (Address *) addressWithString:(NSString *)string {
684 return [[[Address alloc] initWithString:string] autorelease];
687 + (NSArray *) _attributeKeys {
688 return [NSArray arrayWithObjects:@"address", @"name", nil];
691 - (NSArray *) attributeKeys {
692 return [[self class] _attributeKeys];
695 + (BOOL) isKeyExcludedFromWebScript:(const char *)name {
696 return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
699 - (Address *) initWithString:(NSString *)string {
700 if ((self = [super init]) != nil) {
701 const char *data = [string UTF8String];
702 size_t size = [string length];
704 static Pcre address_r("^\"?(.*)\"? <([^>]*)>$");
706 if (address_r(data, size)) {
707 name_ = [address_r[1] retain];
708 address_ = [address_r[2] retain];
710 name_ = [string retain];
718 /* CoreGraphics Primitives {{{ */
729 CGColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) :
732 Set(space, red, green, blue, alpha);
737 CGColorRelease(color_);
744 void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) {
746 float color[] = {red, green, blue, alpha};
747 color_ = CGColorCreate(space, color);
750 operator CGColorRef() {
756 extern "C" void UISetColor(CGColorRef color);
758 /* Random Global Variables {{{ */
759 static const int PulseInterval_ = 50000;
760 static const int ButtonBarHeight_ = 48;
761 static const float KeyboardTime_ = 0.3f;
763 #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist"
764 #define SandboxTemplate_ "/usr/share/sandbox/SandboxTemplate.sb"
765 #define NotifyConfig_ "/etc/notify.conf"
767 static CGColor Blue_;
768 static CGColor Blueish_;
769 static CGColor Black_;
771 static CGColor White_;
772 static CGColor Gray_;
773 static CGColor Green_;
774 static CGColor Purple_;
775 static CGColor Purplish_;
777 static UIColor *CommercialColor_;
779 static NSString *App_;
780 static NSString *Home_;
781 static BOOL Sounds_Keyboard_;
783 static BOOL Advanced_;
785 static BOOL Ignored_;
787 static UIFont *Font12_;
788 static UIFont *Font12Bold_;
789 static UIFont *Font14_;
790 static UIFont *Font18Bold_;
791 static UIFont *Font22Bold_;
793 static const char *Machine_ = NULL;
794 static const NSString *UniqueID_ = nil;
795 static const NSString *Build_ = nil;
796 static const NSString *Product_ = nil;
797 static const NSString *Safari_ = nil;
800 CGColorSpaceRef space_;
805 static NSDictionary *SectionMap_;
806 static NSMutableDictionary *Metadata_;
807 static _transient NSMutableDictionary *Settings_;
808 static _transient NSString *Role_;
809 static _transient NSMutableDictionary *Packages_;
810 static _transient NSMutableDictionary *Sections_;
811 static _transient NSMutableDictionary *Sources_;
812 static bool Changed_;
816 static NSMutableArray *Documents_;
819 NSString *GetLastUpdate() {
820 NSDate *update = [Metadata_ objectForKey:@"LastUpdate"];
823 return @"Never or Unknown";
825 CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle);
826 CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update);
828 CFRelease(formatter);
830 return [(NSString *) formatted autorelease];
833 /* Display Helpers {{{ */
834 inline float Interpolate(float begin, float end, float fraction) {
835 return (end - begin) * fraction + begin;
838 NSString *SizeString(double size) {
839 bool negative = size < 0;
844 while (size > 1024) {
849 static const char *powers_[] = {"B", "kB", "MB", "GB"};
851 return [NSString stringWithFormat:@"%s%.1f %s", (negative ? "-" : ""), size, powers_[power]];
854 NSString *StripVersion(NSString *version) {
855 NSRange colon = [version rangeOfString:@":"];
856 if (colon.location != NSNotFound)
857 version = [version substringFromIndex:(colon.location + 1)];
861 NSString *Simplify(NSString *title) {
862 const char *data = [title UTF8String];
863 size_t size = [title length];
865 static Pcre square_r("^\\[(.*)\\]$");
866 if (square_r(data, size))
867 return Simplify(square_r[1]);
869 static Pcre paren_r("^\\((.*)\\)$");
870 if (paren_r(data, size))
871 return Simplify(paren_r[1]);
873 static Pcre title_r("^(.*?) \\(.*\\)$");
874 if (title_r(data, size))
875 return Simplify(title_r[1]);
881 bool isSectionVisible(NSString *section) {
882 NSDictionary *metadata = [Sections_ objectForKey:section];
883 NSNumber *hidden = metadata == nil ? nil : [metadata objectForKey:@"Hidden"];
884 return hidden == nil || ![hidden boolValue];
887 /* Delegate Prototypes {{{ */
891 @interface NSObject (ProgressDelegate)
894 @implementation NSObject(ProgressDelegate)
896 - (void) _setProgressError:(NSArray *)args {
897 [self performSelector:@selector(setProgressError:forPackage:)
898 withObject:[args objectAtIndex:0]
899 withObject:([args count] == 1 ? nil : [args objectAtIndex:1])
905 @protocol ProgressDelegate
906 - (void) setProgressError:(NSString *)error forPackage:(NSString *)id;
907 - (void) setProgressTitle:(NSString *)title;
908 - (void) setProgressPercent:(float)percent;
909 - (void) startProgress;
910 - (void) addProgressOutput:(NSString *)output;
911 - (bool) isCancelling:(size_t)received;
914 @protocol ConfigurationDelegate
915 - (void) repairWithSelector:(SEL)selector;
916 - (void) setConfigurationData:(NSString *)data;
919 @protocol CydiaDelegate
920 - (void) installPackage:(Package *)package;
921 - (void) removePackage:(Package *)package;
922 - (void) slideUp:(UIActionSheet *)alert;
923 - (void) distUpgrade;
926 - (void) askForSettings;
927 - (UIProgressHUD *) addProgressHUD;
928 - (void) removeProgressHUD:(UIProgressHUD *)hud;
929 - (RVPage *) pageForURL:(NSURL *)url hasTag:(int *)tag;
930 - (RVPage *) pageForPackage:(NSString *)name;
931 - (void) openMailToURL:(NSURL *)url;
932 - (void) clearFirstResponder;
936 /* Status Delegation {{{ */
938 public pkgAcquireStatus
941 _transient NSObject<ProgressDelegate> *delegate_;
949 void setDelegate(id delegate) {
950 delegate_ = delegate;
953 virtual bool MediaChange(std::string media, std::string drive) {
957 virtual void IMSHit(pkgAcquire::ItemDesc &item) {
960 virtual void Fetch(pkgAcquire::ItemDesc &item) {
961 //NSString *name([NSString stringWithUTF8String:item.ShortDesc.c_str()]);
962 [delegate_ setProgressTitle:[NSString stringWithUTF8String:("Downloading " + item.ShortDesc).c_str()]];
965 virtual void Done(pkgAcquire::ItemDesc &item) {
968 virtual void Fail(pkgAcquire::ItemDesc &item) {
970 item.Owner->Status == pkgAcquire::Item::StatIdle ||
971 item.Owner->Status == pkgAcquire::Item::StatDone
975 std::string &error(item.Owner->ErrorText);
979 NSString *description([NSString stringWithUTF8String:item.Description.c_str()]);
980 NSArray *fields([description componentsSeparatedByString:@" "]);
981 NSString *source([fields count] == 0 ? nil : [fields objectAtIndex:0]);
983 [delegate_ performSelectorOnMainThread:@selector(_setProgressError:)
984 withObject:[NSArray arrayWithObjects:
985 [NSString stringWithUTF8String:error.c_str()],
992 virtual bool Pulse(pkgAcquire *Owner) {
993 bool value = pkgAcquireStatus::Pulse(Owner);
996 double(CurrentBytes + CurrentItems) /
997 double(TotalBytes + TotalItems)
1000 [delegate_ setProgressPercent:percent];
1001 return [delegate_ isCancelling:CurrentBytes] ? false : value;
1004 virtual void Start() {
1005 [delegate_ startProgress];
1008 virtual void Stop() {
1012 /* Progress Delegation {{{ */
1017 _transient id<ProgressDelegate> delegate_;
1020 virtual void Update() {
1021 /*[delegate_ setProgressTitle:[NSString stringWithUTF8String:Op.c_str()]];
1022 [delegate_ setProgressPercent:(Percent / 100)];*/
1031 void setDelegate(id delegate) {
1032 delegate_ = delegate;
1035 virtual void Done() {
1036 //[delegate_ setProgressPercent:1];
1041 /* Database Interface {{{ */
1042 @interface Database : NSObject {
1045 pkgCacheFile cache_;
1046 pkgDepCache::Policy *policy_;
1047 pkgRecords *records_;
1048 pkgProblemResolver *resolver_;
1049 pkgAcquire *fetcher_;
1051 SPtr<pkgPackageManager> manager_;
1052 pkgSourceList *list_;
1054 NSMutableDictionary *sources_;
1055 NSMutableArray *packages_;
1057 _transient NSObject<ConfigurationDelegate, ProgressDelegate> *delegate_;
1066 + (Database *) sharedInstance;
1069 - (void) _readCydia:(NSNumber *)fd;
1070 - (void) _readStatus:(NSNumber *)fd;
1071 - (void) _readOutput:(NSNumber *)fd;
1075 - (Package *) packageWithName:(NSString *)name;
1077 - (pkgCacheFile &) cache;
1078 - (pkgDepCache::Policy *) policy;
1079 - (pkgRecords *) records;
1080 - (pkgProblemResolver *) resolver;
1081 - (pkgAcquire &) fetcher;
1082 - (pkgSourceList &) list;
1083 - (NSArray *) packages;
1084 - (NSArray *) sources;
1085 - (void) reloadData;
1093 - (void) updateWithStatus:(Status &)status;
1095 - (void) setDelegate:(id)delegate;
1096 - (Source *) getSource:(const pkgCache::PkgFileIterator &)file;
1100 /* Source Class {{{ */
1101 @interface Source : NSObject {
1102 NSString *description_;
1107 NSString *distribution_;
1111 NSString *defaultIcon_;
1113 NSDictionary *record_;
1117 - (Source *) initWithMetaIndex:(metaIndex *)index;
1119 - (NSComparisonResult) compareByNameAndType:(Source *)source;
1121 - (NSDictionary *) record;
1125 - (NSString *) distribution;
1126 - (NSString *) type;
1128 - (NSString *) host;
1130 - (NSString *) name;
1131 - (NSString *) description;
1132 - (NSString *) label;
1133 - (NSString *) origin;
1134 - (NSString *) version;
1136 - (NSString *) defaultIcon;
1140 @implementation Source
1142 #define _clear(field) \
1149 _clear(distribution_)
1152 _clear(description_)
1156 _clear(defaultIcon_)
1165 + (NSArray *) _attributeKeys {
1166 return [NSArray arrayWithObjects:@"description", @"distribution", @"host", @"key", @"label", @"name", @"origin", @"trusted", @"type", @"uri", @"version", nil];
1169 - (NSArray *) attributeKeys {
1170 return [[self class] _attributeKeys];
1173 + (BOOL) isKeyExcludedFromWebScript:(const char *)name {
1174 return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
1177 - (void) setMetaIndex:(metaIndex *)index {
1180 trusted_ = index->IsTrusted();
1182 uri_ = [[NSString stringWithUTF8String:index->GetURI().c_str()] retain];
1183 distribution_ = [[NSString stringWithUTF8String:index->GetDist().c_str()] retain];
1184 type_ = [[NSString stringWithUTF8String:index->GetType()] retain];
1186 debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index));
1187 if (dindex != NULL) {
1188 std::ifstream release(dindex->MetaIndexFile("Release").c_str());
1190 while (std::getline(release, line)) {
1191 std::string::size_type colon(line.find(':'));
1192 if (colon == std::string::npos)
1195 std::string name(line.substr(0, colon));
1196 std::string value(line.substr(colon + 1));
1197 while (!value.empty() && value[0] == ' ')
1198 value = value.substr(1);
1200 if (name == "Default-Icon")
1201 defaultIcon_ = [[NSString stringWithUTF8String:value.c_str()] retain];
1202 else if (name == "Description")
1203 description_ = [[NSString stringWithUTF8String:value.c_str()] retain];
1204 else if (name == "Label")
1205 label_ = [[NSString stringWithUTF8String:value.c_str()] retain];
1206 else if (name == "Origin")
1207 origin_ = [[NSString stringWithUTF8String:value.c_str()] retain];
1208 else if (name == "Version")
1209 version_ = [[NSString stringWithUTF8String:value.c_str()] retain];
1213 record_ = [Sources_ objectForKey:[self key]];
1215 record_ = [record_ retain];
1218 - (Source *) initWithMetaIndex:(metaIndex *)index {
1219 if ((self = [super init]) != nil) {
1220 [self setMetaIndex:index];
1224 - (NSComparisonResult) compareByNameAndType:(Source *)source {
1225 NSDictionary *lhr = [self record];
1226 NSDictionary *rhr = [source record];
1229 return lhr == nil ? NSOrderedDescending : NSOrderedAscending;
1231 NSString *lhs = [self name];
1232 NSString *rhs = [source name];
1234 if ([lhs length] != 0 && [rhs length] != 0) {
1235 unichar lhc = [lhs characterAtIndex:0];
1236 unichar rhc = [rhs characterAtIndex:0];
1238 if (isalpha(lhc) && !isalpha(rhc))
1239 return NSOrderedAscending;
1240 else if (!isalpha(lhc) && isalpha(rhc))
1241 return NSOrderedDescending;
1244 return [lhs compare:rhs options:LaxCompareOptions_];
1247 - (NSDictionary *) record {
1255 - (NSString *) uri {
1259 - (NSString *) distribution {
1260 return distribution_;
1263 - (NSString *) type {
1267 - (NSString *) key {
1268 return [NSString stringWithFormat:@"%@:%@:%@", type_, uri_, distribution_];
1271 - (NSString *) host {
1272 return [[[NSURL URLWithString:[self uri]] host] lowercaseString];
1275 - (NSString *) name {
1276 return origin_ == nil ? [self host] : origin_;
1279 - (NSString *) description {
1280 return description_;
1283 - (NSString *) label {
1284 return label_ == nil ? [self host] : label_;
1287 - (NSString *) origin {
1291 - (NSString *) version {
1295 - (NSString *) defaultIcon {
1296 return defaultIcon_;
1301 /* Relationship Class {{{ */
1302 @interface Relationship : NSObject {
1307 - (NSString *) type;
1309 - (NSString *) name;
1313 @implementation Relationship
1321 - (NSString *) type {
1329 - (NSString *) name {
1336 /* Package Class {{{ */
1337 @interface Package : NSObject {
1340 pkgCache::PkgIterator iterator_;
1341 _transient Database *database_;
1342 pkgCache::VerIterator version_;
1343 pkgCache::VerFileIterator file_;
1352 NSString *installed_;
1358 NSString *depiction_;
1359 NSString *homepage_;
1365 NSArray *relationships_;
1368 - (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database;
1369 + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database;
1371 - (pkgCache::PkgIterator) iterator;
1373 - (NSString *) section;
1374 - (NSString *) simpleSection;
1378 - (Address *) maintainer;
1380 - (NSString *) description;
1383 - (NSMutableDictionary *) metadata;
1385 - (BOOL) subscribed;
1388 - (NSString *) latest;
1389 - (NSString *) installed;
1392 - (BOOL) upgradableAndEssential:(BOOL)essential;
1395 - (BOOL) unfiltered;
1399 - (BOOL) halfConfigured;
1400 - (BOOL) halfInstalled;
1402 - (NSString *) mode;
1405 - (NSString *) name;
1406 - (NSString *) tagline;
1408 - (NSString *) homepage;
1409 - (NSString *) depiction;
1410 - (Address *) author;
1412 - (NSArray *) files;
1413 - (NSArray *) relationships;
1414 - (NSArray *) warnings;
1415 - (NSArray *) applications;
1417 - (Source *) source;
1418 - (NSString *) role;
1420 - (BOOL) matches:(NSString *)text;
1422 - (bool) hasSupportingRole;
1423 - (BOOL) hasTag:(NSString *)tag;
1424 - (NSString *) primaryPurpose;
1425 - (NSArray *) purposes;
1426 - (bool) isCommercial;
1428 - (NSComparisonResult) compareByName:(Package *)package;
1429 - (NSComparisonResult) compareBySection:(Package *)package;
1431 - (uint32_t) compareForChanges;
1436 - (bool) isUnfilteredAndSearchedForBy:(NSString *)search;
1437 - (bool) isInstalledAndVisible:(NSNumber *)number;
1438 - (bool) isVisiblyUninstalledInSection:(NSString *)section;
1439 - (bool) isVisibleInSource:(Source *)source;
1443 @implementation Package
1448 if (section_ != nil)
1452 if (installed_ != nil)
1453 [installed_ release];
1461 if (depiction_ != nil)
1462 [depiction_ release];
1463 if (homepage_ != nil)
1464 [homepage_ release];
1465 if (sponsor_ != nil)
1474 if (relationships_ != nil)
1475 [relationships_ release];
1480 + (NSString *) webScriptNameForSelector:(SEL)selector {
1481 if (selector == @selector(hasTag:))
1487 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
1488 return [self webScriptNameForSelector:selector] == nil;
1491 + (NSArray *) _attributeKeys {
1492 return [NSArray arrayWithObjects:@"applications", @"author", @"depiction", @"description", @"essential", @"homepage", @"icon", @"id", @"installed", @"latest", @"maintainer", @"name", @"purposes", @"section", @"size", @"source", @"sponsor", @"tagline", @"warnings", nil];
1495 - (NSArray *) attributeKeys {
1496 return [[self class] _attributeKeys];
1499 + (BOOL) isKeyExcludedFromWebScript:(const char *)name {
1500 return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
1503 - (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database {
1504 if ((self = [super init]) != nil) {
1505 _profile(Package$initWithIterator)
1506 @synchronized (database) {
1507 era_ = [database era];
1509 iterator_ = iterator;
1510 database_ = database;
1512 _profile(Package$initWithIterator$Control)
1515 _profile(Package$initWithIterator$Version)
1516 version_ = [database_ policy]->GetCandidateVer(iterator_);
1519 NSString *latest = version_.end() ? nil : [NSString stringWithUTF8String:version_.VerStr()];
1521 _profile(Package$initWithIterator$Latest)
1522 latest_ = latest == nil ? nil : [StripVersion(latest) retain];
1525 pkgCache::VerIterator current;
1526 NSString *installed;
1528 _profile(Package$initWithIterator$Current)
1529 current = iterator_.CurrentVer();
1530 installed = current.end() ? nil : [NSString stringWithUTF8String:current.VerStr()];
1533 _profile(Package$initWithIterator$Installed)
1534 installed_ = [StripVersion(installed) retain];
1537 _profile(Package$initWithIterator$File)
1538 if (!version_.end())
1539 file_ = version_.FileList();
1541 pkgCache &cache([database_ cache]);
1542 file_ = pkgCache::VerFileIterator(cache, cache.VerFileP);
1546 _profile(Package$initWithIterator$Name)
1547 id_ = [[NSString stringWithUTF8String:iterator_.Name()] retain];
1551 _profile(Package$initWithIterator$Parse)
1552 pkgRecords::Parser *parser;
1554 _profile(Package$initWithIterator$Parse$Lookup)
1555 parser = &[database_ records]->Lookup(file_);
1558 const char *begin, *end;
1559 parser->GetRec(begin, end);
1561 NSString *website(nil);
1562 NSString *sponsor(nil);
1563 NSString *author(nil);
1572 {"depiction", &depiction_},
1573 {"homepage", &homepage_},
1574 {"website", &website},
1575 {"sponsor", &sponsor},
1576 {"author", &author},
1580 while (begin != end)
1581 if (*begin == '\n') {
1584 } else if (isblank(*begin)) next: {
1585 begin = static_cast<char *>(memchr(begin + 1, '\n', end - begin - 1));
1588 } else if (const char *colon = static_cast<char *>(memchr(begin, ':', end - begin))) {
1589 const char *name(begin);
1590 size_t size(colon - begin);
1592 begin = static_cast<char *>(memchr(begin, '\n', end - begin));
1595 const char *stop(begin == NULL ? end : begin);
1596 while (stop[-1] == '\r')
1598 while (++colon != stop && isblank(*colon));
1600 for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i)
1601 if (strncasecmp(names[i].name_, name, size) == 0) {
1604 _profile(Package$initWithIterator$Parse$Value)
1605 value = [NSString stringWithUTF8Bytes:colon length:(stop - colon)];
1608 *names[i].value_ = value;
1618 _profile(Package$initWithIterator$Parse$Retain)
1620 name_ = [name_ retain];
1621 _profile(Package$initWithIterator$Parse$Tagline)
1622 tagline_ = [[NSString stringWithUTF8String:parser->ShortDesc().c_str()] retain];
1625 icon_ = [icon_ retain];
1626 if (depiction_ != nil)
1627 depiction_ = [depiction_ retain];
1628 if (homepage_ == nil)
1629 homepage_ = website;
1630 if ([homepage_ isEqualToString:depiction_])
1632 if (homepage_ != nil)
1633 homepage_ = [homepage_ retain];
1635 sponsor_ = [[Address addressWithString:sponsor] retain];
1637 author_ = [[Address addressWithString:author] retain];
1639 tags_ = [[tag componentsSeparatedByString:@", "] retain];
1643 _profile(Package$initWithIterator$Tags)
1645 for (NSString *tag in tags_)
1646 if ([tag hasPrefix:@"role::"]) {
1647 role_ = [[tag substringFromIndex:6] retain];
1652 NSString *solid(latest == nil ? installed : latest);
1653 bool changed(false);
1655 NSString *key([id_ lowercaseString]);
1657 _profile(Package$initWithIterator$Metadata)
1658 NSMutableDictionary *metadata = [Packages_ objectForKey:key];
1659 if (metadata == nil) {
1660 metadata = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
1665 [metadata setObject:solid forKey:@"LastVersion"];
1668 NSDate *first([metadata objectForKey:@"FirstSeen"]);
1669 NSDate *last([metadata objectForKey:@"LastSeen"]);
1670 NSString *version([metadata objectForKey:@"LastVersion"]);
1673 first = last == nil ? now_ : last;
1674 [metadata setObject:first forKey:@"FirstSeen"];
1679 if (version == nil) {
1680 [metadata setObject:solid forKey:@"LastVersion"];
1682 } else if (![version isEqualToString:solid]) {
1683 [metadata setObject:solid forKey:@"LastVersion"];
1685 [metadata setObject:last forKey:@"LastSeen"];
1691 [Packages_ setObject:metadata forKey:key];
1696 const char *section(iterator_.Section());
1697 if (section == NULL)
1700 NSString *name([[NSString stringWithUTF8String:section] stringByReplacingCharacter:' ' withCharacter:'_']);
1703 if (NSDictionary *value = [SectionMap_ objectForKey:name])
1704 if (NSString *rename = [value objectForKey:@"Rename"]) {
1709 section_ = [[name stringByReplacingCharacter:'_' withCharacter:' '] retain];
1712 essential_ = (iterator_->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES;
1713 } _end } return self;
1716 + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database {
1717 return [[[Package alloc]
1718 initWithIterator:iterator
1723 - (pkgCache::PkgIterator) iterator {
1727 - (NSString *) section {
1731 - (NSString *) simpleSection {
1732 if (NSString *section = [self section])
1733 return Simplify(section);
1738 - (NSString *) uri {
1741 pkgIndexFile *index;
1742 pkgCache::PkgFileIterator file(file_.File());
1743 if (![database_ list].FindIndex(file, index))
1745 return [NSString stringWithUTF8String:iterator_->Path];
1746 //return [NSString stringWithUTF8String:file.Site()];
1747 //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()];
1751 - (Address *) maintainer {
1754 pkgRecords::Parser *parser = &[database_ records]->Lookup(file_);
1755 const std::string &maintainer(parser->Maintainer());
1756 return maintainer.empty() ? nil : [Address addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]];
1760 return version_.end() ? 0 : version_->InstalledSize;
1763 - (NSString *) description {
1766 pkgRecords::Parser *parser = &[database_ records]->Lookup(file_);
1767 NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]);
1769 NSArray *lines = [description componentsSeparatedByString:@"\n"];
1770 NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)];
1771 if ([lines count] < 2)
1774 NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
1775 for (size_t i(1), e([lines count]); i != e; ++i) {
1776 NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace];
1777 [trimmed addObject:trim];
1780 return [trimmed componentsJoinedByString:@"\n"];
1784 _profile(Package$index)
1785 NSString *name([self name]);
1786 if ([name length] == 0)
1788 unichar character([name characterAtIndex:0]);
1789 if (!isalpha(character))
1791 return toupper(character);
1795 - (NSMutableDictionary *) metadata {
1796 return [Packages_ objectForKey:[id_ lowercaseString]];
1800 NSDictionary *metadata([self metadata]);
1801 if ([self subscribed])
1802 if (NSDate *last = [metadata objectForKey:@"LastSeen"])
1804 return [metadata objectForKey:@"FirstSeen"];
1807 - (BOOL) subscribed {
1808 NSDictionary *metadata([self metadata]);
1809 if (NSNumber *subscribed = [metadata objectForKey:@"IsSubscribed"])
1810 return [subscribed boolValue];
1816 NSDictionary *metadata([self metadata]);
1817 if (NSNumber *ignored = [metadata objectForKey:@"IsIgnored"])
1818 return [ignored boolValue];
1823 - (NSString *) latest {
1827 - (NSString *) installed {
1832 return !version_.end();
1835 - (BOOL) upgradableAndEssential:(BOOL)essential {
1836 pkgCache::VerIterator current = iterator_.CurrentVer();
1840 value = essential && [self essential] && [self visible];
1842 value = !version_.end() && version_ != current;// && (!essential || ![database_ cache][iterator_].Keep());
1846 - (BOOL) essential {
1851 return [database_ cache][iterator_].InstBroken();
1854 - (BOOL) unfiltered {
1855 NSString *section = [self section];
1856 return section == nil || isSectionVisible(section);
1860 return [self hasSupportingRole] && [self unfiltered];
1864 unsigned char current = iterator_->CurrentState;
1865 return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled;
1868 - (BOOL) halfConfigured {
1869 return iterator_->CurrentState == pkgCache::State::HalfConfigured;
1872 - (BOOL) halfInstalled {
1873 return iterator_->CurrentState == pkgCache::State::HalfInstalled;
1877 pkgDepCache::StateCache &state([database_ cache][iterator_]);
1878 return state.Mode != pkgDepCache::ModeKeep;
1881 - (NSString *) mode {
1882 pkgDepCache::StateCache &state([database_ cache][iterator_]);
1884 switch (state.Mode) {
1885 case pkgDepCache::ModeDelete:
1886 if ((state.iFlags & pkgDepCache::Purge) != 0)
1890 case pkgDepCache::ModeKeep:
1891 if ((state.iFlags & pkgDepCache::AutoKept) != 0)
1895 case pkgDepCache::ModeInstall:
1896 if ((state.iFlags & pkgDepCache::ReInstall) != 0)
1897 return @"Reinstall";
1898 else switch (state.Status) {
1900 return @"Downgrade";
1906 return @"New Install";
1919 - (NSString *) name {
1920 return name_ == nil ? id_ : name_;
1923 - (NSString *) tagline {
1927 - (UIImage *) icon {
1928 NSString *section = [self simpleSection];
1932 if ([icon_ hasPrefix:@"file:///"])
1933 icon = [UIImage imageAtPath:[icon_ substringFromIndex:7]];
1934 if (icon == nil) if (section != nil)
1935 icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]];
1936 if (icon == nil) if (source_ != nil) if (NSString *dicon = [source_ defaultIcon])
1937 if ([dicon hasPrefix:@"file:///"])
1938 icon = [UIImage imageAtPath:[dicon substringFromIndex:7]];
1940 icon = [UIImage applicationImageNamed:@"unknown.png"];
1944 - (NSString *) homepage {
1948 - (NSString *) depiction {
1952 - (Address *) sponsor {
1956 - (Address *) author {
1960 - (NSArray *) files {
1961 NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", id_];
1962 NSMutableArray *files = [NSMutableArray arrayWithCapacity:128];
1965 fin.open([path UTF8String]);
1970 while (std::getline(fin, line))
1971 [files addObject:[NSString stringWithUTF8String:line.c_str()]];
1976 - (NSArray *) relationships {
1977 return relationships_;
1980 - (NSArray *) warnings {
1981 NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]);
1982 const char *name(iterator_.Name());
1984 size_t length(strlen(name));
1985 if (length < 2) invalid:
1986 [warnings addObject:@"illegal package identifier"];
1987 else for (size_t i(0); i != length; ++i)
1989 /* XXX: technically this is not allowed */
1990 (name[i] < 'A' || name[i] > 'Z') &&
1991 (name[i] < 'a' || name[i] > 'z') &&
1992 (name[i] < '0' || name[i] > '9') &&
1993 (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.')
1996 if (strcmp(name, "cydia") != 0) {
1998 bool _private = false;
2001 bool repository = [[self section] isEqualToString:@"Repositories"];
2003 if (NSArray *files = [self files])
2004 for (NSString *file in files)
2005 if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"])
2007 else if (!_private && [file isEqualToString:@"/private"])
2009 else if (!stash && [file isEqualToString:@"/var/stash"])
2012 /* XXX: this is not sensitive enough. only some folders are valid. */
2013 if (cydia && !repository)
2014 [warnings addObject:@"files installed into Cydia.app"];
2016 [warnings addObject:@"files installed with /private/*"];
2018 [warnings addObject:@"files installed to /var/stash"];
2021 return [warnings count] == 0 ? nil : warnings;
2024 - (NSArray *) applications {
2025 NSString *me([[NSBundle mainBundle] bundleIdentifier]);
2027 NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]);
2029 static Pcre application_r("^/Applications/(.*)\\.app/Info.plist$");
2030 if (NSArray *files = [self files])
2031 for (NSString *file in files)
2032 if (application_r(file)) {
2033 NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]);
2034 NSString *id([info objectForKey:@"CFBundleIdentifier"]);
2035 if ([id isEqualToString:me])
2038 NSString *display([info objectForKey:@"CFBundleDisplayName"]);
2040 display = application_r[1];
2042 NSString *bundle([file stringByDeletingLastPathComponent]);
2043 NSString *icon([info objectForKey:@"CFBundleIconFile"]);
2044 if (icon == nil || [icon length] == 0)
2046 NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]);
2048 NSMutableArray *application([NSMutableArray arrayWithCapacity:2]);
2049 [applications addObject:application];
2051 [application addObject:id];
2052 [application addObject:display];
2053 [application addObject:url];
2056 return [applications count] == 0 ? nil : applications;
2059 - (Source *) source {
2061 @synchronized (database_) {
2062 if ([database_ era] != era_ || file_.end())
2065 source_ = [database_ getSource:file_.File()];
2077 - (NSString *) role {
2081 - (BOOL) matches:(NSString *)text {
2087 range = [[self id] rangeOfString:text options:MatchCompareOptions_];
2088 if (range.location != NSNotFound)
2091 range = [[self name] rangeOfString:text options:MatchCompareOptions_];
2092 if (range.location != NSNotFound)
2095 range = [[self tagline] rangeOfString:text options:MatchCompareOptions_];
2096 if (range.location != NSNotFound)
2102 - (bool) hasSupportingRole {
2105 if ([role_ isEqualToString:@"enduser"])
2107 if ([Role_ isEqualToString:@"User"])
2109 if ([role_ isEqualToString:@"hacker"])
2111 if ([Role_ isEqualToString:@"Hacker"])
2113 if ([role_ isEqualToString:@"developer"])
2115 if ([Role_ isEqualToString:@"Developer"])
2120 - (BOOL) hasTag:(NSString *)tag {
2121 return tags_ == nil ? NO : [tags_ containsObject:tag];
2124 - (NSString *) primaryPurpose {
2125 for (NSString *tag in tags_)
2126 if ([tag hasPrefix:@"purpose::"])
2127 return [tag substringFromIndex:9];
2131 - (NSArray *) purposes {
2132 NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]);
2133 for (NSString *tag in tags_)
2134 if ([tag hasPrefix:@"purpose::"])
2135 [purposes addObject:[tag substringFromIndex:9]];
2136 return [purposes count] == 0 ? nil : purposes;
2139 - (bool) isCommercial {
2140 return [self hasTag:@"cydia::commercial"];
2143 - (NSComparisonResult) compareByName:(Package *)package {
2144 NSString *lhs = [self name];
2145 NSString *rhs = [package name];
2147 if ([lhs length] != 0 && [rhs length] != 0) {
2148 unichar lhc = [lhs characterAtIndex:0];
2149 unichar rhc = [rhs characterAtIndex:0];
2151 if (isalpha(lhc) && !isalpha(rhc))
2152 return NSOrderedAscending;
2153 else if (!isalpha(lhc) && isalpha(rhc))
2154 return NSOrderedDescending;
2157 return [lhs compare:rhs options:LaxCompareOptions_];
2160 - (NSComparisonResult) compareBySection:(Package *)package {
2161 NSString *lhs = [self section];
2162 NSString *rhs = [package section];
2164 if (lhs == NULL && rhs != NULL)
2165 return NSOrderedAscending;
2166 else if (lhs != NULL && rhs == NULL)
2167 return NSOrderedDescending;
2168 else if (lhs != NULL && rhs != NULL) {
2169 NSComparisonResult result([lhs compare:rhs options:LaxCompareOptions_]);
2170 return result != NSOrderedSame ? result : [lhs compare:rhs options:ForcedCompareOptions_];
2173 return NSOrderedSame;
2176 - (uint32_t) compareForChanges {
2181 uint32_t timestamp : 30;
2182 uint32_t ignored : 1;
2183 uint32_t upgradable : 1;
2187 bool upgradable([self upgradableAndEssential:YES]);
2188 value.bits.upgradable = upgradable ? 1 : 0;
2191 value.bits.timestamp = 0;
2192 value.bits.ignored = [self ignored] ? 0 : 1;
2193 value.bits.upgradable = 1;
2195 value.bits.timestamp = static_cast<uint32_t>([[self seen] timeIntervalSince1970]) >> 2;
2196 value.bits.ignored = 0;
2197 value.bits.upgradable = 0;
2200 return _not(uint32_t) - value.key;
2204 pkgProblemResolver *resolver = [database_ resolver];
2205 resolver->Clear(iterator_);
2206 resolver->Protect(iterator_);
2207 pkgCacheFile &cache([database_ cache]);
2208 cache->MarkInstall(iterator_, false);
2209 pkgDepCache::StateCache &state((*cache)[iterator_]);
2210 if (!state.Install())
2211 cache->SetReInstall(iterator_, true);
2215 pkgProblemResolver *resolver = [database_ resolver];
2216 resolver->Clear(iterator_);
2217 resolver->Protect(iterator_);
2218 resolver->Remove(iterator_);
2219 [database_ cache]->MarkDelete(iterator_, true);
2222 - (bool) isUnfilteredAndSearchedForBy:(NSString *)search {
2223 _profile(Package$isUnfilteredAndSearchedForBy)
2226 _profile(Package$isUnfilteredAndSearchedForBy$Unfiltered)
2227 value &= [self unfiltered];
2230 _profile(Package$isUnfilteredAndSearchedForBy$Match)
2231 value &= [self matches:search];
2238 - (bool) isInstalledAndVisible:(NSNumber *)number {
2239 return (![number boolValue] || [self visible]) && [self installed] != nil;
2242 - (bool) isVisiblyUninstalledInSection:(NSString *)name {
2243 NSString *section = [self section];
2247 [self installed] == nil && (
2249 section == nil && [name length] == 0 ||
2250 [name isEqualToString:section]
2254 - (bool) isVisibleInSource:(Source *)source {
2255 return [self source] == source && [self visible];
2260 /* Section Class {{{ */
2261 @interface Section : NSObject {
2268 - (NSComparisonResult) compareByName:(Section *)section;
2269 - (Section *) initWithName:(NSString *)name;
2270 - (Section *) initWithName:(NSString *)name row:(size_t)row;
2271 - (Section *) initWithIndex:(unichar)index row:(size_t)row;
2272 - (NSString *) name;
2276 - (void) addToCount;
2280 @implementation Section
2287 - (NSComparisonResult) compareByName:(Section *)section {
2288 NSString *lhs = [self name];
2289 NSString *rhs = [section name];
2291 if ([lhs length] != 0 && [rhs length] != 0) {
2292 unichar lhc = [lhs characterAtIndex:0];
2293 unichar rhc = [rhs characterAtIndex:0];
2295 if (isalpha(lhc) && !isalpha(rhc))
2296 return NSOrderedAscending;
2297 else if (!isalpha(lhc) && isalpha(rhc))
2298 return NSOrderedDescending;
2301 return [lhs compare:rhs options:LaxCompareOptions_];
2304 - (Section *) initWithName:(NSString *)name {
2305 return [self initWithName:name row:0];
2308 - (Section *) initWithName:(NSString *)name row:(size_t)row {
2309 if ((self = [super init]) != nil) {
2310 name_ = [name retain];
2316 - (Section *) initWithIndex:(unichar)index row:(size_t)row {
2317 if ((self = [super init]) != nil) {
2318 name_ = [(index == '#' ? @"123" : [NSString stringWithCharacters:&index length:1]) retain];
2324 - (NSString *) name {
2340 - (void) addToCount {
2348 static NSArray *Finishes_;
2350 /* Database Implementation {{{ */
2351 @implementation Database
2353 + (Database *) sharedInstance {
2354 static Database *instance;
2355 if (instance == nil)
2356 instance = [[Database alloc] init];
2369 - (void) _readCydia:(NSNumber *)fd { _pooled
2370 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
2371 std::istream is(&ib);
2374 static Pcre finish_r("^finish:([^:]*)$");
2376 while (std::getline(is, line)) {
2377 const char *data(line.c_str());
2378 size_t size = line.size();
2379 lprintf("C:%s\n", data);
2381 if (finish_r(data, size)) {
2382 NSString *finish = finish_r[1];
2383 int index = [Finishes_ indexOfObject:finish];
2384 if (index != INT_MAX && index > Finish_)
2392 - (void) _readStatus:(NSNumber *)fd { _pooled
2393 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
2394 std::istream is(&ib);
2397 static Pcre conffile_r("^status: [^ ]* : conffile-prompt : (.*?) *$");
2398 static Pcre pmstatus_r("^([^:]*):([^:]*):([^:]*):(.*)$");
2400 while (std::getline(is, line)) {
2401 const char *data(line.c_str());
2402 size_t size = line.size();
2403 lprintf("S:%s\n", data);
2405 if (conffile_r(data, size)) {
2406 [delegate_ setConfigurationData:conffile_r[1]];
2407 } else if (strncmp(data, "status: ", 8) == 0) {
2408 NSString *string = [NSString stringWithUTF8String:(data + 8)];
2409 [delegate_ setProgressTitle:string];
2410 } else if (pmstatus_r(data, size)) {
2411 std::string type([pmstatus_r[1] UTF8String]);
2412 NSString *id = pmstatus_r[2];
2414 float percent([pmstatus_r[3] floatValue]);
2415 [delegate_ setProgressPercent:(percent / 100)];
2417 NSString *string = pmstatus_r[4];
2419 if (type == "pmerror")
2420 [delegate_ performSelectorOnMainThread:@selector(_setProgressError:)
2421 withObject:[NSArray arrayWithObjects:string, id, nil]
2424 else if (type == "pmstatus") {
2425 [delegate_ setProgressTitle:string];
2426 } else if (type == "pmconffile")
2427 [delegate_ setConfigurationData:string];
2428 else _assert(false);
2429 } else _assert(false);
2435 - (void) _readOutput:(NSNumber *)fd { _pooled
2436 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
2437 std::istream is(&ib);
2440 while (std::getline(is, line)) {
2441 lprintf("O:%s\n", line.c_str());
2442 [delegate_ addProgressOutput:[NSString stringWithUTF8String:line.c_str()]];
2452 - (Package *) packageWithName:(NSString *)name {
2453 if (static_cast<pkgDepCache *>(cache_) == NULL)
2455 pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String]));
2456 return iterator.end() ? nil : [Package packageWithIterator:iterator database:self];
2459 - (Database *) init {
2460 if ((self = [super init]) != nil) {
2467 sources_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain];
2468 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
2472 _assert(pipe(fds) != -1);
2475 _config->Set("APT::Keep-Fds::", cydiafd_);
2476 setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int));
2479 detachNewThreadSelector:@selector(_readCydia:)
2481 withObject:[[NSNumber numberWithInt:fds[0]] retain]
2484 _assert(pipe(fds) != -1);
2488 detachNewThreadSelector:@selector(_readStatus:)
2490 withObject:[[NSNumber numberWithInt:fds[0]] retain]
2493 _assert(pipe(fds) != -1);
2494 _assert(dup2(fds[0], 0) != -1);
2495 _assert(close(fds[0]) != -1);
2497 input_ = fdopen(fds[1], "a");
2499 _assert(pipe(fds) != -1);
2500 _assert(dup2(fds[1], 1) != -1);
2501 _assert(close(fds[1]) != -1);
2504 detachNewThreadSelector:@selector(_readOutput:)
2506 withObject:[[NSNumber numberWithInt:fds[0]] retain]
2511 - (pkgCacheFile &) cache {
2515 - (pkgDepCache::Policy *) policy {
2519 - (pkgRecords *) records {
2523 - (pkgProblemResolver *) resolver {
2527 - (pkgAcquire &) fetcher {
2531 - (pkgSourceList &) list {
2535 - (NSArray *) packages {
2539 - (NSArray *) sources {
2540 return [sources_ allValues];
2543 - (NSArray *) issues {
2544 if (cache_->BrokenCount() == 0)
2547 NSMutableArray *issues([NSMutableArray arrayWithCapacity:4]);
2549 for (Package *package in packages_) {
2550 if (![package broken])
2552 pkgCache::PkgIterator pkg([package iterator]);
2554 NSMutableArray *entry([NSMutableArray arrayWithCapacity:4]);
2555 [entry addObject:[package name]];
2556 [issues addObject:entry];
2558 pkgCache::VerIterator ver(cache_[pkg].InstVerIter(cache_));
2562 for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) {
2563 pkgCache::DepIterator start;
2564 pkgCache::DepIterator end;
2565 dep.GlobOr(start, end); // ++dep
2567 if (!cache_->IsImportantDep(end))
2569 if ((cache_[end] & pkgDepCache::DepGInstall) != 0)
2572 NSMutableArray *failure([NSMutableArray arrayWithCapacity:4]);
2573 [entry addObject:failure];
2574 [failure addObject:[NSString stringWithUTF8String:start.DepType()]];
2576 Package *package([self packageWithName:[NSString stringWithUTF8String:start.TargetPkg().Name()]]);
2577 [failure addObject:[package name]];
2579 pkgCache::PkgIterator target(start.TargetPkg());
2580 if (target->ProvidesList != 0)
2581 [failure addObject:@"?"];
2583 pkgCache::VerIterator ver(cache_[target].InstVerIter(cache_));
2585 [failure addObject:[NSString stringWithUTF8String:ver.VerStr()]];
2586 else if (!cache_[target].CandidateVerIter(cache_).end())
2587 [failure addObject:@"-"];
2588 else if (target->ProvidesList == 0)
2589 [failure addObject:@"!"];
2591 [failure addObject:@"%"];
2595 if (start.TargetVer() != 0)
2596 [failure addObject:[NSString stringWithFormat:@"%s %s", start.CompType(), start.TargetVer()]];
2607 - (void) reloadData { _pooled
2608 @synchronized (self) {
2631 if (!cache_.Open(progress_, true)) {
2633 if (!_error->PopMessage(error))
2636 lprintf("cache_.Open():[%s]\n", error.c_str());
2638 if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ")
2639 [delegate_ repairWithSelector:@selector(configure)];
2640 else if (error == "The package lists or status file could not be parsed or opened.")
2641 [delegate_ repairWithSelector:@selector(update)];
2642 // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)")
2643 // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)")
2644 // else if (error == "The list of sources could not be read.")
2645 else _assert(false);
2651 now_ = [[NSDate date] retain];
2653 policy_ = new pkgDepCache::Policy();
2654 records_ = new pkgRecords(cache_);
2655 resolver_ = new pkgProblemResolver(cache_);
2656 fetcher_ = new pkgAcquire(&status_);
2659 list_ = new pkgSourceList();
2660 _assert(list_->ReadMainList());
2662 _assert(cache_->DelCount() == 0 && cache_->InstCount() == 0);
2663 _assert(pkgApplyStatus(cache_));
2665 if (cache_->BrokenCount() != 0) {
2666 _assert(pkgFixBroken(cache_));
2667 _assert(cache_->BrokenCount() == 0);
2668 _assert(pkgMinimizeUpgrade(cache_));
2671 [sources_ removeAllObjects];
2672 for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) {
2673 std::vector<pkgIndexFile *> *indices = (*source)->GetIndexFiles();
2674 for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index)
2676 setObject:[[[Source alloc] initWithMetaIndex:*source] autorelease]
2677 forKey:[NSNumber numberWithLong:reinterpret_cast<uintptr_t>(*index)]
2681 [packages_ removeAllObjects];
2683 for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator)
2684 if (Package *package = [Package packageWithIterator:iterator database:self])
2685 [packages_ addObject:package];
2687 [packages_ sortUsingSelector:@selector(compareByName:)];
2690 _config->Set("Acquire::http::Timeout", 15);
2691 _config->Set("Acquire::http::MaxParallel", 4);
2694 - (void) configure {
2695 NSString *dpkg = [NSString stringWithFormat:@"dpkg --configure -a --status-fd %u", statusfd_];
2696 system([dpkg UTF8String]);
2704 Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
2705 _assert(!_error->PendingError());
2708 fetcher.Clean(_config->FindDir("Dir::Cache::Archives"));
2711 public pkgArchiveCleaner
2714 virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) {
2719 if (!cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)) {
2721 while (_error->PopMessage(error))
2722 lprintf("ArchiveCleaner: %s\n", error.c_str());
2727 pkgRecords records(cache_);
2729 lock_ = new FileFd();
2730 lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
2731 _assert(!_error->PendingError());
2734 // XXX: explain this with an error message
2735 _assert(list.ReadMainList());
2737 manager_ = (_system->CreatePM(cache_));
2738 _assert(manager_->GetArchives(fetcher_, &list, &records));
2739 _assert(!_error->PendingError());
2743 NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; {
2745 _assert(list.ReadMainList());
2746 for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source)
2747 [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]];
2750 if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) {
2755 bool failed = false;
2756 for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) {
2757 if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete)
2760 std::string uri = (*item)->DescURI();
2761 std::string error = (*item)->ErrorText;
2763 lprintf("pAf:%s:%s\n", uri.c_str(), error.c_str());
2766 [delegate_ performSelectorOnMainThread:@selector(_setProgressError:)
2767 withObject:[NSArray arrayWithObjects:
2768 [NSString stringWithUTF8String:error.c_str()],
2780 pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_);
2782 if (_error->PendingError()) {
2787 if (result == pkgPackageManager::Failed) {
2792 if (result != pkgPackageManager::Completed) {
2797 NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; {
2799 _assert(list.ReadMainList());
2800 for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source)
2801 [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]];
2804 if (![before isEqualToArray:after])
2809 _assert(pkgDistUpgrade(cache_));
2813 [self updateWithStatus:status_];
2816 - (void) updateWithStatus:(Status &)status {
2818 _assert(list.ReadMainList());
2821 lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
2822 _assert(!_error->PendingError());
2824 pkgAcquire fetcher(&status);
2825 _assert(list.GetIndexes(&fetcher));
2827 if (fetcher.Run(PulseInterval_) != pkgAcquire::Failed) {
2828 bool failed = false;
2829 for (pkgAcquire::ItemIterator item = fetcher.ItemsBegin(); item != fetcher.ItemsEnd(); item++)
2830 if ((*item)->Status != pkgAcquire::Item::StatDone) {
2831 (*item)->Finished();
2835 if (!failed && _config->FindB("APT::Get::List-Cleanup", true) == true) {
2836 _assert(fetcher.Clean(_config->FindDir("Dir::State::lists")));
2837 _assert(fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/"));
2840 [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"];
2845 - (void) setDelegate:(id)delegate {
2846 delegate_ = delegate;
2847 status_.setDelegate(delegate);
2848 progress_.setDelegate(delegate);
2851 - (Source *) getSource:(const pkgCache::PkgFileIterator &)file {
2852 pkgIndexFile *index(NULL);
2853 list_->FindIndex(file, index);
2854 return [sources_ objectForKey:[NSNumber numberWithLong:reinterpret_cast<uintptr_t>(index)]];
2860 /* PopUp Windows {{{ */
2861 @interface PopUpView : UIView {
2862 _transient id delegate_;
2863 UITransitionView *transition_;
2868 - (id) initWithView:(UIView *)view delegate:(id)delegate;
2872 @implementation PopUpView
2875 [transition_ setDelegate:nil];
2876 [transition_ release];
2882 [transition_ transition:UITransitionPushFromTop toView:nil];
2885 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to {
2886 if (from != nil && to == nil)
2887 [self removeFromSuperview];
2890 - (id) initWithView:(UIView *)view delegate:(id)delegate {
2891 if ((self = [super initWithFrame:[view bounds]]) != nil) {
2892 delegate_ = delegate;
2894 transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]];
2895 [self addSubview:transition_];
2897 overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]];
2899 [view addSubview:self];
2901 [transition_ setDelegate:self];
2903 UIView *blank = [[[UIView alloc] initWithFrame:[transition_ bounds]] autorelease];
2904 [transition_ transition:UITransitionNone toView:blank];
2905 [transition_ transition:UITransitionPushFromBottom toView:overlay_];
2912 /* Mail Composition {{{ */
2913 @interface MailToView : PopUpView {
2914 MailComposeController *controller_;
2917 - (id) initWithView:(UIView *)view delegate:(id)delegate url:(NSURL *)url;
2921 @implementation MailToView
2924 [controller_ release];
2928 - (void) mailComposeControllerWillAttemptToSend:(MailComposeController *)controller {
2932 - (void) mailComposeControllerDidAttemptToSend:(MailComposeController *)controller mailDelivery:(id)delivery {
2933 NSLog(@"did:%@", delivery);
2934 // [UIApp setStatusBarShowsProgress:NO];
2935 if ([controller error]){
2936 NSArray *buttons = [NSArray arrayWithObjects:@"OK", nil];
2937 UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:@"Error" buttons:buttons defaultButtonIndex:0 delegate:self context:self];
2938 [mailAlertSheet setBodyText:[controller error]];
2939 [mailAlertSheet popupAlertAnimated:YES];
2943 - (void) showError {
2944 NSLog(@"%@", [controller_ error]);
2945 NSArray *buttons = [NSArray arrayWithObjects:@"OK", nil];
2946 UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:@"Error" buttons:buttons defaultButtonIndex:0 delegate:self context:self];
2947 [mailAlertSheet setBodyText:[controller_ error]];
2948 [mailAlertSheet popupAlertAnimated:YES];
2951 - (void) deliverMessage { _pooled
2955 if (![controller_ deliverMessage])
2956 [self performSelectorOnMainThread:@selector(showError) withObject:nil waitUntilDone:NO];
2959 - (void) mailComposeControllerCompositionFinished:(MailComposeController *)controller {
2960 if ([controller_ needsDelivery])
2961 [NSThread detachNewThreadSelector:@selector(deliverMessage) toTarget:self withObject:nil];
2966 - (id) initWithView:(UIView *)view delegate:(id)delegate url:(NSURL *)url {
2967 if ((self = [super initWithView:view delegate:delegate]) != nil) {
2968 controller_ = [[MailComposeController alloc] initForContentSize:[overlay_ bounds].size];
2969 [controller_ setDelegate:self];
2970 [controller_ initializeUI];
2971 [controller_ setupForURL:url];
2973 UIView *view([controller_ view]);
2974 [overlay_ addSubview:view];
2980 /* Confirmation View {{{ */
2981 bool DepSubstrate(const pkgCache::VerIterator &iterator) {
2982 if (!iterator.end())
2983 for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) {
2984 if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends)
2986 pkgCache::PkgIterator package(dep.TargetPkg());
2989 if (strcmp(package.Name(), "mobilesubstrate") == 0)
2996 @protocol ConfirmationViewDelegate
3001 @interface ConfirmationView : BrowserView {
3002 _transient Database *database_;
3003 UIActionSheet *essential_;
3010 - (id) initWithBook:(RVBook *)book database:(Database *)database;
3014 @implementation ConfirmationView
3021 if (essential_ != nil)
3022 [essential_ release];
3028 [book_ popFromSuperviewAnimated:YES];
3031 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
3032 NSString *context([sheet context]);
3034 if ([context isEqualToString:@"remove"]) {
3042 [delegate_ confirm];
3049 } else if ([context isEqualToString:@"unable"]) {
3053 [super alertSheet:sheet buttonClicked:button];
3056 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
3057 [window setValue:changes_ forKey:@"changes"];
3058 [window setValue:issues_ forKey:@"issues"];
3059 [window setValue:sizes_ forKey:@"sizes"];
3060 [super webView:sender didClearWindowObject:window forFrame:frame];
3063 - (id) initWithBook:(RVBook *)book database:(Database *)database {
3064 if ((self = [super initWithBook:book]) != nil) {
3065 database_ = database;
3067 NSMutableArray *installing = [NSMutableArray arrayWithCapacity:16];
3068 NSMutableArray *reinstalling = [NSMutableArray arrayWithCapacity:16];
3069 NSMutableArray *upgrading = [NSMutableArray arrayWithCapacity:16];
3070 NSMutableArray *downgrading = [NSMutableArray arrayWithCapacity:16];
3071 NSMutableArray *removing = [NSMutableArray arrayWithCapacity:16];
3075 pkgDepCache::Policy *policy([database_ policy]);
3077 pkgCacheFile &cache([database_ cache]);
3078 NSArray *packages = [database_ packages];
3079 for (Package *package in packages) {
3080 pkgCache::PkgIterator iterator = [package iterator];
3081 pkgDepCache::StateCache &state(cache[iterator]);
3083 NSString *name([package name]);
3085 if (state.NewInstall())
3086 [installing addObject:name];
3087 else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
3088 [reinstalling addObject:name];
3089 else if (state.Upgrade())
3090 [upgrading addObject:name];
3091 else if (state.Downgrade())
3092 [downgrading addObject:name];
3093 else if (state.Delete()) {
3094 if ([package essential])
3096 [removing addObject:name];
3099 substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator));
3100 substrate_ |= DepSubstrate(iterator.CurrentVer());
3105 else if (Advanced_ || true) {
3106 essential_ = [[UIActionSheet alloc]
3107 initWithTitle:@"Removing Essentials"
3108 buttons:[NSArray arrayWithObjects:
3109 @"Cancel Operation (Safe)",
3110 @"Force Removal (Unsafe)",
3112 defaultButtonIndex:0
3118 [essential_ setDestructiveButton:[[essential_ buttons] objectAtIndex:0]];
3120 [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."];
3122 essential_ = [[UIActionSheet alloc]
3123 initWithTitle:@"Unable to Comply"
3124 buttons:[NSArray arrayWithObjects:@"Okay", nil]
3125 defaultButtonIndex:0
3130 [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."];
3133 changes_ = [[NSArray alloc] initWithObjects:
3141 issues_ = [database_ issues];
3143 issues_ = [issues_ retain];
3145 sizes_ = [[NSArray alloc] initWithObjects:
3146 SizeString([database_ fetcher].FetchNeeded()),
3147 SizeString([database_ fetcher].PartialPresent()),
3148 SizeString([database_ cache]->UsrSize()),
3151 [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"confirm" ofType:@"html"]]];
3155 - (NSString *) backButtonTitle {
3159 - (NSString *) leftButtonTitle {
3163 - (id) rightButtonTitle {
3164 return issues_ != nil ? nil : [super rightButtonTitle];
3167 - (id) _rightButtonTitle {
3168 #if AlwaysReload || IgnoreInstall
3169 return [super _rightButtonTitle];
3175 - (void) _leftButtonClicked {
3180 - (void) _rightButtonClicked {
3182 return [super _rightButtonClicked];
3184 if (essential_ != nil)
3185 [essential_ popupAlertAnimated:YES];
3189 [delegate_ confirm];
3197 /* Progress Data {{{ */
3198 @interface ProgressData : NSObject {
3204 - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object;
3211 @implementation ProgressData
3213 - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object {
3214 if ((self = [super init]) != nil) {
3215 selector_ = selector;
3235 /* Progress View {{{ */
3236 @interface ProgressView : UIView <
3237 ConfigurationDelegate,
3240 _transient Database *database_;
3242 UIView *background_;
3243 UITransitionView *transition_;
3245 UINavigationBar *navbar_;
3246 UIProgressBar *progress_;
3247 UITextView *output_;
3248 UITextLabel *status_;
3249 UIPushButton *close_;
3252 SHA1SumValue springlist_;
3253 SHA1SumValue notifyconf_;
3254 SHA1SumValue sandplate_;
3257 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to;
3259 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database delegate:(id)delegate;
3260 - (void) setContentView:(UIView *)view;
3263 - (void) _retachThread;
3264 - (void) _detachNewThreadData:(ProgressData *)data;
3265 - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title;
3271 @protocol ProgressViewDelegate
3272 - (void) progressViewIsComplete:(ProgressView *)sender;
3275 @implementation ProgressView
3278 [transition_ setDelegate:nil];
3279 [navbar_ setDelegate:nil];
3282 if (background_ != nil)
3283 [background_ release];
3284 [transition_ release];
3287 [progress_ release];
3294 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to {
3295 if (bootstrap_ && from == overlay_ && to == view_)
3299 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database delegate:(id)delegate {
3300 if ((self = [super initWithFrame:frame]) != nil) {
3301 database_ = database;
3302 delegate_ = delegate;
3304 transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]];
3305 [transition_ setDelegate:self];
3307 overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]];
3310 [overlay_ setBackgroundColor:[UIColor blackColor]];
3312 background_ = [[UIView alloc] initWithFrame:[self bounds]];
3313 [background_ setBackgroundColor:[UIColor blackColor]];
3314 [self addSubview:background_];
3317 [self addSubview:transition_];
3319 CGSize navsize = [UINavigationBar defaultSize];
3320 CGRect navrect = {{0, 0}, navsize};
3322 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
3323 [overlay_ addSubview:navbar_];
3325 [navbar_ setBarStyle:1];
3326 [navbar_ setDelegate:self];
3328 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:nil] autorelease];
3329 [navbar_ pushNavigationItem:navitem];
3331 CGRect bounds = [overlay_ bounds];
3332 CGSize prgsize = [UIProgressBar defaultSize];
3335 (bounds.size.width - prgsize.width) / 2,
3336 bounds.size.height - prgsize.height - 20
3339 progress_ = [[UIProgressBar alloc] initWithFrame:prgrect];
3340 [progress_ setStyle:0];
3342 status_ = [[UITextLabel alloc] initWithFrame:CGRectMake(
3344 bounds.size.height - prgsize.height - 50,
3345 bounds.size.width - 20,
3349 [status_ setColor:[UIColor whiteColor]];
3350 [status_ setBackgroundColor:[UIColor clearColor]];
3352 [status_ setCentersHorizontally:YES];
3353 //[status_ setFont:font];
3356 output_ = [[UITextView alloc] initWithFrame:CGRectMake(
3358 navrect.size.height + 20,
3359 bounds.size.width - 20,
3360 bounds.size.height - navsize.height - 62 - navrect.size.height
3364 //[output_ setTextFont:@"Courier New"];
3365 [output_ setTextSize:12];
3367 [output_ setTextColor:[UIColor whiteColor]];
3368 [output_ setBackgroundColor:[UIColor clearColor]];
3370 [output_ setMarginTop:0];
3371 [output_ setAllowsRubberBanding:YES];
3372 [output_ setEditable:NO];
3374 [overlay_ addSubview:output_];
3376 close_ = [[UIPushButton alloc] initWithFrame:CGRectMake(
3378 bounds.size.height - prgsize.height - 50,
3379 bounds.size.width - 20,
3383 [close_ setAutosizesToFit:NO];
3384 [close_ setDrawsShadow:YES];
3385 [close_ setStretchBackground:YES];
3386 [close_ setEnabled:YES];
3388 UIFont *bold = [UIFont boldSystemFontOfSize:22];
3389 [close_ setTitleFont:bold];
3391 [close_ addTarget:self action:@selector(closeButtonPushed) forEvents:kUIControlEventMouseUpInside];
3392 [close_ setBackground:[UIImage applicationImageNamed:@"green-up.png"] forState:0];
3393 [close_ setBackground:[UIImage applicationImageNamed:@"green-dn.png"] forState:1];
3397 - (void) setContentView:(UIView *)view {
3398 view_ = [view retain];
3401 - (void) resetView {
3402 [transition_ transition:6 toView:view_];
3405 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
3406 NSString *context([sheet context]);
3408 if ([context isEqualToString:@"error"])
3410 else if ([context isEqualToString:@"conffile"]) {
3411 FILE *input = [database_ input];
3415 fprintf(input, "N\n");
3419 fprintf(input, "Y\n");
3430 - (void) closeButtonPushed {
3439 [delegate_ suspendWithAnimation:YES];
3443 system("launchctl stop com.apple.SpringBoard");
3447 system("launchctl unload "SpringBoard_"; launchctl load "SpringBoard_);
3456 - (void) _retachThread {
3457 UINavigationItem *item = [navbar_ topItem];
3458 [item setTitle:@"Complete"];
3460 [overlay_ addSubview:close_];
3461 [progress_ removeFromSuperview];
3462 [status_ removeFromSuperview];
3464 [delegate_ progressViewIsComplete:self];
3467 FileFd file(SandboxTemplate_, FileFd::ReadOnly);
3468 MMap mmap(file, MMap::ReadOnly);
3470 sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size());
3471 if (!(sandplate_ == sha1.Result()))
3476 FileFd file(NotifyConfig_, FileFd::ReadOnly);
3477 MMap mmap(file, MMap::ReadOnly);
3479 sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size());
3480 if (!(notifyconf_ == sha1.Result()))
3485 FileFd file(SpringBoard_, FileFd::ReadOnly);
3486 MMap mmap(file, MMap::ReadOnly);
3488 sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size());
3489 if (!(springlist_ == sha1.Result()))
3494 case 0: [close_ setTitle:@"Return to Cydia"]; break;
3495 case 1: [close_ setTitle:@"Close Cydia (Restart)"]; break;
3496 case 2: [close_ setTitle:@"Restart SpringBoard"]; break;
3497 case 3: [close_ setTitle:@"Reload SpringBoard"]; break;
3498 case 4: [close_ setTitle:@"Reboot Device"]; break;
3501 #define Cache_ "/User/Library/Caches/com.apple.mobile.installation.plist"
3503 if (NSMutableDictionary *cache = [[NSMutableDictionary alloc] initWithContentsOfFile:@ Cache_]) {
3504 [cache autorelease];
3506 NSFileManager *manager = [NSFileManager defaultManager];
3507 NSError *error = nil;
3509 id system = [cache objectForKey:@"System"];
3514 if (stat(Cache_, &info) == -1)
3517 [system removeAllObjects];
3519 if (NSArray *apps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]) {
3520 for (NSString *app in apps)
3521 if ([app hasSuffix:@".app"]) {
3522 NSString *path = [@"/Applications" stringByAppendingPathComponent:app];
3523 NSString *plist = [path stringByAppendingPathComponent:@"Info.plist"];
3524 if (NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithContentsOfFile:plist]) {
3526 if ([info objectForKey:@"CFBundleIdentifier"] != nil) {
3527 [info setObject:path forKey:@"Path"];
3528 [info setObject:@"System" forKey:@"ApplicationType"];
3529 [system addInfoDictionary:info];
3535 [cache writeToFile:@Cache_ atomically:YES];
3537 if (chown(Cache_, info.st_uid, info.st_gid) == -1)
3539 if (chmod(Cache_, info.st_mode) == -1)
3543 lprintf("%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
3546 notify_post("com.apple.mobile.application_installed");
3548 [delegate_ setStatusBarShowsProgress:NO];
3551 - (void) _detachNewThreadData:(ProgressData *)data { _pooled
3552 [[data target] performSelector:[data selector] withObject:[data object]];
3555 [self performSelectorOnMainThread:@selector(_retachThread) withObject:nil waitUntilDone:YES];
3558 - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title {
3559 UINavigationItem *item = [navbar_ topItem];
3560 [item setTitle:title];
3562 [status_ setText:nil];
3563 [output_ setText:@""];
3564 [progress_ setProgress:0];
3566 [close_ removeFromSuperview];
3567 [overlay_ addSubview:progress_];
3568 [overlay_ addSubview:status_];
3570 [delegate_ setStatusBarShowsProgress:YES];
3574 FileFd file(SandboxTemplate_, FileFd::ReadOnly);
3575 MMap mmap(file, MMap::ReadOnly);
3577 sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size());
3578 sandplate_ = sha1.Result();
3582 FileFd file(NotifyConfig_, FileFd::ReadOnly);
3583 MMap mmap(file, MMap::ReadOnly);
3585 sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size());
3586 notifyconf_ = sha1.Result();
3590 FileFd file(SpringBoard_, FileFd::ReadOnly);
3591 MMap mmap(file, MMap::ReadOnly);
3593 sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size());
3594 springlist_ = sha1.Result();
3597 [transition_ transition:6 toView:overlay_];
3600 detachNewThreadSelector:@selector(_detachNewThreadData:)
3602 withObject:[[ProgressData alloc]
3603 initWithSelector:selector
3610 - (void) repairWithSelector:(SEL)selector {
3612 detachNewThreadSelector:selector
3619 - (void) setConfigurationData:(NSString *)data {
3621 performSelectorOnMainThread:@selector(_setConfigurationData:)
3627 - (void) setProgressError:(NSString *)error forPackage:(NSString *)id {
3628 Package *package = id == nil ? nil : [database_ packageWithName:id];
3630 UIActionSheet *sheet = [[[UIActionSheet alloc]
3631 initWithTitle:(package == nil ? id : [package name])
3632 buttons:[NSArray arrayWithObjects:@"Okay", nil]
3633 defaultButtonIndex:0
3638 [sheet setBodyText:error];
3639 [sheet popupAlertAnimated:YES];
3642 - (void) setProgressTitle:(NSString *)title {
3644 performSelectorOnMainThread:@selector(_setProgressTitle:)
3650 - (void) setProgressPercent:(float)percent {
3652 performSelectorOnMainThread:@selector(_setProgressPercent:)
3653 withObject:[NSNumber numberWithFloat:percent]
3658 - (void) startProgress {
3661 - (void) addProgressOutput:(NSString *)output {
3663 performSelectorOnMainThread:@selector(_addProgressOutput:)
3669 - (bool) isCancelling:(size_t)received {
3673 - (void) _setConfigurationData:(NSString *)data {
3674 static Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$");
3676 _assert(conffile_r(data));
3678 NSString *ofile = conffile_r[1];
3679 //NSString *nfile = conffile_r[2];
3681 UIActionSheet *sheet = [[[UIActionSheet alloc]
3682 initWithTitle:@"Configuration Upgrade"
3683 buttons:[NSArray arrayWithObjects:
3684 @"Keep My Old Copy",
3685 @"Accept The New Copy",
3686 // XXX: @"See What Changed",
3688 defaultButtonIndex:0
3693 [sheet setBodyText:[NSString stringWithFormat:
3694 @"The following file has been changed by both the package maintainer and by you (or for you by a script).\n\n%@"
3697 [sheet popupAlertAnimated:YES];
3700 - (void) _setProgressTitle:(NSString *)title {
3701 NSMutableArray *words([[title componentsSeparatedByString:@" "] mutableCopy]);
3702 for (size_t i(0), e([words count]); i != e; ++i) {
3703 NSString *word([words objectAtIndex:i]);
3704 if (Package *package = [database_ packageWithName:word])
3705 [words replaceObjectAtIndex:i withObject:[package name]];
3708 [status_ setText:[words componentsJoinedByString:@" "]];
3711 - (void) _setProgressPercent:(NSNumber *)percent {
3712 [progress_ setProgress:[percent floatValue]];
3715 - (void) _addProgressOutput:(NSString *)output {
3716 [output_ setText:[NSString stringWithFormat:@"%@\n%@", [output_ text], output]];
3717 CGSize size = [output_ contentSize];
3718 CGRect rect = {{0, size.height}, {size.width, 0}};
3719 [output_ scrollRectToVisible:rect animated:YES];
3722 - (BOOL) isRunning {
3729 /* Package Cell {{{ */
3730 @interface PackageCell : UITableCell {
3733 NSString *description_;
3739 UITextLabel *status_;
3743 - (PackageCell *) init;
3744 - (void) setPackage:(Package *)package;
3746 + (int) heightForPackage:(Package *)package;
3750 @implementation PackageCell
3752 - (void) clearPackage {
3763 if (description_ != nil) {
3764 [description_ release];
3768 if (source_ != nil) {
3773 if (badge_ != nil) {
3780 [self clearPackage];
3787 - (PackageCell *) init {
3788 if ((self = [super init]) != nil) {
3790 status_ = [[UITextLabel alloc] initWithFrame:CGRectMake(48, 68, 280, 20)];
3791 [status_ setBackgroundColor:[UIColor clearColor]];
3792 [status_ setFont:small];
3797 - (void) setPackage:(Package *)package {
3798 [self clearPackage];
3800 Source *source = [package source];
3801 NSString *section = [package simpleSection];
3803 icon_ = [[package icon] retain];
3805 name_ = [[package name] retain];
3806 description_ = [[package tagline] retain];
3807 commercial_ = [package isCommercial];
3809 NSString *label = nil;
3810 bool trusted = false;
3812 if (source != nil) {
3813 label = [source label];
3814 trusted = [source trusted];
3815 } else if ([[package id] isEqualToString:@"firmware"])
3818 label = @"Unknown/Local";
3820 NSString *from = [NSString stringWithFormat:@"from %@", label];
3822 if (section != nil && ![section isEqualToString:label])
3823 from = [from stringByAppendingString:[NSString stringWithFormat:@" (%@)", section]];
3825 source_ = [from retain];
3827 if (NSString *purpose = [package primaryPurpose])
3828 if ((badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]) != nil)
3829 badge_ = [badge_ retain];
3832 if (NSString *mode = [package mode]) {
3833 [badge_ setImage:[UIImage applicationImageNamed:
3834 [mode isEqualToString:@"Remove"] || [mode isEqualToString:@"Purge"] ? @"removing.png" : @"installing.png"
3837 [status_ setText:[NSString stringWithFormat:@"Queued for %@", mode]];
3838 [status_ setColor:[UIColor colorWithCGColor:Blueish_]];
3839 } else if ([package half]) {
3840 [badge_ setImage:[UIImage applicationImageNamed:@"damaged.png"]];
3841 [status_ setText:@"Package Damaged"];
3842 [status_ setColor:[UIColor redColor]];
3844 [badge_ setImage:nil];
3845 [status_ setText:nil];
3852 - (void) drawRect:(CGRect)rect {
3854 //[self setBackgroundColor:(commercial_ ? CommercialColor_ : [UIColor whiteColor])];
3858 [super drawRect:rect];
3861 - (void) drawBackgroundInRect:(CGRect)rect withFade:(float)fade {
3862 if (fade == 0 && commercial_) {
3863 CGContextRef context(UIGraphicsGetCurrentContext());
3864 [[self backgroundColor] set];
3866 back.size.height -= 1;
3867 CGContextFillRect(context, back);
3870 [super drawBackgroundInRect:rect withFade:fade];
3873 - (void) drawContentInRect:(CGRect)rect selected:(BOOL)selected {
3876 rect.size = [icon_ size];
3878 rect.size.width /= 2;
3879 rect.size.height /= 2;
3881 rect.origin.x = 25 - rect.size.width / 2;
3882 rect.origin.y = 25 - rect.size.height / 2;
3884 [icon_ drawInRect:rect];
3887 if (badge_ != nil) {
3888 CGSize size = [badge_ size];
3890 [badge_ drawAtPoint:CGPointMake(
3891 36 - size.width / 2,
3892 36 - size.height / 2
3900 UISetColor(commercial_ ? Purple_ : Black_);
3901 [name_ drawAtPoint:CGPointMake(48, 8) forWidth:240 withFont:Font18Bold_ ellipsis:2];
3902 [source_ drawAtPoint:CGPointMake(58, 29) forWidth:225 withFont:Font12_ ellipsis:2];
3905 UISetColor(commercial_ ? Purplish_ : Gray_);
3906 [description_ drawAtPoint:CGPointMake(12, 46) forWidth:280 withFont:Font14_ ellipsis:2];
3908 [super drawContentInRect:rect selected:selected];
3911 + (int) heightForPackage:(Package *)package {
3912 NSString *tagline([package tagline]);
3913 int height = tagline == nil || [tagline length] == 0 ? -17 : 0;
3915 if ([package hasMode] || [package half])
3924 /* Section Cell {{{ */
3925 @interface SectionCell : UISimpleTableCell {
3930 _UISwitchSlider *switch_;
3935 - (void) setSection:(Section *)section editing:(BOOL)editing;
3939 @implementation SectionCell
3941 - (void) clearSection {
3942 if (section_ != nil) {
3952 if (count_ != nil) {
3959 [self clearSection];
3966 if ((self = [super init]) != nil) {
3967 icon_ = [[UIImage applicationImageNamed:@"folder.png"] retain];
3969 switch_ = [[_UISwitchSlider alloc] initWithFrame:CGRectMake(218, 9, 60, 25)];
3970 [switch_ addTarget:self action:@selector(onSwitch:) forEvents:kUIControlEventMouseUpInside];
3974 - (void) onSwitch:(id)sender {
3975 NSMutableDictionary *metadata = [Sections_ objectForKey:section_];
3976 if (metadata == nil) {
3977 metadata = [NSMutableDictionary dictionaryWithCapacity:2];
3978 [Sections_ setObject:metadata forKey:section_];
3982 [metadata setObject:[NSNumber numberWithBool:([switch_ value] == 0)] forKey:@"Hidden"];
3985 - (void) setSection:(Section *)section editing:(BOOL)editing {
3986 if (editing != editing_) {
3988 [switch_ removeFromSuperview];
3990 [self addSubview:switch_];
3994 [self clearSection];
3996 if (section == nil) {
3997 name_ = [@"All Packages" retain];
4000 section_ = [section name];
4001 if (section_ != nil)
4002 section_ = [section_ retain];
4003 name_ = [(section_ == nil ? @"(No Section)" : section_) retain];
4004 count_ = [[NSString stringWithFormat:@"%d", [section count]] retain];
4007 [switch_ setValue:(isSectionVisible(section_) ? 1 : 0) animated:NO];
4011 - (void) drawContentInRect:(CGRect)rect selected:(BOOL)selected {
4012 [icon_ drawInRect:CGRectMake(8, 7, 32, 32)];
4019 [name_ drawAtPoint:CGPointMake(48, 9) forWidth:(editing_ ? 164 : 250) withFont:Font22Bold_ ellipsis:2];
4021 CGSize size = [count_ sizeWithFont:Font14_];
4025 [count_ drawAtPoint:CGPointMake(13 + (29 - size.width) / 2, 16) withFont:Font12Bold_];
4027 [super drawContentInRect:rect selected:selected];
4033 /* File Table {{{ */
4034 @interface FileTable : RVPage {
4035 _transient Database *database_;
4038 NSMutableArray *files_;
4042 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4043 - (void) setPackage:(Package *)package;
4047 @implementation FileTable
4050 if (package_ != nil)
4059 - (int) numberOfRowsInTable:(UITable *)table {
4060 return files_ == nil ? 0 : [files_ count];
4063 - (float) table:(UITable *)table heightForRow:(int)row {
4067 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
4068 if (reusing == nil) {
4069 reusing = [[[UIImageAndTextTableCell alloc] init] autorelease];
4070 UIFont *font = [UIFont systemFontOfSize:16];
4071 [[(UIImageAndTextTableCell *)reusing titleTextLabel] setFont:font];
4073 [(UIImageAndTextTableCell *)reusing setTitle:[files_ objectAtIndex:row]];
4077 - (BOOL) table:(UITable *)table canSelectRow:(int)row {
4081 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4082 if ((self = [super initWithBook:book]) != nil) {
4083 database_ = database;
4085 files_ = [[NSMutableArray arrayWithCapacity:32] retain];
4087 list_ = [[UITable alloc] initWithFrame:[self bounds]];
4088 [self addSubview:list_];
4090 UITableColumn *column = [[[UITableColumn alloc]
4091 initWithTitle:@"Name"
4093 width:[self frame].size.width
4096 [list_ setDataSource:self];
4097 [list_ setSeparatorStyle:1];
4098 [list_ addTableColumn:column];
4099 [list_ setDelegate:self];
4100 [list_ setReusesTableCells:YES];
4104 - (void) setPackage:(Package *)package {
4105 if (package_ != nil) {
4106 [package_ autorelease];
4115 [files_ removeAllObjects];
4117 if (package != nil) {
4118 package_ = [package retain];
4119 name_ = [[package id] retain];
4121 if (NSArray *files = [package files])
4122 [files_ addObjectsFromArray:files];
4124 if ([files_ count] != 0) {
4125 if ([[files_ objectAtIndex:0] isEqualToString:@"/."])
4126 [files_ removeObjectAtIndex:0];
4127 [files_ sortUsingSelector:@selector(compareByPath:)];
4129 NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8];
4130 [stack addObject:@"/"];
4132 for (int i(0), e([files_ count]); i != e; ++i) {
4133 NSString *file = [files_ objectAtIndex:i];
4134 while (![file hasPrefix:[stack lastObject]])
4135 [stack removeLastObject];
4136 NSString *directory = [stack lastObject];
4137 [stack addObject:[file stringByAppendingString:@"/"]];
4138 [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@",
4139 ([stack count] - 2) * 3, "",
4140 [file substringFromIndex:[directory length]]
4149 - (void) resetViewAnimated:(BOOL)animated {
4150 [list_ resetViewAnimated:animated];
4153 - (void) reloadData {
4154 [self setPackage:[database_ packageWithName:name_]];
4155 [self reloadButtons];
4158 - (NSString *) title {
4159 return @"Installed Files";
4162 - (NSString *) backButtonTitle {
4168 /* Package View {{{ */
4169 @interface PackageView : BrowserView {
4170 _transient Database *database_;
4174 NSMutableArray *buttons_;
4177 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4178 - (void) setPackage:(Package *)package;
4182 @implementation PackageView
4185 if (package_ != nil)
4193 - (void) _clickButtonWithName:(NSString *)name {
4194 if ([name isEqualToString:@"Install"])
4195 [delegate_ installPackage:package_];
4196 else if ([name isEqualToString:@"Reinstall"])
4197 [delegate_ installPackage:package_];
4198 else if ([name isEqualToString:@"Remove"])
4199 [delegate_ removePackage:package_];
4200 else if ([name isEqualToString:@"Upgrade"])
4201 [delegate_ installPackage:package_];
4202 else _assert(false);
4205 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
4206 NSString *context([sheet context]);
4208 if ([context isEqualToString:@"modify"]) {
4209 int count = [buttons_ count];
4210 _assert(count != 0);
4211 _assert(button <= count + 1);
4213 if (count != button - 1)
4214 [self _clickButtonWithName:[buttons_ objectAtIndex:(button - 1)]];
4218 [super alertSheet:sheet buttonClicked:button];
4221 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
4222 return [super webView:sender didFinishLoadForFrame:frame];
4225 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
4226 [window setValue:package_ forKey:@"package"];
4227 [super webView:sender didClearWindowObject:window forFrame:frame];
4230 - (bool) _allowJavaScriptPanel {
4235 - (void) _rightButtonClicked {
4236 /*[super _rightButtonClicked];
4239 int count = [buttons_ count];
4240 _assert(count != 0);
4243 [self _clickButtonWithName:[buttons_ objectAtIndex:0]];
4245 NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:(count + 1)];
4246 [buttons addObjectsFromArray:buttons_];
4247 [buttons addObject:@"Cancel"];
4249 [delegate_ slideUp:[[[UIActionSheet alloc]
4252 defaultButtonIndex:2
4260 - (id) _rightButtonTitle {
4261 int count = [buttons_ count];
4262 return count == 0 ? nil : count != 1 ? @"Modify" : [buttons_ objectAtIndex:0];
4265 - (NSString *) backButtonTitle {
4269 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4270 if ((self = [super initWithBook:book]) != nil) {
4271 database_ = database;
4272 buttons_ = [[NSMutableArray alloc] initWithCapacity:4];
4276 - (void) setPackage:(Package *)package {
4277 if (package_ != nil) {
4278 [package_ autorelease];
4287 [buttons_ removeAllObjects];
4289 if (package != nil) {
4290 package_ = [package retain];
4291 name_ = [[package id] retain];
4292 commercial_ = [package isCommercial];
4294 [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"package" ofType:@"html"]]];
4296 if ([package_ source] == nil);
4297 else if ([package_ upgradableAndEssential:NO])
4298 [buttons_ addObject:@"Upgrade"];
4299 else if ([package_ installed] == nil)
4300 [buttons_ addObject:@"Install"];
4302 [buttons_ addObject:@"Reinstall"];
4303 if ([package_ installed] != nil)
4304 [buttons_ addObject:@"Remove"];
4308 - (bool) isLoading {
4309 return commercial_ ? [super isLoading] : false;
4312 - (void) reloadData {
4313 [self setPackage:[database_ packageWithName:name_]];
4314 [self reloadButtons];
4319 /* Package Table {{{ */
4320 @interface PackageTable : RVPage {
4321 _transient Database *database_;
4323 NSMutableArray *packages_;
4324 NSMutableArray *sections_;
4325 UISectionList *list_;
4328 - (id) initWithBook:(RVBook *)book database:(Database *)database title:(NSString *)title;
4330 - (void) setDelegate:(id)delegate;
4332 - (void) reloadData;
4333 - (void) resetCursor;
4335 - (UISectionList *) list;
4337 - (void) setShouldHideHeaderInShortLists:(BOOL)hide;
4341 @implementation PackageTable
4344 [list_ setDataSource:nil];
4347 [packages_ release];
4348 [sections_ release];
4353 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
4354 return [sections_ count];
4357 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
4358 return [[sections_ objectAtIndex:section] name];
4361 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
4362 return [[sections_ objectAtIndex:section] row];
4365 - (int) numberOfRowsInTable:(UITable *)table {
4366 return [packages_ count];
4369 - (float) table:(UITable *)table heightForRow:(int)row {
4370 return [PackageCell heightForPackage:[packages_ objectAtIndex:row]];
4373 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
4375 reusing = [[[PackageCell alloc] init] autorelease];
4376 [(PackageCell *)reusing setPackage:[packages_ objectAtIndex:row]];
4380 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
4384 - (void) tableRowSelected:(NSNotification *)notification {
4385 int row = [[notification object] selectedRow];
4389 Package *package = [packages_ objectAtIndex:row];
4390 package = [database_ packageWithName:[package id]];
4391 PackageView *view = [[[PackageView alloc] initWithBook:book_ database:database_] autorelease];
4392 [view setPackage:package];
4393 [view setDelegate:delegate_];
4394 [book_ pushPage:view];
4397 - (id) initWithBook:(RVBook *)book database:(Database *)database title:(NSString *)title {
4398 if ((self = [super initWithBook:book]) != nil) {
4399 database_ = database;
4400 title_ = [title retain];
4402 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
4403 sections_ = [[NSMutableArray arrayWithCapacity:16] retain];
4405 list_ = [[UISectionList alloc] initWithFrame:[self bounds] showSectionIndex:YES];
4406 [list_ setDataSource:self];
4408 UITableColumn *column = [[[UITableColumn alloc]
4409 initWithTitle:@"Name"
4411 width:[self frame].size.width
4414 UITable *table = [list_ table];
4415 [table setSeparatorStyle:1];
4416 [table addTableColumn:column];
4417 [table setDelegate:self];
4418 [table setReusesTableCells:YES];
4420 [self addSubview:list_];
4422 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
4423 [list_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
4427 - (void) setDelegate:(id)delegate {
4428 delegate_ = delegate;
4431 - (bool) hasPackage:(Package *)package {
4435 - (void) reloadData {
4436 NSArray *packages = [database_ packages];
4438 [packages_ removeAllObjects];
4439 [sections_ removeAllObjects];
4441 _profile(PackageTable$reloadData$Filter)
4442 for (Package *package in packages)
4443 if ([self hasPackage:package])
4444 [packages_ addObject:package];
4447 Section *section = nil;
4449 _profile(PackageTable$reloadData$Section)
4450 for (size_t offset(0), end([packages_ count]); offset != end; ++offset) {
4454 _profile(PackageTable$reloadData$Section$Package)
4455 package = [packages_ objectAtIndex:offset];
4456 index = [package index];
4459 if (section == nil || [section index] != index) {
4460 _profile(PackageTable$reloadData$Section$Allocate)
4461 section = [[[Section alloc] initWithIndex:index row:offset] autorelease];
4464 _profile(PackageTable$reloadData$Section$Add)
4465 [sections_ addObject:section];
4469 [section addToCount];
4473 _profile(PackageTable$reloadData$List)
4478 - (NSString *) title {
4482 - (void) resetViewAnimated:(BOOL)animated {
4483 [list_ resetViewAnimated:animated];
4486 - (void) resetCursor {
4487 [[list_ table] scrollPointVisibleAtTopLeft:CGPointMake(0, 0) animated:NO];
4490 - (UISectionList *) list {
4494 - (void) setShouldHideHeaderInShortLists:(BOOL)hide {
4495 [list_ setShouldHideHeaderInShortLists:hide];
4500 /* Filtered Package Table {{{ */
4501 @interface FilteredPackageTable : PackageTable {
4507 - (void) setObject:(id)object;
4509 - (id) initWithBook:(RVBook *)book database:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object;
4513 @implementation FilteredPackageTable
4521 - (void) setObject:(id)object {
4527 object_ = [object retain];
4530 - (bool) hasPackage:(Package *)package {
4531 _profile(FilteredPackageTable$hasPackage)
4532 return [package valid] && (*reinterpret_cast<bool (*)(id, SEL, id)>(imp_))(package, filter_, object_);
4536 - (id) initWithBook:(RVBook *)book database:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object {
4537 if ((self = [super initWithBook:book database:database title:title]) != nil) {
4539 object_ = object == nil ? nil : [object retain];
4541 /* XXX: this is an unsafe optimization of doomy hell */
4542 Method method = class_getInstanceMethod([Package class], filter);
4543 imp_ = method_getImplementation(method);
4544 _assert(imp_ != NULL);
4553 /* Add Source View {{{ */
4554 @interface AddSourceView : RVPage {
4555 _transient Database *database_;
4558 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4562 @implementation AddSourceView
4564 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4565 if ((self = [super initWithBook:book]) != nil) {
4566 database_ = database;
4572 /* Source Cell {{{ */
4573 @interface SourceCell : UITableCell {
4576 NSString *description_;
4582 - (SourceCell *) initWithSource:(Source *)source;
4586 @implementation SourceCell
4591 [description_ release];
4596 - (SourceCell *) initWithSource:(Source *)source {
4597 if ((self = [super init]) != nil) {
4599 icon_ = [UIImage applicationImageNamed:[NSString stringWithFormat:@"Sources/%@.png", [source host]]];
4601 icon_ = [UIImage applicationImageNamed:@"unknown.png"];
4602 icon_ = [icon_ retain];
4604 origin_ = [[source name] retain];
4605 label_ = [[source uri] retain];
4606 description_ = [[source description] retain];
4610 - (void) drawContentInRect:(CGRect)rect selected:(BOOL)selected {
4612 [icon_ drawInRect:CGRectMake(10, 10, 30, 30)];
4619 [origin_ drawAtPoint:CGPointMake(48, 8) forWidth:240 withFont:Font18Bold_ ellipsis:2];
4623 [label_ drawAtPoint:CGPointMake(58, 29) forWidth:225 withFont:Font12_ ellipsis:2];
4627 [description_ drawAtPoint:CGPointMake(12, 46) forWidth:280 withFont:Font14_ ellipsis:2];
4629 [super drawContentInRect:rect selected:selected];
4634 /* Source Table {{{ */
4635 @interface SourceTable : RVPage {
4636 _transient Database *database_;
4637 UISectionList *list_;
4638 NSMutableArray *sources_;
4639 UIActionSheet *alert_;
4643 UIProgressHUD *hud_;
4646 //NSURLConnection *installer_;
4647 NSURLConnection *trivial_bz2_;
4648 NSURLConnection *trivial_gz_;
4649 //NSURLConnection *automatic_;
4654 - (id) initWithBook:(RVBook *)book database:(Database *)database;
4658 @implementation SourceTable
4660 - (void) _deallocConnection:(NSURLConnection *)connection {
4661 if (connection != nil) {
4662 [connection cancel];
4663 //[connection setDelegate:nil];
4664 [connection release];
4669 [[list_ table] setDelegate:nil];
4670 [list_ setDataSource:nil];
4679 //[self _deallocConnection:installer_];
4680 [self _deallocConnection:trivial_gz_];
4681 [self _deallocConnection:trivial_bz2_];
4682 //[self _deallocConnection:automatic_];
4689 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
4690 return offset_ == 0 ? 1 : 2;
4693 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
4694 switch (section + (offset_ == 0 ? 1 : 0)) {
4695 case 0: return @"Entered by User";
4696 case 1: return @"Installed by Packages";
4704 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
4705 switch (section + (offset_ == 0 ? 1 : 0)) {
4707 case 1: return offset_;
4715 - (int) numberOfRowsInTable:(UITable *)table {
4716 return [sources_ count];
4719 - (float) table:(UITable *)table heightForRow:(int)row {
4720 Source *source = [sources_ objectAtIndex:row];
4721 return [source description] == nil ? 56 : 73;
4724 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col {
4725 Source *source = [sources_ objectAtIndex:row];
4726 // XXX: weird warning, stupid selectors ;P
4727 return [[[SourceCell alloc] initWithSource:(id)source] autorelease];
4730 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
4734 - (BOOL) table:(UITable *)table canSelectRow:(int)row {
4738 - (void) tableRowSelected:(NSNotification*)notification {
4739 UITable *table([list_ table]);
4740 int row([table selectedRow]);
4744 Source *source = [sources_ objectAtIndex:row];
4746 PackageTable *packages = [[[FilteredPackageTable alloc]
4749 title:[source label]
4750 filter:@selector(isVisibleInSource:)
4754 [packages setDelegate:delegate_];
4756 [book_ pushPage:packages];
4759 - (BOOL) table:(UITable *)table canDeleteRow:(int)row {
4760 Source *source = [sources_ objectAtIndex:row];
4761 return [source record] != nil;
4764 - (void) table:(UITable *)table willSwipeToDeleteRow:(int)row {
4765 [[list_ table] setDeleteConfirmationRow:row];
4768 - (void) table:(UITable *)table deleteRow:(int)row {
4769 Source *source = [sources_ objectAtIndex:row];
4770 [Sources_ removeObjectForKey:[source key]];
4771 [delegate_ syncData];
4775 [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys:
4778 @"./", @"Distribution",
4779 nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href_]];
4781 [delegate_ syncData];
4784 - (NSString *) getWarning {
4785 NSString *href(href_);
4786 NSRange colon([href rangeOfString:@"://"]);
4787 if (colon.location != NSNotFound)
4788 href = [href substringFromIndex:(colon.location + 3)];
4789 href = [href stringByAddingPercentEscapes];
4790 href = [@"http://cydia.saurik.com/api/repotag/" stringByAppendingString:href];
4791 href = [href stringByCachingURLWithCurrentCDN];
4793 NSURL *url([NSURL URLWithString:href]);
4795 NSStringEncoding encoding;
4796 NSError *error(nil);
4798 if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error])
4799 return [warning length] == 0 ? nil : warning;
4803 - (void) _endConnection:(NSURLConnection *)connection {
4804 NSURLConnection **field = NULL;
4805 if (connection == trivial_bz2_)
4806 field = &trivial_bz2_;
4807 else if (connection == trivial_gz_)
4808 field = &trivial_gz_;
4809 _assert(field != NULL);
4810 [connection release];
4814 trivial_bz2_ == nil &&
4820 if (NSString *warning = [self yieldToSelector:@selector(getWarning)]) {
4823 UIActionSheet *sheet = [[[UIActionSheet alloc]
4824 initWithTitle:@"Source Warning"
4825 buttons:[NSArray arrayWithObjects:@"Add Anyway", @"Cancel", nil]
4826 defaultButtonIndex:0
4831 [sheet setNumberOfRows:1];
4833 [sheet setBodyText:warning];
4834 [sheet popupAlertAnimated:YES];
4837 } else if (error_ != nil) {
4838 UIActionSheet *sheet = [[[UIActionSheet alloc]
4839 initWithTitle:@"Verification Error"
4840 buttons:[NSArray arrayWithObjects:@"OK", nil]
4841 defaultButtonIndex:0
4846 [sheet setBodyText:[error_ localizedDescription]];
4847 [sheet popupAlertAnimated:YES];
4849 UIActionSheet *sheet = [[[UIActionSheet alloc]
4850 initWithTitle:@"Did not Find Repository"
4851 buttons:[NSArray arrayWithObjects:@"OK", nil]
4852 defaultButtonIndex:0
4857 [sheet setBodyText:@"The indicated repository could not be found. This could be because you are trying to add a legacy Installer repository (these are not supported). Also, this interface is only capable of working with exact repository URLs. If you host a repository and are having issues please contact the author of Cydia with any questions you have."];
4858 [sheet popupAlertAnimated:YES];
4861 [delegate_ setStatusBarShowsProgress:NO];
4862 [delegate_ removeProgressHUD:hud_];
4872 if (error_ != nil) {
4879 - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
4880 switch ([response statusCode]) {
4886 - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
4887 lprintf("connection:\"%s\" didFailWithError:\"%s\"", [href_ UTF8String], [[error localizedDescription] UTF8String]);
4889 error_ = [error retain];
4890 [self _endConnection:connection];
4893 - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
4894 [self _endConnection:connection];
4897 - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method {
4898 NSMutableURLRequest *request = [NSMutableURLRequest
4899 requestWithURL:[NSURL URLWithString:href]
4900 cachePolicy:NSURLRequestUseProtocolCachePolicy
4901 timeoutInterval:20.0
4904 [request setHTTPMethod:method];
4906 return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
4909 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
4910 NSString *context([sheet context]);
4912 if ([context isEqualToString:@"source"]) {
4915 NSString *href = [[sheet textField] text];
4917 //installer_ = [[self _requestHRef:href method:@"GET"] retain];
4919 if (![href hasSuffix:@"/"])
4920 href_ = [href stringByAppendingString:@"/"];
4923 href_ = [href_ retain];
4925 trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain];
4926 trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain];
4927 //trivial_bz2_ = [[self _requestHRef:[href stringByAppendingString:@"dists/Release"] method:@"HEAD"] retain];
4931 hud_ = [[delegate_ addProgressHUD] retain];
4932 [hud_ setText:@"Verifying URL"];
4943 } else if ([context isEqualToString:@"trivial"])
4945 else if ([context isEqualToString:@"urlerror"])
4947 else if ([context isEqualToString:@"warning"]) {
4967 - (id) initWithBook:(RVBook *)book database:(Database *)database {
4968 if ((self = [super initWithBook:book]) != nil) {
4969 database_ = database;
4970 sources_ = [[NSMutableArray arrayWithCapacity:16] retain];
4972 //list_ = [[UITable alloc] initWithFrame:[self bounds]];
4973 list_ = [[UISectionList alloc] initWithFrame:[self bounds] showSectionIndex:NO];
4974 [list_ setShouldHideHeaderInShortLists:NO];
4976 [self addSubview:list_];
4977 [list_ setDataSource:self];
4979 UITableColumn *column = [[UITableColumn alloc]
4980 initWithTitle:@"Name"
4982 width:[self frame].size.width
4985 UITable *table = [list_ table];
4986 [table setSeparatorStyle:1];
4987 [table addTableColumn:column];
4988 [table setDelegate:self];
4992 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
4993 [list_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
4997 - (void) reloadData {
4999 _assert(list.ReadMainList());
5001 [sources_ removeAllObjects];
5002 [sources_ addObjectsFromArray:[database_ sources]];
5004 [sources_ sortUsingSelector:@selector(compareByNameAndType:)];
5007 int count = [sources_ count];
5008 for (offset_ = 0; offset_ != count; ++offset_) {
5009 Source *source = [sources_ objectAtIndex:offset_];
5010 if ([source record] == nil)
5017 - (void) resetViewAnimated:(BOOL)animated {
5018 [list_ resetViewAnimated:animated];
5021 - (void) _leftButtonClicked {
5022 /*[book_ pushPage:[[[AddSourceView alloc]
5027 UIActionSheet *sheet = [[[UIActionSheet alloc]
5028 initWithTitle:@"Enter Cydia/APT URL"
5029 buttons:[NSArray arrayWithObjects:@"Add Source", @"Cancel", nil]
5030 defaultButtonIndex:0
5035 [sheet setNumberOfRows:1];
5037 [sheet addTextFieldWithValue:@"http://" label:@""];
5039 UITextInputTraits *traits = [[sheet textField] textInputTraits];
5040 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
5041 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
5042 [traits setKeyboardType:UIKeyboardTypeURL];
5043 // XXX: UIReturnKeyDone
5044 [traits setReturnKeyType:UIReturnKeyNext];
5046 [sheet popupAlertAnimated:YES];
5049 - (void) _rightButtonClicked {
5050 UITable *table = [list_ table];
5051 BOOL editing = [table isRowDeletionEnabled];
5052 [table enableRowDeletion:!editing animated:YES];
5053 [book_ reloadButtonsForPage:self];
5056 - (NSString *) title {
5060 - (NSString *) leftButtonTitle {
5061 return [[list_ table] isRowDeletionEnabled] ? @"Add" : nil;
5064 - (id) rightButtonTitle {
5065 return [[list_ table] isRowDeletionEnabled] ? @"Done" : @"Edit";
5068 - (UINavigationButtonStyle) rightButtonStyle {
5069 return [[list_ table] isRowDeletionEnabled] ? UINavigationButtonStyleHighlighted : UINavigationButtonStyleNormal;
5075 /* Installed View {{{ */
5076 @interface InstalledView : RVPage {
5077 _transient Database *database_;
5078 FilteredPackageTable *packages_;
5082 - (id) initWithBook:(RVBook *)book database:(Database *)database;
5086 @implementation InstalledView
5089 [packages_ release];
5093 - (id) initWithBook:(RVBook *)book database:(Database *)database {
5094 if ((self = [super initWithBook:book]) != nil) {
5095 database_ = database;
5097 packages_ = [[FilteredPackageTable alloc]
5101 filter:@selector(isInstalledAndVisible:)
5102 with:[NSNumber numberWithBool:YES]
5105 [self addSubview:packages_];
5107 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
5108 [packages_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
5112 - (void) resetViewAnimated:(BOOL)animated {
5113 [packages_ resetViewAnimated:animated];
5116 - (void) reloadData {
5117 [packages_ reloadData];
5120 - (void) _rightButtonClicked {
5121 [packages_ setObject:[NSNumber numberWithBool:expert_]];
5122 [packages_ reloadData];
5124 [book_ reloadButtonsForPage:self];
5127 - (NSString *) title {
5128 return @"Installed";
5131 - (NSString *) backButtonTitle {
5135 - (id) rightButtonTitle {
5136 return Role_ != nil && [Role_ isEqualToString:@"Developer"] ? nil : expert_ ? @"Expert" : @"Simple";
5139 - (UINavigationButtonStyle) rightButtonStyle {
5140 return expert_ ? UINavigationButtonStyleHighlighted : UINavigationButtonStyleNormal;
5143 - (void) setDelegate:(id)delegate {
5144 [super setDelegate:delegate];
5145 [packages_ setDelegate:delegate];
5152 @interface HomeView : BrowserView {
5157 @implementation HomeView
5159 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
5160 NSString *context([sheet context]);
5162 if ([context isEqualToString:@"about"])
5165 [super alertSheet:sheet buttonClicked:button];
5168 - (void) _leftButtonClicked {
5169 UIActionSheet *sheet = [[[UIActionSheet alloc]
5170 initWithTitle:@"About Cydia Installer"
5171 buttons:[NSArray arrayWithObjects:@"Close", nil]
5172 defaultButtonIndex:0
5178 @"Copyright (C) 2008-2009\n"
5179 "Jay Freeman (saurik)\n"
5180 "saurik@saurik.com\n"
5181 "http://www.saurik.com/\n"
5184 "http://www.theokorigroup.com/\n"
5186 "College of Creative Studies,\n"
5187 "University of California,\n"
5189 "http://www.ccs.ucsb.edu/"
5192 [sheet popupAlertAnimated:YES];
5195 - (NSString *) leftButtonTitle {
5201 /* Manage View {{{ */
5202 @interface ManageView : BrowserView {
5207 @implementation ManageView
5209 - (NSString *) title {
5213 - (void) _leftButtonClicked {
5214 [delegate_ askForSettings];
5217 - (NSString *) leftButtonTitle {
5222 - (id) _rightButtonTitle {
5227 - (bool) isLoading {
5234 #include <BrowserView.m>
5236 /* Cydia Book {{{ */
5237 @interface CYBook : RVBook <
5240 _transient Database *database_;
5241 UINavigationBar *overlay_;
5242 UINavigationBar *underlay_;
5243 UIProgressIndicator *indicator_;
5244 UITextLabel *prompt_;
5245 UIProgressBar *progress_;
5246 UINavigationButton *cancel_;
5250 - (id) initWithFrame:(CGRect)frame database:(Database *)database;
5256 @implementation CYBook
5260 [indicator_ release];
5262 [progress_ release];
5267 - (NSString *) getTitleForPage:(RVPage *)page {
5268 return Simplify([super getTitleForPage:page]);
5276 [UIView beginAnimations:nil context:NULL];
5278 CGRect ovrframe = [overlay_ frame];
5279 ovrframe.origin.y = 0;
5280 [overlay_ setFrame:ovrframe];
5282 CGRect barframe = [navbar_ frame];
5283 barframe.origin.y += ovrframe.size.height;
5284 [navbar_ setFrame:barframe];
5286 CGRect trnframe = [transition_ frame];
5287 trnframe.origin.y += ovrframe.size.height;
5288 trnframe.size.height -= ovrframe.size.height;
5289 [transition_ setFrame:trnframe];
5291 [UIView endAnimations];
5293 [indicator_ startAnimation];
5294 [prompt_ setText:@"Updating Database"];
5295 [progress_ setProgress:0];
5298 [overlay_ addSubview:cancel_];
5301 detachNewThreadSelector:@selector(_update)
5310 [indicator_ stopAnimation];
5312 [UIView beginAnimations:nil context:NULL];
5314 CGRect ovrframe = [overlay_ frame];
5315 ovrframe.origin.y = -ovrframe.size.height;
5316 [overlay_ setFrame:ovrframe];
5318 CGRect barframe = [navbar_ frame];
5319 barframe.origin.y -= ovrframe.size.height;
5320 [navbar_ setFrame:barframe];
5322 CGRect trnframe = [transition_ frame];
5323 trnframe.origin.y -= ovrframe.size.height;
5324 trnframe.size.height += ovrframe.size.height;
5325 [transition_ setFrame:trnframe];
5327 [UIView commitAnimations];
5329 [delegate_ performSelector:@selector(reloadData) withObject:nil afterDelay:0];
5332 - (id) initWithFrame:(CGRect)frame database:(Database *)database {
5333 if ((self = [super initWithFrame:frame]) != nil) {
5334 database_ = database;
5336 CGRect ovrrect = [navbar_ bounds];
5337 ovrrect.size.height = [UINavigationBar defaultSize].height;
5338 ovrrect.origin.y = -ovrrect.size.height;
5340 overlay_ = [[UINavigationBar alloc] initWithFrame:ovrrect];
5341 [self addSubview:overlay_];
5343 ovrrect.origin.y = frame.size.height;
5344 underlay_ = [[UINavigationBar alloc] initWithFrame:ovrrect];
5345 [underlay_ setTintColor:[UIColor colorWithRed:0.23 green:0.23 blue:0.23 alpha:1]];
5346 [self addSubview:underlay_];
5348 [overlay_ setBarStyle:1];
5349 [underlay_ setBarStyle:1];
5351 int barstyle = [overlay_ _barStyle:NO];
5352 bool ugly = barstyle == 0;
5354 UIProgressIndicatorStyle style = ugly ?
5355 UIProgressIndicatorStyleMediumBrown :
5356 UIProgressIndicatorStyleMediumWhite;
5358 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:style];
5359 unsigned indoffset = (ovrrect.size.height - indsize.height) / 2;
5360 CGRect indrect = {{indoffset, indoffset}, indsize};
5362 indicator_ = [[UIProgressIndicator alloc] initWithFrame:indrect];
5363 [indicator_ setStyle:style];
5364 [overlay_ addSubview:indicator_];
5366 CGSize prmsize = {215, indsize.height + 4};
5369 indoffset * 2 + indsize.width,
5373 unsigned(ovrrect.size.height - prmsize.height) / 2
5376 UIFont *font = [UIFont systemFontOfSize:15];
5378 prompt_ = [[UITextLabel alloc] initWithFrame:prmrect];
5380 [prompt_ setColor:[UIColor colorWithCGColor:(ugly ? Blueish_ : Off_)]];
5381 [prompt_ setBackgroundColor:[UIColor clearColor]];
5382 [prompt_ setFont:font];
5384 [overlay_ addSubview:prompt_];
5386 CGSize prgsize = {75, 100};
5389 ovrrect.size.width - prgsize.width - 10,
5390 (ovrrect.size.height - prgsize.height) / 2
5393 progress_ = [[UIProgressBar alloc] initWithFrame:prgrect];
5394 [progress_ setStyle:0];
5395 [overlay_ addSubview:progress_];
5397 cancel_ = [[UINavigationButton alloc] initWithTitle:@"Cancel" style:UINavigationButtonStyleHighlighted];
5398 [cancel_ addTarget:self action:@selector(_onCancel) forControlEvents:UIControlEventTouchUpInside];
5400 CGRect frame = [cancel_ frame];
5401 frame.size.width = 65;
5402 frame.origin.x = ovrrect.size.width - frame.size.width - 5;
5403 frame.origin.y = (ovrrect.size.height - frame.size.height) / 2;
5404 [cancel_ setFrame:frame];
5406 [cancel_ setBarStyle:barstyle];
5410 - (void) _onCancel {
5412 [cancel_ removeFromSuperview];
5415 - (void) _update { _pooled
5417 status.setDelegate(self);
5419 [database_ updateWithStatus:status];
5422 performSelectorOnMainThread:@selector(_update_)
5428 - (void) setProgressError:(NSString *)error forPackage:(NSString *)id {
5429 [prompt_ setText:[NSString stringWithFormat:@"Error: %@", error]];
5432 - (void) setProgressTitle:(NSString *)title {
5434 performSelectorOnMainThread:@selector(_setProgressTitle:)
5440 - (void) setProgressPercent:(float)percent {
5442 performSelectorOnMainThread:@selector(_setProgressPercent:)
5443 withObject:[NSNumber numberWithFloat:percent]
5448 - (void) startProgress {
5451 - (void) addProgressOutput:(NSString *)output {
5453 performSelectorOnMainThread:@selector(_addProgressOutput:)
5459 - (bool) isCancelling:(size_t)received {
5463 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
5467 - (void) _setProgressTitle:(NSString *)title {
5468 [prompt_ setText:title];
5471 - (void) _setProgressPercent:(NSNumber *)percent {
5472 [progress_ setProgress:[percent floatValue]];
5475 - (void) _addProgressOutput:(NSString *)output {
5480 /* Cydia:// Protocol {{{ */
5481 @interface CydiaURLProtocol : NSURLProtocol {
5486 @implementation CydiaURLProtocol
5488 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
5489 NSURL *url([request URL]);
5492 NSString *scheme([[url scheme] lowercaseString]);
5493 if (scheme == nil || ![scheme isEqualToString:@"cydia"])
5498 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
5502 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
5503 id<NSURLProtocolClient> client([self client]);
5505 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
5507 NSData *data(UIImagePNGRepresentation(icon));
5509 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
5510 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
5511 [client URLProtocol:self didLoadData:data];
5512 [client URLProtocolDidFinishLoading:self];
5516 - (void) startLoading {
5517 id<NSURLProtocolClient> client([self client]);
5518 NSURLRequest *request([self request]);
5520 NSURL *url([request URL]);
5521 NSString *href([url absoluteString]);
5523 NSString *path([href substringFromIndex:8]);
5524 NSRange slash([path rangeOfString:@"/"]);
5527 if (slash.location == NSNotFound) {
5531 command = [path substringToIndex:slash.location];
5532 path = [path substringFromIndex:(slash.location + 1)];
5535 Database *database([Database sharedInstance]);
5537 if ([command isEqualToString:@"package-icon"]) {
5540 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
5541 Package *package([database packageWithName:path]);
5544 UIImage *icon([package icon]);
5545 [self _returnPNGWithImage:icon forRequest:request];
5546 } else if ([command isEqualToString:@"source-icon"]) {
5549 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
5550 NSString *source(Simplify(path));
5551 UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sources/%@.png", App_, source]]);
5553 icon = [UIImage applicationImageNamed:@"unknown.png"];
5554 [self _returnPNGWithImage:icon forRequest:request];
5555 } else if ([command isEqualToString:@"uikit-image"]) {
5558 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
5559 UIImage *icon(_UIImageWithName(path));
5560 [self _returnPNGWithImage:icon forRequest:request];
5561 } else if ([command isEqualToString:@"section-icon"]) {
5564 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
5565 NSString *section(Simplify(path));
5566 UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]);
5568 icon = [UIImage applicationImageNamed:@"unknown.png"];
5569 [self _returnPNGWithImage:icon forRequest:request];
5571 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
5575 - (void) stopLoading {
5581 /* Sections View {{{ */
5582 @interface SectionsView : RVPage {
5583 _transient Database *database_;
5584 NSMutableArray *sections_;
5585 NSMutableArray *filtered_;
5586 UITransitionView *transition_;
5592 - (id) initWithBook:(RVBook *)book database:(Database *)database;
5593 - (void) reloadData;
5598 @implementation SectionsView
5601 [list_ setDataSource:nil];
5602 [list_ setDelegate:nil];
5604 [sections_ release];
5605 [filtered_ release];
5606 [transition_ release];
5608 [accessory_ release];
5612 - (int) numberOfRowsInTable:(UITable *)table {
5613 return editing_ ? [sections_ count] : [filtered_ count] + 1;
5616 - (float) table:(UITable *)table heightForRow:(int)row {
5620 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
5622 reusing = [[[SectionCell alloc] init] autorelease];
5623 [(SectionCell *)reusing setSection:(editing_ ?
5624 [sections_ objectAtIndex:row] :
5625 (row == 0 ? nil : [filtered_ objectAtIndex:(row - 1)])
5626 ) editing:editing_];
5630 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
5634 - (BOOL) table:(UITable *)table canSelectRow:(int)row {
5638 - (void) tableRowSelected:(NSNotification *)notification {
5639 int row = [[notification object] selectedRow];
5650 title = @"All Packages";
5652 section = [filtered_ objectAtIndex:(row - 1)];
5653 name = [section name];
5659 title = @"(No Section)";
5663 PackageTable *table = [[[FilteredPackageTable alloc]
5667 filter:@selector(isVisiblyUninstalledInSection:)
5671 [table setDelegate:delegate_];
5673 [book_ pushPage:table];
5676 - (id) initWithBook:(RVBook *)book database:(Database *)database {
5677 if ((self = [super initWithBook:book]) != nil) {
5678 database_ = database;
5680 sections_ = [[NSMutableArray arrayWithCapacity:16] retain];
5681 filtered_ = [[NSMutableArray arrayWithCapacity:16] retain];
5683 transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]];
5684 [self addSubview:transition_];
5686 list_ = [[UITable alloc] initWithFrame:[transition_ bounds]];
5687 [transition_ transition:0 toView:list_];
5689 UITableColumn *column = [[[UITableColumn alloc]
5690 initWithTitle:@"Name"
5692 width:[self frame].size.width
5695 [list_ setDataSource:self];
5696 [list_ setSeparatorStyle:1];
5697 [list_ addTableColumn:column];
5698 [list_ setDelegate:self];
5699 [list_ setReusesTableCells:YES];
5703 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
5704 [list_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
5708 - (void) reloadData {
5709 NSArray *packages = [database_ packages];
5711 [sections_ removeAllObjects];
5712 [filtered_ removeAllObjects];
5714 NSMutableArray *filtered = [NSMutableArray arrayWithCapacity:[packages count]];
5715 NSMutableDictionary *sections = [NSMutableDictionary dictionaryWithCapacity:32];
5718 for (Package *package in packages) {
5719 NSString *name([package section]);
5722 Section *section([sections objectForKey:name]);
5723 if (section == nil) {
5724 section = [[[Section alloc] initWithName:name] autorelease];
5725 [sections setObject:section forKey:name];
5729 if ([package valid] && [package installed] == nil && [package visible])
5730 [filtered addObject:package];
5734 [sections_ addObjectsFromArray:[sections allValues]];
5735 [sections_ sortUsingSelector:@selector(compareByName:)];
5738 [filtered sortUsingSelector:@selector(compareBySection:)];
5741 Section *section = nil;
5742 for (Package *package in filtered) {
5743 NSString *name = [package section];
5745 if (section == nil || name != nil && ![[section name] isEqualToString:name]) {
5746 section = name == nil ?
5747 [[[Section alloc] initWithName:nil] autorelease] :
5748 [sections objectForKey:name];
5749 [filtered_ addObject:section];
5752 [section addToCount];
5760 - (void) resetView {
5762 [self _rightButtonClicked];
5765 - (void) resetViewAnimated:(BOOL)animated {
5766 [list_ resetViewAnimated:animated];
5769 - (void) _rightButtonClicked {
5770 if ((editing_ = !editing_))
5773 [delegate_ updateData];
5774 [book_ reloadTitleForPage:self];
5775 [book_ reloadButtonsForPage:self];
5778 - (NSString *) title {
5779 return editing_ ? @"Section Visibility" : @"Install by Section";
5782 - (NSString *) backButtonTitle {
5786 - (id) rightButtonTitle {
5787 return [sections_ count] == 0 ? nil : editing_ ? @"Done" : @"Edit";
5790 - (UINavigationButtonStyle) rightButtonStyle {
5791 return editing_ ? UINavigationButtonStyleHighlighted : UINavigationButtonStyleNormal;
5794 - (UIView *) accessoryView {
5800 /* Changes View {{{ */
5801 @interface ChangesView : RVPage {
5802 _transient Database *database_;
5803 NSMutableArray *packages_;
5804 NSMutableArray *sections_;
5805 UISectionList *list_;
5809 - (id) initWithBook:(RVBook *)book database:(Database *)database;
5810 - (void) reloadData;
5814 @implementation ChangesView
5817 [[list_ table] setDelegate:nil];
5818 [list_ setDataSource:nil];
5820 [packages_ release];
5821 [sections_ release];
5826 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
5827 return [sections_ count];
5830 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
5831 return [[sections_ objectAtIndex:section] name];
5834 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
5835 return [[sections_ objectAtIndex:section] row];
5838 - (int) numberOfRowsInTable:(UITable *)table {
5839 return [packages_ count];
5842 - (float) table:(UITable *)table heightForRow:(int)row {
5843 return [PackageCell heightForPackage:[packages_ objectAtIndex:row]];
5846 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing {
5848 reusing = [[[PackageCell alloc] init] autorelease];
5849 [(PackageCell *)reusing setPackage:[packages_ objectAtIndex:row]];
5853 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
5857 - (void) tableRowSelected:(NSNotification *)notification {
5858 int row = [[notification object] selectedRow];
5861 Package *package = [packages_ objectAtIndex:row];
5862 PackageView *view = [[[PackageView alloc] initWithBook:book_ database:database_] autorelease];
5863 [view setDelegate:delegate_];
5864 [view setPackage:package];
5865 [book_ pushPage:view];
5868 - (void) _leftButtonClicked {
5869 [(CYBook *)book_ update];
5870 [self reloadButtons];
5873 - (void) _rightButtonClicked {
5874 [delegate_ distUpgrade];
5877 - (id) initWithBook:(RVBook *)book database:(Database *)database {
5878 if ((self = [super initWithBook:book]) != nil) {
5879 database_ = database;
5881 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
5882 sections_ = [[NSMutableArray arrayWithCapacity:16] retain];
5884 list_ = [[UISectionList alloc] initWithFrame:[self bounds] showSectionIndex:NO];
5885 [self addSubview:list_];
5887 [list_ setShouldHideHeaderInShortLists:NO];
5888 [list_ setDataSource:self];
5889 //[list_ setSectionListStyle:1];
5891 UITableColumn *column = [[[UITableColumn alloc]
5892 initWithTitle:@"Name"
5894 width:[self frame].size.width
5897 UITable *table = [list_ table];
5898 [table setSeparatorStyle:1];
5899 [table addTableColumn:column];
5900 [table setDelegate:self];
5901 [table setReusesTableCells:YES];
5905 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
5906 [list_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
5910 - (void) reloadData {
5911 NSArray *packages = [database_ packages];
5913 [packages_ removeAllObjects];
5914 [sections_ removeAllObjects];
5917 for (Package *package in packages)
5919 [package installed] == nil && [package valid] && [package visible] ||
5920 [package upgradableAndEssential:YES]
5922 [packages_ addObject:package];
5925 [packages_ radixSortUsingSelector:@selector(compareForChanges) withObject:nil];
5928 Section *upgradable = [[[Section alloc] initWithName:@"Available Upgrades"] autorelease];
5929 Section *ignored = [[[Section alloc] initWithName:@"Ignored Upgrades"] autorelease];
5930 Section *section = nil;
5934 bool unseens = false;
5936 CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle);
5939 for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) {
5940 Package *package = [packages_ objectAtIndex:offset];
5942 if (![package upgradableAndEssential:YES]) {
5944 NSDate *seen = [package seen];
5946 if (section == nil || last != seen && (seen == nil || [seen compare:last] != NSOrderedSame)) {
5949 NSString *name(seen == nil ? [@"n/a ?" retain] : (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) seen));
5950 section = [[[Section alloc] initWithName:name row:offset] autorelease];
5951 [sections_ addObject:section];
5955 [section addToCount];
5956 } else if ([package ignored])
5957 [ignored addToCount];
5960 [upgradable addToCount];
5965 CFRelease(formatter);
5968 Section *last = [sections_ lastObject];
5969 size_t count = [last count];
5970 [packages_ removeObjectsInRange:NSMakeRange([packages_ count] - count, count)];
5971 [sections_ removeLastObject];
5974 if ([ignored count] != 0)
5975 [sections_ insertObject:ignored atIndex:0];
5977 [sections_ insertObject:upgradable atIndex:0];
5980 [self reloadButtons];
5983 - (void) resetViewAnimated:(BOOL)animated {
5984 [list_ resetViewAnimated:animated];
5987 - (NSString *) leftButtonTitle {
5988 return [(CYBook *)book_ updating] ? nil : @"Refresh";
5991 - (id) rightButtonTitle {
5992 return upgrades_ == 0 ? nil : [NSString stringWithFormat:@"Upgrade (%u)", upgrades_];
5995 - (NSString *) title {
6001 /* Search View {{{ */
6002 @protocol SearchViewDelegate
6003 - (void) showKeyboard:(BOOL)show;
6006 @interface SearchView : RVPage {
6008 UISearchField *field_;
6009 UITransitionView *transition_;
6010 FilteredPackageTable *table_;
6011 UIPreferencesTable *advanced_;
6017 - (id) initWithBook:(RVBook *)book database:(Database *)database;
6018 - (void) reloadData;
6022 @implementation SearchView
6025 [field_ setDelegate:nil];
6027 [accessory_ release];
6029 [transition_ release];
6031 [advanced_ release];
6036 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
6040 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group {
6042 case 0: return @"Advanced Search (Coming Soon!)";
6044 default: _assert(false);
6048 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
6052 default: _assert(false);
6056 - (void) _showKeyboard:(BOOL)show {
6057 CGSize keysize = [UIKeyboard defaultSize];
6058 CGRect keydown = [book_ pageBounds];
6059 CGRect keyup = keydown;
6060 keyup.size.height -= keysize.height - ButtonBarHeight_;
6062 float delay = KeyboardTime_ * ButtonBarHeight_ / keysize.height;
6064 UIFrameAnimation *animation = [[[UIFrameAnimation alloc] initWithTarget:[table_ list]] autorelease];
6065 [animation setSignificantRectFields:8];
6068 [animation setStartFrame:keydown];
6069 [animation setEndFrame:keyup];
6071 [animation setStartFrame:keyup];
6072 [animation setEndFrame:keydown];
6075 UIAnimator *animator = [UIAnimator sharedAnimator];
6078 addAnimations:[NSArray arrayWithObjects:animation, nil]
6079 withDuration:(KeyboardTime_ - delay)
6084 [animator performSelector:@selector(startAnimation:) withObject:animation afterDelay:delay];
6086 [delegate_ showKeyboard:show];
6089 - (void) textFieldDidBecomeFirstResponder:(UITextField *)field {
6090 [self _showKeyboard:YES];
6093 - (void) textFieldDidResignFirstResponder:(UITextField *)field {
6094 [self _showKeyboard:NO];
6097 - (void) keyboardInputChanged:(UIFieldEditor *)editor {
6099 NSString *text([field_ text]);
6100 [field_ setClearButtonStyle:(text == nil || [text length] == 0 ? 0 : 2)];
6106 - (void) textFieldClearButtonPressed:(UITextField *)field {
6110 - (void) keyboardInputShouldDelete:(id)input {
6114 - (BOOL) keyboardInput:(id)input shouldInsertText:(NSString *)text isMarkedText:(int)marked {
6115 if ([text length] != 1 || [text characterAtIndex:0] != '\n') {
6119 [field_ resignFirstResponder];
6124 - (id) initWithBook:(RVBook *)book database:(Database *)database {
6125 if ((self = [super initWithBook:book]) != nil) {
6126 CGRect pageBounds = [book_ pageBounds];
6128 transition_ = [[UITransitionView alloc] initWithFrame:pageBounds];
6129 [self addSubview:transition_];
6131 advanced_ = [[UIPreferencesTable alloc] initWithFrame:pageBounds];
6133 [advanced_ setReusesTableCells:YES];
6134 [advanced_ setDataSource:self];
6135 [advanced_ reloadData];
6137 dimmed_ = [[UIView alloc] initWithFrame:pageBounds];
6138 CGColor dimmed(space_, 0, 0, 0, 0.5);
6139 [dimmed_ setBackgroundColor:[UIColor colorWithCGColor:dimmed]];
6141 table_ = [[FilteredPackageTable alloc]
6145 filter:@selector(isUnfilteredAndSearchedForBy:)
6149 [table_ setShouldHideHeaderInShortLists:NO];
6150 [transition_ transition:0 toView:table_];
6159 area.origin.x = /*cnfrect.origin.x + cnfrect.size.width + 4 +*/ 10;
6166 [self bounds].size.width - area.origin.x - 18;
6168 area.size.height = [UISearchField defaultHeight];
6170 field_ = [[UISearchField alloc] initWithFrame:area];
6172 UIFont *font = [UIFont systemFontOfSize:16];
6173 [field_ setFont:font];
6175 [field_ setPlaceholder:@"Package Names & Descriptions"];
6176 [field_ setDelegate:self];
6178 [field_ setPaddingTop:5];
6180 UITextInputTraits *traits([field_ textInputTraits]);
6181 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
6182 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
6183 [traits setReturnKeyType:UIReturnKeySearch];
6185 CGRect accrect = {{0, 6}, {6 + cnfrect.size.width + 6 + area.size.width + 6, area.size.height}};
6187 accessory_ = [[UIView alloc] initWithFrame:accrect];
6188 [accessory_ addSubview:field_];
6190 /*UIPushButton *configure = [[[UIPushButton alloc] initWithFrame:cnfrect] autorelease];
6191 [configure setShowPressFeedback:YES];
6192 [configure setImage:[UIImage applicationImageNamed:@"advanced.png"]];
6193 [configure addTarget:self action:@selector(configurePushed) forEvents:1];
6194 [accessory_ addSubview:configure];*/
6196 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
6197 [table_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
6203 LKAnimation *animation = [LKTransition animation];
6204 [animation setType:@"oglFlip"];
6205 [animation setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
6206 [animation setFillMode:@"extended"];
6207 [animation setTransitionFlags:3];
6208 [animation setDuration:10];
6209 [animation setSpeed:0.35];
6210 [animation setSubtype:(flipped_ ? @"fromLeft" : @"fromRight")];
6211 [[transition_ _layer] addAnimation:animation forKey:0];
6212 [transition_ transition:0 toView:(flipped_ ? (UIView *) table_ : (UIView *) advanced_)];
6213 flipped_ = !flipped_;
6217 - (void) configurePushed {
6218 [field_ resignFirstResponder];
6222 - (void) resetViewAnimated:(BOOL)animated {
6225 [table_ resetViewAnimated:animated];
6228 - (void) _reloadData {
6231 - (void) reloadData {
6234 [table_ setObject:[field_ text]];
6235 _profile(SearchView$reloadData)
6236 [table_ reloadData];
6239 [table_ resetCursor];
6242 - (UIView *) accessoryView {
6246 - (NSString *) title {
6250 - (NSString *) backButtonTitle {
6254 - (void) setDelegate:(id)delegate {
6255 [table_ setDelegate:delegate];
6256 [super setDelegate:delegate];
6262 @interface SettingsView : RVPage {
6263 _transient Database *database_;
6266 UIPreferencesTable *table_;
6267 _UISwitchSlider *subscribedSwitch_;
6268 _UISwitchSlider *ignoredSwitch_;
6269 UIPreferencesControlTableCell *subscribedCell_;
6270 UIPreferencesControlTableCell *ignoredCell_;
6273 - (id) initWithBook:(RVBook *)book database:(Database *)database package:(NSString *)package;
6277 @implementation SettingsView
6280 [table_ setDataSource:nil];
6283 if (package_ != nil)
6286 [subscribedSwitch_ release];
6287 [ignoredSwitch_ release];
6288 [subscribedCell_ release];
6289 [ignoredCell_ release];
6293 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
6294 if (package_ == nil)
6300 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group {
6301 if (package_ == nil)
6308 default: _assert(false);
6314 - (BOOL) preferencesTable:(UIPreferencesTable *)table isLabelGroup:(int)group {
6315 if (package_ == nil)
6322 default: _assert(false);
6328 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
6329 if (package_ == nil)
6336 default: _assert(false);
6342 - (void) onSomething:(UIPreferencesControlTableCell *)cell withKey:(NSString *)key {
6343 if (package_ == nil)
6346 _UISwitchSlider *slider([cell control]);
6347 BOOL value([slider value] != 0);
6348 NSMutableDictionary *metadata([package_ metadata]);
6351 if (NSNumber *number = [metadata objectForKey:key])
6352 before = [number boolValue];
6356 if (value != before) {
6357 [metadata setObject:[NSNumber numberWithBool:value] forKey:key];
6359 [delegate_ updateData];
6363 - (void) onSubscribed:(UIPreferencesControlTableCell *)cell {
6364 [self onSomething:cell withKey:@"IsSubscribed"];
6367 - (void) onIgnored:(UIPreferencesControlTableCell *)cell {
6368 [self onSomething:cell withKey:@"IsIgnored"];
6371 - (id) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group {
6372 if (package_ == nil)
6376 case 0: switch (row) {
6378 return subscribedCell_;
6380 return ignoredCell_;
6381 default: _assert(false);
6384 case 1: switch (row) {
6386 UIPreferencesControlTableCell *cell([[[UIPreferencesControlTableCell alloc] init] autorelease]);
6387 [cell setShowSelection:NO];
6388 [cell setTitle:@"Changes only shows upgrades to installed packages so as to minimize spam from packagers. Activate this to see upgrades to this package even when it is not installed."];
6392 default: _assert(false);
6395 default: _assert(false);
6401 - (id) initWithBook:(RVBook *)book database:(Database *)database package:(NSString *)package {
6402 if ((self = [super initWithBook:book])) {
6403 database_ = database;
6404 name_ = [package retain];
6406 table_ = [[UIPreferencesTable alloc] initWithFrame:[self bounds]];
6407 [self addSubview:table_];
6409 subscribedSwitch_ = [[_UISwitchSlider alloc] initWithFrame:CGRectMake(200, 10, 50, 20)];
6410 [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:kUIControlEventMouseUpInside];
6412 ignoredSwitch_ = [[_UISwitchSlider alloc] initWithFrame:CGRectMake(200, 10, 50, 20)];
6413 [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:kUIControlEventMouseUpInside];
6415 subscribedCell_ = [[UIPreferencesControlTableCell alloc] init];
6416 [subscribedCell_ setShowSelection:NO];
6417 [subscribedCell_ setTitle:@"Show All Changes"];
6418 [subscribedCell_ setControl:subscribedSwitch_];
6420 ignoredCell_ = [[UIPreferencesControlTableCell alloc] init];
6421 [ignoredCell_ setShowSelection:NO];
6422 [ignoredCell_ setTitle:@"Ignore Upgrades"];
6423 [ignoredCell_ setControl:ignoredSwitch_];
6425 [table_ setDataSource:self];
6430 - (void) resetViewAnimated:(BOOL)animated {
6431 [table_ resetViewAnimated:animated];
6434 - (void) reloadData {
6435 if (package_ != nil)
6436 [package_ autorelease];
6437 package_ = [database_ packageWithName:name_];
6438 if (package_ != nil) {
6440 [subscribedSwitch_ setValue:([package_ subscribed] ? 1 : 0) animated:NO];
6441 [ignoredSwitch_ setValue:([package_ ignored] ? 1 : 0) animated:NO];
6444 [table_ reloadData];
6447 - (NSString *) title {
6453 /* Signature View {{{ */
6454 @interface SignatureView : BrowserView {
6455 _transient Database *database_;
6459 - (id) initWithBook:(RVBook *)book database:(Database *)database package:(NSString *)package;
6463 @implementation SignatureView
6470 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
6472 [super webView:sender didClearWindowObject:window forFrame:frame];
6475 - (id) initWithBook:(RVBook *)book database:(Database *)database package:(NSString *)package {
6476 if ((self = [super initWithBook:book]) != nil) {
6477 database_ = database;
6478 package_ = [package retain];
6483 - (void) resetViewAnimated:(BOOL)animated {
6486 - (void) reloadData {
6487 [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"signature" ofType:@"html"]]];
6493 @interface Cydia : UIApplication <
6494 ConfirmationViewDelegate,
6495 ProgressViewDelegate,
6504 UIToolbar *buttonbar_;
6508 NSMutableArray *essential_;
6509 NSMutableArray *broken_;
6511 Database *database_;
6512 ProgressView *progress_;
6516 UIKeyboard *keyboard_;
6517 UIProgressHUD *hud_;
6519 SectionsView *sections_;
6520 ChangesView *changes_;
6521 ManageView *manage_;
6522 SearchView *search_;
6527 @implementation Cydia
6530 if ([broken_ count] != 0) {
6531 int count = [broken_ count];
6533 UIActionSheet *sheet = [[[UIActionSheet alloc]
6534 initWithTitle:[NSString stringWithFormat:@"%d Half-Installed Package%@", count, (count == 1 ? @"" : @"s")]
6535 buttons:[NSArray arrayWithObjects:
6537 @"Ignore (Temporary)",
6539 defaultButtonIndex:0
6544 [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."];
6545 [sheet popupAlertAnimated:YES];
6546 } else if (!Ignored_ && [essential_ count] != 0) {
6547 int count = [essential_ count];
6549 UIActionSheet *sheet = [[[UIActionSheet alloc]
6550 initWithTitle:[NSString stringWithFormat:@"%d Essential Upgrade%@", count, (count == 1 ? @"" : @"s")]
6551 buttons:[NSArray arrayWithObjects:
6552 @"Upgrade Essential",
6553 @"Complete Upgrade",
6554 @"Ignore (Temporary)",
6556 defaultButtonIndex:0
6561 [sheet setBodyText:@"One or more essential packages are currently out of date. If these upgrades are not performed you are likely to encounter errors."];
6562 [sheet popupAlertAnimated:YES];
6566 - (void) _reloadData {
6569 static bool loaded(false);
6570 UIProgressHUD *hud([self addProgressHUD]);
6571 [hud setText:(loaded ? @"Reloading Data" : @"Loading Data")];
6574 [database_ yieldToSelector:@selector(reloadData) withObject:nil];
6577 [self removeProgressHUD:hud];
6581 [essential_ removeAllObjects];
6582 [broken_ removeAllObjects];
6584 NSArray *packages = [database_ packages];
6585 for (Package *package in packages) {
6587 [broken_ addObject:package];
6588 if ([package upgradableAndEssential:NO]) {
6589 if ([package essential])
6590 [essential_ addObject:package];
6596 NSString *badge([[NSNumber numberWithInt:changes] stringValue]);
6597 [buttonbar_ setBadgeValue:badge forButton:3];
6598 if ([buttonbar_ respondsToSelector:@selector(setBadgeAnimated:forButton:)])
6599 [buttonbar_ setBadgeAnimated:YES forButton:3];
6600 [self setApplicationBadge:badge];
6602 [buttonbar_ setBadgeValue:nil forButton:3];
6603 if ([buttonbar_ respondsToSelector:@selector(setBadgeAnimated:forButton:)])
6604 [buttonbar_ setBadgeAnimated:NO forButton:3];
6605 [self removeApplicationBadge];
6610 // XXX: what is this line of code for?
6611 if ([packages count] == 0);
6612 else if (Loaded_ || ManualRefresh) loaded:
6617 if (NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]) {
6618 NSTimeInterval interval([update timeIntervalSinceNow]);
6619 if (interval <= 0 && interval > -600)
6627 - (void) _saveConfig {
6630 _assert([Metadata_ writeToFile:@"/var/lib/cydia/metadata.plist" atomically:YES] == YES);
6636 - (void) updateData {
6639 /* XXX: this is just stupid */
6640 if (tag_ != 2 && sections_ != nil)
6641 [sections_ reloadData];
6642 if (tag_ != 3 && changes_ != nil)
6643 [changes_ reloadData];
6644 if (tag_ != 5 && search_ != nil)
6645 [search_ reloadData];
6655 FILE *file = fopen("/etc/apt/sources.list.d/cydia.list", "w");
6656 _assert(file != NULL);
6658 NSArray *keys = [Sources_ allKeys];
6660 for (NSString *key in keys) {
6661 NSDictionary *source = [Sources_ objectForKey:key];
6663 fprintf(file, "%s %s %s\n",
6664 [[source objectForKey:@"Type"] UTF8String],
6665 [[source objectForKey:@"URI"] UTF8String],
6666 [[source objectForKey:@"Distribution"] UTF8String]
6675 detachNewThreadSelector:@selector(update_)
6678 title:@"Updating Sources"
6682 - (void) reloadData {
6683 @synchronized (self) {
6684 if (confirm_ == nil)
6690 pkgProblemResolver *resolver = [database_ resolver];
6692 resolver->InstallProtect();
6693 if (!resolver->Resolve(true))
6697 - (void) popUpBook:(RVBook *)book {
6698 [underlay_ popSubview:book];
6701 - (CGRect) popUpBounds {
6702 return [underlay_ bounds];
6706 [database_ prepare];
6708 confirm_ = [[RVBook alloc] initWithFrame:[self popUpBounds]];
6709 [confirm_ setDelegate:self];
6711 ConfirmationView *page([[[ConfirmationView alloc] initWithBook:confirm_ database:database_] autorelease]);
6712 [page setDelegate:self];
6714 [confirm_ setPage:page];
6715 [self popUpBook:confirm_];
6718 - (void) installPackage:(Package *)package {
6719 @synchronized (self) {
6726 - (void) removePackage:(Package *)package {
6727 @synchronized (self) {
6734 - (void) distUpgrade {
6735 @synchronized (self) {
6736 [database_ upgrade];
6742 @synchronized (self) {
6744 if (confirm_ != nil) {
6752 [overlay_ removeFromSuperview];
6756 detachNewThreadSelector:@selector(perform)
6763 - (void) bootstrap_ {
6765 [database_ upgrade];
6766 [database_ prepare];
6767 [database_ perform];
6770 - (void) bootstrap {
6772 detachNewThreadSelector:@selector(bootstrap_)
6775 title:@"Bootstrap Install"
6779 - (void) progressViewIsComplete:(ProgressView *)progress {
6780 if (confirm_ != nil) {
6781 [underlay_ addSubview:overlay_];
6782 [confirm_ popFromSuperviewAnimated:NO];
6788 - (void) setPage:(RVPage *)page {
6789 [page resetViewAnimated:NO];
6790 [page setDelegate:self];
6791 [book_ setPage:page];
6794 - (RVPage *) _pageForURL:(NSURL *)url withClass:(Class)_class {
6795 BrowserView *browser = [[[_class alloc] initWithBook:book_] autorelease];
6796 [browser loadURL:url];
6800 - (void) _setHomePage {
6801 [self setPage:[self _pageForURL:[NSURL URLWithString:@"http://cydia.saurik.com/"] withClass:[HomeView class]]];
6804 - (void) buttonBarItemTapped:(id)sender {
6805 unsigned tag = [sender tag];
6807 [book_ resetViewAnimated:YES];
6809 } else if (tag_ == 2 && tag != 2)
6810 [sections_ resetView];
6813 case 1: [self _setHomePage]; break;
6815 case 2: [self setPage:sections_]; break;
6816 case 3: [self setPage:changes_]; break;
6817 case 4: [self setPage:manage_]; break;
6818 case 5: [self setPage:search_]; break;
6820 default: _assert(false);
6826 - (void) applicationWillSuspend {
6828 [super applicationWillSuspend];
6831 - (void) askForSettings {
6832 UIActionSheet *role = [[[UIActionSheet alloc]
6833 initWithTitle:@"Who Are You?"
6834 buttons:[NSArray arrayWithObjects:
6835 @"User (Graphical Only)",
6836 @"Hacker (+ Command Line)",
6837 @"Developer (No Filters)",
6839 defaultButtonIndex:-1
6844 [role setBodyText:@"Not all of the packages available via Cydia are designed to be used by all users. Please categorize yourself so that Cydia can apply helpful filters.\n\nThis choice can be changed from \"Settings\" under the \"Manage\" tab."];
6845 [role popupAlertAnimated:YES];
6850 [self setStatusBarShowsProgress:NO];
6851 [self removeProgressHUD:hud_];
6856 pid_t pid = ExecFork();
6858 execlp("launchctl", "launchctl", "stop", "com.apple.SpringBoard", NULL);
6859 perror("launchctl stop");
6866 [self askForSettings];
6871 overlay_ = [[UIView alloc] initWithFrame:[underlay_ bounds]];
6873 CGRect screenrect = [UIHardware fullScreenApplicationContentRect];
6874 book_ = [[CYBook alloc] initWithFrame:CGRectMake(
6875 0, 0, screenrect.size.width, screenrect.size.height - 48
6876 ) database:database_];
6878 [book_ setDelegate:self];
6880 [overlay_ addSubview:book_];
6882 NSArray *buttonitems = [NSArray arrayWithObjects:
6883 [NSDictionary dictionaryWithObjectsAndKeys:
6884 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
6885 @"home-up.png", kUIButtonBarButtonInfo,
6886 @"home-dn.png", kUIButtonBarButtonSelectedInfo,
6887 [NSNumber numberWithInt:1], kUIButtonBarButtonTag,
6888 self, kUIButtonBarButtonTarget,
6889 @"Home", kUIButtonBarButtonTitle,
6890 @"0", kUIButtonBarButtonType,
6893 [NSDictionary dictionaryWithObjectsAndKeys:
6894 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
6895 @"install-up.png", kUIButtonBarButtonInfo,
6896 @"install-dn.png", kUIButtonBarButtonSelectedInfo,
6897 [NSNumber numberWithInt:2], kUIButtonBarButtonTag,
6898 self, kUIButtonBarButtonTarget,
6899 @"Sections", kUIButtonBarButtonTitle,
6900 @"0", kUIButtonBarButtonType,
6903 [NSDictionary dictionaryWithObjectsAndKeys:
6904 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
6905 @"changes-up.png", kUIButtonBarButtonInfo,
6906 @"changes-dn.png", kUIButtonBarButtonSelectedInfo,
6907 [NSNumber numberWithInt:3], kUIButtonBarButtonTag,
6908 self, kUIButtonBarButtonTarget,
6909 @"Changes", kUIButtonBarButtonTitle,
6910 @"0", kUIButtonBarButtonType,
6913 [NSDictionary dictionaryWithObjectsAndKeys:
6914 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
6915 @"manage-up.png", kUIButtonBarButtonInfo,
6916 @"manage-dn.png", kUIButtonBarButtonSelectedInfo,
6917 [NSNumber numberWithInt:4], kUIButtonBarButtonTag,
6918 self, kUIButtonBarButtonTarget,
6919 @"Manage", kUIButtonBarButtonTitle,
6920 @"0", kUIButtonBarButtonType,
6923 [NSDictionary dictionaryWithObjectsAndKeys:
6924 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
6925 @"search-up.png", kUIButtonBarButtonInfo,
6926 @"search-dn.png", kUIButtonBarButtonSelectedInfo,
6927 [NSNumber numberWithInt:5], kUIButtonBarButtonTag,
6928 self, kUIButtonBarButtonTarget,
6929 @"Search", kUIButtonBarButtonTitle,
6930 @"0", kUIButtonBarButtonType,
6934 buttonbar_ = [[UIToolbar alloc]
6936 withFrame:CGRectMake(
6937 0, screenrect.size.height - ButtonBarHeight_,
6938 screenrect.size.width, ButtonBarHeight_
6940 withItemList:buttonitems
6943 [buttonbar_ setDelegate:self];
6944 [buttonbar_ setBarStyle:1];
6945 [buttonbar_ setButtonBarTrackingMode:2];
6947 int buttons[5] = {1, 2, 3, 4, 5};
6948 [buttonbar_ registerButtonGroup:0 withButtons:buttons withCount:5];
6949 [buttonbar_ showButtonGroup:0 withDuration:0];
6951 for (int i = 0; i != 5; ++i)
6952 [[buttonbar_ viewWithTag:(i + 1)] setFrame:CGRectMake(
6953 i * 64 + 2, 1, 60, ButtonBarHeight_
6956 [buttonbar_ showSelectionForButton:1];
6957 [overlay_ addSubview:buttonbar_];
6959 [UIKeyboard initImplementationNow];
6960 CGSize keysize = [UIKeyboard defaultSize];
6961 CGRect keyrect = {{0, [overlay_ bounds].size.height}, keysize};
6962 keyboard_ = [[UIKeyboard alloc] initWithFrame:keyrect];
6963 //[[UIKeyboardImpl sharedInstance] setSoundsEnabled:(Sounds_Keyboard_ ? YES : NO)];
6964 [overlay_ addSubview:keyboard_];
6967 [underlay_ addSubview:overlay_];
6971 sections_ = [[SectionsView alloc] initWithBook:book_ database:database_];
6972 changes_ = [[ChangesView alloc] initWithBook:book_ database:database_];
6973 search_ = [[SearchView alloc] initWithBook:book_ database:database_];
6975 manage_ = (ManageView *) [[self
6976 _pageForURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"manage" ofType:@"html"]]
6977 withClass:[ManageView class]
6985 [self _setHomePage];
6988 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
6989 NSString *context([sheet context]);
6991 if ([context isEqualToString:@"missing"])
6993 else if ([context isEqualToString:@"fixhalf"]) {
6996 @synchronized (self) {
6997 for (Package *broken in broken_) {
7000 NSString *id = [broken id];
7001 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]);
7002 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]);
7003 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]);
7004 unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]);
7013 [broken_ removeAllObjects];
7022 } else if ([context isEqualToString:@"role"]) {
7024 case 1: Role_ = @"User"; break;
7025 case 2: Role_ = @"Hacker"; break;
7026 case 3: Role_ = @"Developer"; break;
7033 bool reset = Settings_ != nil;
7035 Settings_ = [NSMutableDictionary dictionaryWithObjectsAndKeys:
7039 [Metadata_ setObject:Settings_ forKey:@"Settings"];
7049 } else if ([context isEqualToString:@"upgrade"]) {
7052 @synchronized (self) {
7053 for (Package *essential in essential_)
7054 [essential install];
7077 - (void) reorganize { _pooled
7078 system("/usr/libexec/cydia/free.sh");
7079 [self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:NO];
7082 - (void) applicationSuspend:(__GSEvent *)event {
7083 if (hud_ == nil && ![progress_ isRunning])
7084 [super applicationSuspend:event];
7087 - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 {
7089 [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3];
7092 - (void) _setSuspended:(BOOL)value {
7094 [super _setSuspended:value];
7097 - (UIProgressHUD *) addProgressHUD {
7098 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window_] autorelease]);
7099 [window_ setUserInteractionEnabled:NO];
7101 [progress_ addSubview:hud];
7105 - (void) removeProgressHUD:(UIProgressHUD *)hud {
7107 [hud removeFromSuperview];
7108 [window_ setUserInteractionEnabled:YES];
7111 - (void) openMailToURL:(NSURL *)url {
7112 // XXX: this makes me sad
7114 [[[MailToView alloc] initWithView:underlay_ delegate:self url:url] autorelease];
7116 [UIApp openURL:url];// asPanel:YES];
7120 - (void) clearFirstResponder {
7121 if (id responder = [window_ firstResponder])
7122 [responder resignFirstResponder];
7125 - (RVPage *) pageForPackage:(NSString *)name {
7126 if (Package *package = [database_ packageWithName:name]) {
7127 PackageView *view = [[[PackageView alloc] initWithBook:book_ database:database_] autorelease];
7128 [view setPackage:package];
7131 UIActionSheet *sheet = [[[UIActionSheet alloc]
7132 initWithTitle:@"Cannot Locate Package"
7133 buttons:[NSArray arrayWithObjects:@"Close", nil]
7134 defaultButtonIndex:0
7139 [sheet setBodyText:[NSString stringWithFormat:
7140 @"The package %@ cannot be found in your current sources. I might recommend installing more sources."
7143 [sheet popupAlertAnimated:YES];
7148 - (RVPage *) pageForURL:(NSURL *)url hasTag:(int *)tag {
7152 NSString *scheme([[url scheme] lowercaseString]);
7153 if (![scheme isEqualToString:@"cydia"])
7155 NSString *path([url absoluteString]);
7156 if ([path length] < 8)
7158 path = [path substringFromIndex:8];
7159 if (![path hasPrefix:@"/"])
7160 path = [@"/" stringByAppendingString:path];
7162 if ([path isEqualToString:@"/add-source"])
7163 return [[[AddSourceView alloc] initWithBook:book_ database:database_] autorelease];
7164 else if ([path isEqualToString:@"/storage"])
7165 return [self _pageForURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"storage" ofType:@"html"]] withClass:[BrowserView class]];
7166 else if ([path isEqualToString:@"/sources"])
7167 return [[[SourceTable alloc] initWithBook:book_ database:database_] autorelease];
7168 else if ([path isEqualToString:@"/packages"])
7169 return [[[InstalledView alloc] initWithBook:book_ database:database_] autorelease];
7170 else if ([path hasPrefix:@"/url/"])
7171 return [self _pageForURL:[NSURL URLWithString:[path substringFromIndex:5]] withClass:[BrowserView class]];
7172 else if ([path hasPrefix:@"/launch/"])
7173 [self launchApplicationWithIdentifier:[path substringFromIndex:8] suspended:NO];
7174 else if ([path hasPrefix:@"/package-settings/"])
7175 return [[[SettingsView alloc] initWithBook:book_ database:database_ package:[path substringFromIndex:18]] autorelease];
7176 else if ([path hasPrefix:@"/package-signature/"])
7177 return [[[SignatureView alloc] initWithBook:book_ database:database_ package:[path substringFromIndex:19]] autorelease];
7178 else if ([path hasPrefix:@"/package/"])
7179 return [self pageForPackage:[path substringFromIndex:9]];
7180 else if ([path hasPrefix:@"/files/"]) {
7181 NSString *name = [path substringFromIndex:7];
7183 if (Package *package = [database_ packageWithName:name]) {
7184 FileTable *files = [[[FileTable alloc] initWithBook:book_ database:database_] autorelease];
7185 [files setPackage:package];
7193 - (void) applicationOpenURL:(NSURL *)url {
7194 [super applicationOpenURL:url];
7196 if (RVPage *page = [self pageForURL:url hasTag:&tag]) {
7197 [self setPage:page];
7198 [buttonbar_ showSelectionForButton:tag];
7203 - (void) applicationDidFinishLaunching:(id)unused {
7205 Font12_ = [[UIFont systemFontOfSize:12] retain];
7206 Font12Bold_ = [[UIFont boldSystemFontOfSize:12] retain];
7207 Font14_ = [[UIFont systemFontOfSize:14] retain];
7208 Font18Bold_ = [[UIFont boldSystemFontOfSize:18] retain];
7209 Font22Bold_ = [[UIFont boldSystemFontOfSize:22] retain];
7211 _assert(pkgInitConfig(*_config));
7212 _assert(pkgInitSystem(*_config, _system));
7216 essential_ = [[NSMutableArray alloc] initWithCapacity:4];
7217 broken_ = [[NSMutableArray alloc] initWithCapacity:4];
7219 [NSURLProtocol registerClass:[CydiaURLProtocol class]];
7221 CGRect screenrect = [UIHardware fullScreenApplicationContentRect];
7222 window_ = [[UIWindow alloc] initWithContentRect:screenrect];
7224 [window_ orderFront:self];
7225 [window_ makeKey:self];
7226 [window_ setHidden:NO];
7228 database_ = [Database sharedInstance];
7229 progress_ = [[ProgressView alloc] initWithFrame:[window_ bounds] database:database_ delegate:self];
7230 [database_ setDelegate:progress_];
7231 [window_ setContentView:progress_];
7233 underlay_ = [[UIView alloc] initWithFrame:[progress_ bounds]];
7234 [progress_ setContentView:underlay_];
7236 [progress_ resetView];
7239 readlink("/Applications", NULL, 0) == -1 && errno == EINVAL ||
7240 readlink("/Library/Ringtones", NULL, 0) == -1 && errno == EINVAL ||
7241 readlink("/Library/Wallpaper", NULL, 0) == -1 && errno == EINVAL ||
7242 readlink("/usr/include", NULL, 0) == -1 && errno == EINVAL ||
7243 readlink("/usr/libexec", NULL, 0) == -1 && errno == EINVAL ||
7244 readlink("/usr/share", NULL, 0) == -1 && errno == EINVAL /*||
7245 readlink("/var/lib", NULL, 0) == -1 && errno == EINVAL*/
7247 [self setIdleTimerDisabled:YES];
7249 hud_ = [[self addProgressHUD] retain];
7250 [hud_ setText:@"Reorganizing\n\nWill Automatically\nClose When Done"];
7252 [self setStatusBarShowsProgress:YES];
7255 detachNewThreadSelector:@selector(reorganize)
7263 - (void) showKeyboard:(BOOL)show {
7264 CGSize keysize = [UIKeyboard defaultSize];
7265 CGRect keydown = {{0, [overlay_ bounds].size.height}, keysize};
7266 CGRect keyup = keydown;
7267 keyup.origin.y -= keysize.height;
7269 UIFrameAnimation *animation = [[[UIFrameAnimation alloc] initWithTarget:keyboard_] autorelease];
7270 [animation setSignificantRectFields:2];
7273 [animation setStartFrame:keydown];
7274 [animation setEndFrame:keyup];
7275 [keyboard_ activate];
7277 [animation setStartFrame:keyup];
7278 [animation setEndFrame:keydown];
7279 [keyboard_ deactivate];
7282 [[UIAnimator sharedAnimator]
7283 addAnimations:[NSArray arrayWithObjects:animation, nil]
7284 withDuration:KeyboardTime_
7289 - (void) slideUp:(UIActionSheet *)alert {
7291 [alert presentSheetFromButtonBar:buttonbar_];
7293 [alert presentSheetInView:overlay_];
7298 void AddPreferences(NSString *plist) { _pooled
7299 NSMutableDictionary *settings = [[[NSMutableDictionary alloc] initWithContentsOfFile:plist] autorelease];
7300 _assert(settings != NULL);
7301 NSMutableArray *items = [settings objectForKey:@"items"];
7305 for (NSMutableDictionary *item in items) {
7306 NSString *label = [item objectForKey:@"label"];
7307 if (label != nil && [label isEqualToString:@"Cydia"]) {
7314 for (size_t i(0); i != [items count]; ++i) {
7315 NSDictionary *item([items objectAtIndex:i]);
7316 NSString *label = [item objectForKey:@"label"];
7317 if (label != nil && [label isEqualToString:@"General"]) {
7318 [items insertObject:[NSDictionary dictionaryWithObjectsAndKeys:
7319 @"CydiaSettings", @"bundle",
7320 @"PSLinkCell", @"cell",
7321 [NSNumber numberWithBool:YES], @"hasIcon",
7322 [NSNumber numberWithBool:YES], @"isController",
7324 nil] atIndex:(i + 1)];
7330 _assert([settings writeToFile:plist atomically:YES] == YES);
7335 id Alloc_(id self, SEL selector) {
7336 id object = alloc_(self, selector);
7337 lprintf("[%s]A-%p\n", self->isa->name, object);
7342 id Dealloc_(id self, SEL selector) {
7343 id object = dealloc_(self, selector);
7344 lprintf("[%s]D-%p\n", self->isa->name, object);
7348 int main(int argc, char *argv[]) { _pooled
7350 class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16");
7352 bool substrate(false);
7358 for (int argi(1); argi != argc; ++argi)
7359 if (strcmp(argv[argi], "--") == 0) {
7361 argv[argi] = argv[0];
7367 for (int argi(1); argi != arge; ++argi)
7368 if (strcmp(args[argi], "--bootstrap") == 0)
7370 else if (strcmp(args[argi], "--substrate") == 0)
7373 fprintf(stderr, "unknown argument: %s\n", args[argi]);
7376 App_ = [[NSBundle mainBundle] bundlePath];
7377 Home_ = NSHomeDirectory();
7378 Locale_ = CFLocaleCopyCurrent();
7381 NSString *plist = [Home_ stringByAppendingString:@"/Library/Preferences/com.apple.preferences.sounds.plist"];
7382 if (NSDictionary *sounds = [NSDictionary dictionaryWithContentsOfFile:plist])
7383 if (NSNumber *keyboard = [sounds objectForKey:@"keyboard"])
7384 Sounds_Keyboard_ = [keyboard boolValue];
7390 #if 1 /* XXX: this costs 1.4s of startup performance */
7391 if (unlink("/var/cache/apt/pkgcache.bin") == -1)
7392 _assert(errno == ENOENT);
7393 if (unlink("/var/cache/apt/srcpkgcache.bin") == -1)
7394 _assert(errno == ENOENT);
7397 /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc));
7398 alloc_ = alloc->method_imp;
7399 alloc->method_imp = (IMP) &Alloc_;*/
7401 /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc));
7402 dealloc_ = dealloc->method_imp;
7403 dealloc->method_imp = (IMP) &Dealloc_;*/
7408 size = sizeof(maxproc);
7409 if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
7410 perror("sysctlbyname(\"kern.maxproc\", ?)");
7411 else if (maxproc < 64) {
7413 if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
7414 perror("sysctlbyname(\"kern.maxproc\", #)");
7417 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
7418 char *machine = new char[size];
7419 if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1)
7420 perror("sysctlbyname(\"hw.machine\", ?)");
7424 UniqueID_ = [[UIDevice currentDevice] uniqueIdentifier];
7426 if (NSDictionary *system = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"])
7427 Build_ = [system objectForKey:@"ProductBuildVersion"];
7428 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) {
7429 Product_ = [info objectForKey:@"SafariProductVersion"];
7430 Safari_ = [info objectForKey:@"CFBundleVersion"];
7433 /*AddPreferences(@"/Applications/Preferences.app/Settings-iPhone.plist");
7434 AddPreferences(@"/Applications/Preferences.app/Settings-iPod.plist");*/
7437 Metadata_ = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"];
7440 if (Metadata_ == NULL)
7441 Metadata_ = [[NSMutableDictionary alloc] initWithCapacity:2];
7443 Settings_ = [Metadata_ objectForKey:@"Settings"];
7445 Packages_ = [Metadata_ objectForKey:@"Packages"];
7446 Sections_ = [Metadata_ objectForKey:@"Sections"];
7447 Sources_ = [Metadata_ objectForKey:@"Sources"];
7450 if (Settings_ != nil)
7451 Role_ = [Settings_ objectForKey:@"Role"];
7453 if (Packages_ == nil) {
7454 Packages_ = [[[NSMutableDictionary alloc] initWithCapacity:128] autorelease];
7455 [Metadata_ setObject:Packages_ forKey:@"Packages"];
7458 if (Sections_ == nil) {
7459 Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease];
7460 [Metadata_ setObject:Sections_ forKey:@"Sections"];
7463 if (Sources_ == nil) {
7464 Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
7465 [Metadata_ setObject:Sources_ forKey:@"Sources"];
7469 Documents_ = [[[NSMutableArray alloc] initWithCapacity:4] autorelease];
7472 if (substrate && access("/Applications/WinterBoard.app/WinterBoard.dylib", F_OK) == 0)
7473 dlopen("/Applications/WinterBoard.app/WinterBoard.dylib", RTLD_LAZY | RTLD_GLOBAL);
7474 /*if (substrate && access("/Library/MobileSubstrate/MobileSubstrate.dylib", F_OK) == 0)
7475 dlopen("/Library/MobileSubstrate/MobileSubstrate.dylib", RTLD_LAZY | RTLD_GLOBAL);*/
7477 if (access("/User", F_OK) != 0) {
7479 system("/usr/libexec/cydia/firmware.sh");
7483 _assert([[NSFileManager defaultManager]
7484 createDirectoryAtPath:@"/var/cache/apt/archives/partial"
7485 withIntermediateDirectories:YES
7490 space_ = CGColorSpaceCreateDeviceRGB();
7492 Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0);
7493 Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0);
7494 Black_.Set(space_, 0.0, 0.0, 0.0, 1.0);
7495 Off_.Set(space_, 0.9, 0.9, 0.9, 1.0);
7496 White_.Set(space_, 1.0, 1.0, 1.0, 1.0);
7497 Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0);
7498 Green_.Set(space_, 0.0, 0.5, 0.0, 1.0);
7499 Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0);
7500 Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0);
7501 /*Purple_.Set(space_, 1.0, 0.3, 0.0, 1.0);
7502 Purplish_.Set(space_, 1.0, 0.6, 0.4, 1.0); ORANGE */
7503 /*Purple_.Set(space_, 1.0, 0.5, 0.0, 1.0);
7504 Purplish_.Set(space_, 1.0, 0.7, 0.2, 1.0); ORANGISH */
7505 /*Purple_.Set(space_, 0.5, 0.0, 0.7, 1.0);
7506 Purplish_.Set(space_, 0.7, 0.4, 0.8, 1.0); PURPLE */
7508 CommercialColor_ = [UIColor colorWithRed:0.93f green:1.00f blue:0.88f alpha:1.00f];
7510 Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil];
7512 SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease];
7514 UIApplicationUseLegacyEvents(YES);
7515 UIKeyboardDisableAutomaticAppearance();
7518 int value = UIApplicationMain(argc, argv, @"Cydia", @"Cydia");
7520 CGColorSpaceRelease(space_);