X-Git-Url: https://git.saurik.com/cydia.git/blobdiff_plain/4121c5e002cf039931b67ef2c592783c64f0cc59..217e22f0f4f84e74a3649d5e292c283b0c468b7f:/MobileCydia.mm diff --git a/MobileCydia.mm b/MobileCydia.mm index 30997aa8..99b2b57a 100644 --- a/MobileCydia.mm +++ b/MobileCydia.mm @@ -1,5 +1,5 @@ /* Cydia - iPhone UIKit Front-End for Debian APT - * Copyright (C) 2008-2013 Jay Freeman (saurik) + * Copyright (C) 2008-2015 Jay Freeman (saurik) */ /* GNU General Public License, Version 3 {{{ */ @@ -26,6 +26,9 @@ #include "CyteKit/UCPlatform.h" #include "CyteKit/Localize.h" +#include +#include + #include #include @@ -38,7 +41,6 @@ #include #endif -#include #include #include @@ -51,17 +53,19 @@ #include #include -#include #include +#include #include +#include #include #include -#include +#include "fdstream.hpp" #undef ABS +#include "apt.h" #include #include #include @@ -80,8 +84,6 @@ #include #include -#include - #include #include #include @@ -89,6 +91,7 @@ #include #include +#include #include #include #include @@ -106,23 +109,15 @@ extern "C" { #include #include "Sources.h" -#include +#include "Substrate.hpp" #include "Menes/Menes.h" -#include "CyteKit/IndirectDelegate.h" -#include "CyteKit/PerlCompatibleRegEx.hpp" -#include "CyteKit/TableViewCell.h" -#include "CyteKit/TabBarController.h" -#include "CyteKit/WebScriptObject-Cyte.h" -#include "CyteKit/WebViewController.h" -#include "CyteKit/WebViewTableViewCell.h" -#include "CyteKit/stringWithUTF8Bytes.h" +#include "CyteKit/CyteKit.h" +#include "CyteKit/RegEx.hpp" #include "Cydia/MIMEAddress.h" #include "Cydia/LoadingViewController.h" #include "Cydia/ProgressEvent.h" - -#include "SDURLCache/SDURLCache.h" /* }}} */ /* Profiler {{{ */ @@ -159,7 +154,7 @@ class ProfileTime { void Print() { if (total_ != 0) - std::cerr << std::setw(5) << count_ << ", " << std::setw(7) << total_ << " : " << name_ << std::endl; + std::cerr << std::setw(7) << count_ << ", " << std::setw(8) << total_ << " : " << name_ << std::endl; total_ = 0; count_ = 0; } @@ -195,17 +190,13 @@ void PrintTimes() { #define _end } /* }}} */ -// XXX: I hate clang. Apple: please get over your petty hatred of GPL and fix your gcc fork -#define synchronized(lock) \ - synchronized(static_cast(lock)) - extern NSString *Cydia_; #define lprintf(args...) fprintf(stderr, args) #define ForRelease 1 #define TraceLogging (1 && !ForRelease) -#define HistogramInsertionSort (!ForRelease ? 0 : 0) +#define HistogramInsertionSort (0 && !ForRelease) #define ProfileTimes (0 && !ForRelease) #define ForSaurik (0 && !ForRelease) #define LogBrowser (0 && !ForRelease) @@ -213,7 +204,6 @@ extern NSString *Cydia_; #define ManualRefresh (1 && !ForRelease) #define ShowInternals (0 && !ForRelease) #define AlwaysReload (0 && !ForRelease) -#define TryIndexedCollation (0 && !ForRelease) #if !TraceLogging #undef _trace @@ -237,14 +227,27 @@ union SplitHash { }; // }}} +@implementation NSDictionary (Cydia) +- (id) invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)arguments { + if (false); + else if ([name isEqualToString:@"get"]) + return [self objectForKey:[arguments objectAtIndex:0]]; + else if ([name isEqualToString:@"keys"]) + return [self allKeys]; + return nil; +} @end + static NSString *Colon_; NSString *Elision_; static NSString *Error_; static NSString *Warning_; static NSString *Cache_; +#define Cache(file) \ + [NSString stringWithFormat:@"%@/%s", Cache_, file] static void (*$SBSSetInterceptsMenuButtonForever)(bool); +static NSData *(*$SBSCopyIconImagePNGDataForDisplayIdentifier)(NSString *); static CFStringRef (*$MGCopyAnswer)(CFStringRef); @@ -288,13 +291,8 @@ static _finline NSString *CydiaURL(NSString *path) { return [[NSString stringWithUTF8String:page] stringByAppendingString:path]; } -static void ReapZombie(pid_t pid) { - int status; - wait: - if (waitpid(pid, &status, 0) == -1) - if (errno == EINTR) - goto wait; - else _assert(false); +static NSString *ShellEscape(NSString *value) { + return [NSString stringWithFormat:@"'%@'", [value stringByReplacingOccurrencesOfString:@"'" withString:@"'\\''"]]; } static _finline void UpdateExternalStatus(uint64_t newStatus) { @@ -314,57 +312,51 @@ static CGFloat CYStatusBarHeight() { /* NSForcedOrderingSearch doesn't work on the iPhone */ static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; -static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareCaseInsensitive | kCFCompareNonliteral | kCFCompareLocalized | kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; +static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; /* Insertion Sort {{{ */ -CFIndex SKBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { - const char *ptr = (const char *)list; - while (0 < count) { - CFIndex half = count / 2; - const char *probe = ptr + elementSize * half; - CFComparisonResult cr = comparator(element, probe, context); - if (0 == cr) return (probe - (const char *)list) / elementSize; - ptr = (cr < 0) ? ptr : probe + elementSize; - count = (cr < 0) ? half : (half + (count & 1) - 1); - } - return (ptr - (const char *)list) / elementSize; -} - -CFIndex CFBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { +template +size_t CFBSearch_(const Type_ &element, const void *list, size_t count, CFComparisonResult (*comparator)(Type_, Type_, void *), void *context) { const char *ptr = (const char *)list; while (0 < count) { - CFIndex half = count / 2; - const char *probe = ptr + elementSize * half; - CFComparisonResult cr = comparator(element, probe, context); - if (0 == cr) return (probe - (const char *)list) / elementSize; - ptr = (cr < 0) ? ptr : probe + elementSize; + size_t half = count / 2; + const char *probe = ptr + sizeof(Type_) * half; + CFComparisonResult cr = comparator(element, * (const Type_ *) probe, context); + if (0 == cr) return (probe - (const char *)list) / sizeof(Type_); + ptr = (cr < 0) ? ptr : probe + sizeof(Type_); count = (cr < 0) ? half : (half + (count & 1) - 1); } - return (ptr - (const char *)list) / elementSize; + return (ptr - (const char *)list) / sizeof(Type_); } -void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFComparatorFunction comparator, void *context) { - if (range.length == 0) +template +void CYArrayInsertionSortValues(Type_ *values, size_t length, CFComparisonResult (*comparator)(Type_, Type_, void *), void *context) { + if (length == 0) return; - const void **values(new const void *[range.length]); - CFArrayGetValues(array, range, values); #if HistogramInsertionSort > 0 - uint32_t total(0), *offsets(new uint32_t[range.length]); + uint32_t total(0), *offsets(new uint32_t[length]); #endif - for (CFIndex index(1); index != range.length; ++index) { - const void *value(values[index]); - //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); - CFIndex correct(index); + for (size_t index(1); index != length; ++index) { + Type_ value(values[index]); +#if 0 + size_t correct(CFBSearch_(value, values, index, comparator, context)); +#else + size_t correct(index); while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { #if HistogramInsertionSort > 1 NSLog(@"%@ < %@", value, values[correct - 1]); #endif if (--correct == 0) break; + if (index - correct >= 8) { + correct = CFBSearch_(value, values, correct, comparator, context); + break; + } } +#endif if (correct != index) { size_t offset(index - correct); #if HistogramInsertionSort @@ -378,11 +370,8 @@ void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFCompar } } - CFArrayReplaceValues(array, range, values, range.length); - delete [] values; - #if HistogramInsertionSort > 0 - for (CFIndex index(0); index != range.length; ++index) + for (size_t index(0); index != range.length; ++index) if (offsets[index] != 0) NSLog(@"Insertion Displacement [%u]: %u", index, offsets[index]); NSLog(@"Average Insertion Displacement: %f", double(total) / range.length); @@ -392,47 +381,6 @@ void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFCompar /* }}} */ -/* Apple Bug Fixes {{{ */ -@implementation UIWebDocumentView (Cydia) - -- (void) _setScrollerOffset:(CGPoint)offset { - UIScroller *scroller([self _scroller]); - - CGSize size([scroller contentSize]); - CGSize bounds([scroller bounds].size); - - CGPoint max; - max.x = size.width - bounds.width; - max.y = size.height - bounds.height; - - // wtf Apple?! - if (max.x < 0) - max.x = 0; - if (max.y < 0) - max.y = 0; - - offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; - offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; - - [scroller setOffset:offset]; -} - -@end -/* }}} */ - -NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { - size_t length([self length] - state->state); - if (length <= 0) - return 0; - else if (length > count) - length = count; - for (size_t i(0); i != length; ++i) - objects[i] = [self item:state->state++]; - state->itemsPtr = objects; - state->mutationsPtr = (unsigned long *) self; - return length; -} - /* Cydia NSString Additions {{{ */ @interface NSString (Cydia) - (NSComparisonResult) compareByPath:(NSString *)other; @@ -491,6 +439,10 @@ static _finline CFStringRef CYStringCreate(const char *data, size_t size) { CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast(data), size, kCFStringEncodingISOLatin1, NO, kCFAllocatorNull); } +static _finline CFStringRef CYStringCreate(const std::string &data) { + return CYStringCreate(data.data(), data.size()); +} + static _finline CFStringRef CYStringCreate(const char *data) { return CYStringCreate(data, strlen(data)); } @@ -547,14 +499,14 @@ class CYString { cache_ = reinterpret_cast(CFRetain(rhs.cache_)); } - void copy(apr_pool_t *pool) { - char *temp(reinterpret_cast(apr_palloc(pool, size_ + 1))); + void copy(CYPool *pool) { + char *temp(pool->malloc(size_ + 1)); memcpy(temp, data_, size_); temp[size_] = '\0'; data_ = temp; } - void set(apr_pool_t *pool, const char *data, size_t size) { + void set(CYPool *pool, const char *data, size_t size) { if (size == 0) clear(); else { @@ -568,11 +520,11 @@ class CYString { } } - _finline void set(apr_pool_t *pool, const char *data) { + _finline void set(CYPool *pool, const char *data) { set(pool, data, data == NULL ? 0 : strlen(data)); } - _finline void set(apr_pool_t *pool, const std::string &rhs) { + _finline void set(CYPool *pool, const std::string &rhs) { set(pool, rhs.data(), rhs.size()); } @@ -714,29 +666,76 @@ static _H System_; static NSString *SerialNumber_ = nil; static NSString *ChipID_ = nil; static NSString *BBSNum_ = nil; -static _H Token_; static _H UniqueID_; static _H UserAgent_; static _H Product_; static _H Safari_; +static _H CollationLocale_; +static _H CollationThumbs_; +static std::vector CollationOffset_; +static _H CollationTitles_; +static _H CollationStarts_; +static UTransliterator *CollationTransl_; +//static Function CollationModify_; + +typedef std::basic_string ustring; +static ustring CollationString_; + +#define CUC const ustring &str(*reinterpret_cast(rep)) +#define UC ustring &str(*reinterpret_cast(rep)) +static struct UReplaceableCallbacks CollationUCalls_ = { + .length = [](const UReplaceable *rep) -> int32_t { CUC; + return str.size(); + }, + + .charAt = [](const UReplaceable *rep, int32_t offset) -> UChar { CUC; + //fprintf(stderr, "charAt(%d) : %d\n", offset, str.size()); + if (offset >= str.size()) + return 0xffff; + return str[offset]; + }, + + .char32At = [](const UReplaceable *rep, int32_t offset) -> UChar32 { CUC; + //fprintf(stderr, "char32At(%d) : %d\n", offset, str.size()); + if (offset >= str.size()) + return 0xffff; + UChar32 c; + U16_GET(str.data(), 0, offset, str.size(), c); + return c; + }, + + .replace = [](UReplaceable *rep, int32_t start, int32_t limit, const UChar *text, int32_t length) -> void { UC; + //fprintf(stderr, "replace(%d, %d, %d) : %d\n", start, limit, length, str.size()); + str.replace(start, limit - start, text, length); + }, + + .extract = [](UReplaceable *rep, int32_t start, int32_t limit, UChar *dst) -> void { UC; + //fprintf(stderr, "extract(%d, %d) : %d\n", start, limit, str.size()); + str.copy(dst, limit - start, start); + }, + + .copy = [](UReplaceable *rep, int32_t start, int32_t limit, int32_t dest) -> void { UC; + //fprintf(stderr, "copy(%d, %d, %d) : %d\n", start, limit, dest, str.size()); + str.replace(dest, 0, str, start, limit - start); + }, +}; + static CFLocaleRef Locale_; static NSArray *Languages_; static CGColorSpaceRef space_; +#define CacheState_ "/var/mobile/Library/Caches/com.saurik.Cydia/CacheState.plist" +#define SavedState_ "/var/mobile/Library/Caches/com.saurik.Cydia/SavedState.plist" + static NSDictionary *SectionMap_; -static NSMutableDictionary *Metadata_; -static _transient NSMutableDictionary *Settings_; -static _transient NSMutableDictionary *Packages_; +static _H Backgrounded_; static _transient NSMutableDictionary *Values_; static _transient NSMutableDictionary *Sections_; _H Sources_; static _transient NSNumber *Version_; -bool Changed_; static time_t now_; -bool IsWildcat_; -static CGFloat ScreenScale_; static NSString *Idiom_; static _H Firmware_; static NSString *Major_; @@ -744,10 +743,7 @@ static NSString *Major_; static _H SessionData_; static _H HostConfig_; static _H BridgedHosts_; -static _H TokenHosts_; static _H InsecureHosts_; -static _H PipelinedHosts_; -static _H CachedURLs_; static NSString *kCydiaProgressEventTypeError = @"Error"; static NSString *kCydiaProgressEventTypeInformation = @"Information"; @@ -760,13 +756,33 @@ inline float Interpolate(float begin, float end, float fraction) { return (end - begin) * fraction + begin; } +static inline double Retina(double value) { + value *= ScreenScale_; + value = round(value); + value /= ScreenScale_; + return value; +} + +static inline CGRect Retina(CGRect value) { + value.origin.x *= ScreenScale_; + value.origin.y *= ScreenScale_; + value.size.width *= ScreenScale_; + value.size.height *= ScreenScale_; + value = CGRectIntegral(value); + value.origin.x /= ScreenScale_; + value.origin.y /= ScreenScale_; + value.size.width /= ScreenScale_; + value.size.height /= ScreenScale_; + return value; +} + static _finline const char *StripVersion_(const char *version) { const char *colon(strchr(version, ':')); return colon == NULL ? version : colon + 1; } NSString *LocalizeSection(NSString *section) { - static Pcre title_r("^(.*?) \\((.*)\\)$"); + static RegEx title_r("(.*?) \\((.*)\\)"); if (title_r(section)) { NSString *parent(title_r[1]); NSString *child(title_r[2]); @@ -782,17 +798,17 @@ NSString *LocalizeSection(NSString *section) { NSString *Simplify(NSString *title) { const char *data = [title UTF8String]; - size_t size = [title length]; + size_t size = [title lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; - static Pcre square_r("^\\[(.*)\\]$"); + static RegEx square_r("\\[(.*)\\]"); if (square_r(data, size)) return Simplify(square_r[1]); - static Pcre paren_r("^\\((.*)\\)$"); + static RegEx paren_r("\\((.*)\\)"); if (paren_r(data, size)) return Simplify(paren_r[1]); - static Pcre title_r("^(.*?) \\((.*)\\)$"); + static RegEx title_r("(.*?) \\((.*)\\)"); if (title_r(data, size)) return Simplify(title_r[1]); @@ -800,20 +816,6 @@ NSString *Simplify(NSString *title) { } /* }}} */ -NSString *GetLastUpdate() { - NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]; - - if (update == nil) - return UCLocalize("NEVER_OR_UNKNOWN"); - - CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); - CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update); - - CFRelease(formatter); - - return [(NSString *) formatted autorelease]; -} - bool isSectionVisible(NSString *section) { NSDictionary *metadata([Sections_ objectForKey:(section ?: @"")]); NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); @@ -848,6 +850,25 @@ static NSString *CYHex(NSData *data, bool reverse = false) { return [NSString stringWithUTF8String:string]; } +static NSString *VerifySource(NSString *href) { + static RegEx href_r("(http(s?)://|file:///)[^# ]*"); + if (!href_r(href)) { + [[[[UIAlertView alloc] + initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("INVALID_URL")] + message:UCLocalize("INVALID_URL_EX") + delegate:nil + cancelButtonTitle:UCLocalize("OK") + otherButtonTitles:nil + ] autorelease] show]; + + return nil; + } + + if (![href hasSuffix:@"/"]) + href = [href stringByAppendingString:@"/"]; + return href; +} + @class Cydia; /* Delegate Prototypes {{{ */ @@ -863,6 +884,16 @@ static NSString *CYHex(NSData *data, bool reverse = false) { @class CYPackageController; +@protocol SourceDelegate +- (void) setFetch:(NSNumber *)fetch; +@end + +@protocol FetchDelegate +- (bool) isSourceCancelled; +- (void) startSourceFetch:(NSString *)uri; +- (void) stopSourceFetch:(NSString *)uri; +@end + @protocol CydiaDelegate - (void) returnToCydia; - (void) saveState; @@ -874,13 +905,14 @@ static NSString *CYHex(NSData *data, bool reverse = false) { - (void) removePackage:(Package *)package; - (void) beginUpdate; - (BOOL) updating; +- (bool) requestUpdate; - (void) distUpgrade; - (void) loadData; - (void) updateData; - (void) _saveConfig; - (void) syncData; - (void) addSource:(NSDictionary *)source; -- (void) addTrivialSource:(NSString *)href; +- (BOOL) addTrivialSource:(NSString *)href; - (UIProgressHUD *) addProgressHUD; - (void) removeProgressHUD:(UIProgressHUD *)hud; - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; @@ -888,67 +920,88 @@ static NSString *CYHex(NSData *data, bool reverse = false) { @end /* }}} */ -/* Status Delegation {{{ */ -class Status : +/* CancelStatus {{{ */ +class CancelStatus : public pkgAcquireStatus { private: - _transient NSObject *delegate_; bool cancelled_; public: - Status() : - delegate_(nil), + CancelStatus() : cancelled_(false) { } - void setDelegate(NSObject *delegate) { - delegate_ = delegate; + virtual bool MediaChange(std::string media, std::string drive) { + return false; } - NSObject *getDelegate() const { - return delegate_; + virtual void IMSHit(pkgAcquire::ItemDesc &desc) { + Done(desc); } - virtual bool MediaChange(std::string media, std::string drive) { - return false; + virtual bool Pulse_(pkgAcquire *Owner) = 0; + + virtual bool Pulse(pkgAcquire *Owner) { + if (pkgAcquireStatus::Pulse(Owner) && Pulse_(Owner)) + return true; + else { + cancelled_ = true; + return false; + } + } + + _finline bool WasCancelled() const { + return cancelled_; + } +}; +/* }}} */ +/* DelegateStatus {{{ */ +class CydiaStatus : + public CancelStatus +{ + private: + _transient NSObject *delegate_; + + public: + CydiaStatus() : + delegate_(nil) + { } - virtual void IMSHit(pkgAcquire::ItemDesc &item) { - Done(item); + void setDelegate(NSObject *delegate) { + delegate_ = delegate; } - virtual void Fetch(pkgAcquire::ItemDesc &item) { - NSString *name([NSString stringWithUTF8String:item.ShortDesc.c_str()]); - CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), name] ofType:kCydiaProgressEventTypeStatus forItem:item]); + virtual void Fetch(pkgAcquire::ItemDesc &desc) { + NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); + CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; } - virtual void Done(pkgAcquire::ItemDesc &item) { - NSString *name([NSString stringWithUTF8String:item.ShortDesc.c_str()]); - CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:Colon_, UCLocalize("DONE"), name] ofType:kCydiaProgressEventTypeStatus forItem:item]); + virtual void Done(pkgAcquire::ItemDesc &desc) { + NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); + CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:Colon_, UCLocalize("DONE"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; } - virtual void Fail(pkgAcquire::ItemDesc &item) { + virtual void Fail(pkgAcquire::ItemDesc &desc) { if ( - item.Owner->Status == pkgAcquire::Item::StatIdle || - item.Owner->Status == pkgAcquire::Item::StatDone + desc.Owner->Status == pkgAcquire::Item::StatIdle || + desc.Owner->Status == pkgAcquire::Item::StatDone ) return; - std::string &error(item.Owner->ErrorText); + std::string &error(desc.Owner->ErrorText); if (error.empty()) return; - CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError forItem:item]); + CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError forItemDesc:desc]); [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; } - virtual bool Pulse(pkgAcquire *Owner) { - bool value = pkgAcquireStatus::Pulse(Owner); - + virtual bool Pulse_(pkgAcquire *Owner) { double percent( double(CurrentBytes + CurrentItems) / double(TotalBytes + TotalItems) @@ -962,16 +1015,7 @@ class Status : [NSNumber numberWithDouble:CurrentCPS], @"Speed", nil] waitUntilDone:YES]; - if (value && ![delegate_ isProgressCancelled]) - return true; - else { - cancelled_ = true; - return false; - } - } - - _finline bool WasCancelled() const { - return cancelled_; + return ![delegate_ isProgressCancelled]; } virtual void Start() { @@ -991,9 +1035,10 @@ typedef std::map< unsigned long, _H > SourceMap; @interface Database : NSObject { NSZone *zone_; - apr_pool_t *pool_; + CYPool pool_; unsigned era_; + _H delock_; pkgCacheFile cache_; pkgDepCache::Policy *policy_; @@ -1007,12 +1052,12 @@ typedef std::map< unsigned long, _H > SourceMap; SourceMap sourceMap_; _H sourceList_; - CFMutableArrayRef packages_; + _H packages_; _transient NSObject *delegate_; _transient NSObject *progress_; - Status status_; + CydiaStatus status_; int cydiafd_; int statusfd_; @@ -1023,6 +1068,7 @@ typedef std::map< unsigned long, _H > SourceMap; + (Database *) sharedInstance; - (unsigned) era; +- (bool) hasPackages; - (void) _readCydia:(NSNumber *)fd; - (void) _readStatus:(NSNumber *)fd; @@ -1049,7 +1095,7 @@ typedef std::map< unsigned long, _H > SourceMap; - (bool) upgrade; - (void) update; -- (void) updateWithStatus:(Status &)status; +- (void) updateWithStatus:(CancelStatus &)status; - (void) setDelegate:(NSObject *)delegate; @@ -1057,11 +1103,109 @@ typedef std::map< unsigned long, _H > SourceMap; - (NSObject *) progressDelegate; - (Source *) getSource:(pkgCache::PkgFileIterator)file; +- (void) setFetch:(bool)fetch forURI:(const char *)uri; +- (void) resetFetch; - (NSString *) mappedSectionForPointer:(const char *)pointer; @end /* }}} */ +/* SourceStatus {{{ */ +class SourceStatus : + public CancelStatus +{ + private: + _transient NSObject *delegate_; + _transient Database *database_; + std::set fetches_; + + public: + SourceStatus(NSObject *delegate, Database *database) : + delegate_(delegate), + database_(database) + { + } + + void Set(bool fetch, const std::string &uri) { + if (fetch) { + if (!fetches_.insert(uri).second) + return; + } else { + if (fetches_.erase(uri) == 0) + return; + } + + //printf("Set(%s, %s)\n", fetch ? "true" : "false", uri.c_str()); + + auto slash(uri.rfind('/')); + if (slash != std::string::npos) + [database_ setFetch:fetch forURI:uri.substr(0, slash).c_str()]; + } + + _finline void Set(bool fetch, pkgAcquire::Item *item) { + /*unsigned long ID(fetch ? 1 : 0); + if (item->ID == ID) + return; + item->ID = ID;*/ + Set(fetch, item->DescURI()); + } + + void Log(const char *tag, pkgAcquire::Item *item) { + //printf("%s(%s) S:%u Q:%u\n", tag, item->DescURI().c_str(), item->Status, item->QueueCounter); + } + + virtual void Fetch(pkgAcquire::ItemDesc &desc) { + Log("Fetch", desc.Owner); + Set(true, desc.Owner); + } + + virtual void Done(pkgAcquire::ItemDesc &desc) { + Log("Done", desc.Owner); + Set(false, desc.Owner); + } + + virtual void Fail(pkgAcquire::ItemDesc &desc) { + Log("Fail", desc.Owner); + Set(false, desc.Owner); + } + + virtual bool Pulse_(pkgAcquire *Owner) { + std::set fetches; + for (pkgAcquire::ItemCIterator item(Owner->ItemsBegin()); item != Owner->ItemsEnd(); ++item) { + bool fetch; + if ((*item)->QueueCounter == 0) + fetch = false; + else switch ((*item)->Status) { + case pkgAcquire::Item::StatFetching: + fetches.insert((*item)->DescURI()); + fetch = true; + break; + + default: + fetch = false; + break; + } + + Log(fetch ? "Pulse" : "Pulse", *item); + Set(fetch, *item); + } + + std::vector stops; + std::set_difference(fetches_.begin(), fetches_.end(), fetches.begin(), fetches.end(), std::back_insert_iterator>(stops)); + for (std::vector::const_iterator stop(stops.begin()); stop != stops.end(); ++stop) { + //printf("Stop(%s)\n", stop->c_str()); + Set(false, *stop); + } + + return ![delegate_ isSourceCancelled]; + } + + virtual void Stop() { + pkgAcquireStatus::Stop(); + [database_ resetFetch]; + } +}; +/* }}} */ /* ProgressEvent Implementation {{{ */ @implementation CydiaProgressEvent @@ -1075,10 +1219,10 @@ typedef std::map< unsigned long, _H > SourceMap; return event; } -+ (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forItem:(pkgAcquire::ItemDesc &)item { ++ (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forItemDesc:(pkgAcquire::ItemDesc &)desc { CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); - NSString *description([NSString stringWithUTF8String:item.Description.c_str()]); + NSString *description([NSString stringWithUTF8String:desc.Description.c_str()]); NSArray *fields([description componentsSeparatedByString:@" "]); [event setItem:fields]; @@ -1087,7 +1231,7 @@ typedef std::map< unsigned long, _H > SourceMap; [event setVersion:[fields objectAtIndex:3]]; } - [event setURL:[NSString stringWithUTF8String:item.URI.c_str()]]; + [event setURL:[NSString stringWithUTF8String:desc.URI.c_str()]]; return event; } @@ -1213,14 +1357,14 @@ struct PackageValue : char version_[8]; char name_[]; -}; +} _packed; struct MetaValue : Cytore::Block { uint32_t active_; Cytore::Offset packages_[1 << 16]; -}; +} _packed; static Cytore::File MetaFile_; // }}} @@ -1231,7 +1375,7 @@ static PackageValue *PackageFind(const char *name, size_t length, bool *fail = N PackageValue *metadata; Cytore::Offset *offset(&MetaFile_->packages_[nhash.u16[0]]); - offset: if (offset->IsNull()) { + for (;; offset = &metadata->next_) { if (offset->IsNull()) { *offset = MetaFile_.New(length + 1); metadata = &MetaFile_.Get(*offset); @@ -1243,16 +1387,18 @@ static PackageValue *PackageFind(const char *name, size_t length, bool *fail = N memset(metadata, 0, sizeof(*metadata)); } - memcpy(metadata->name_, name, length + 1); + memcpy(metadata->name_, name, length); + metadata->name_[length] = '\0'; metadata->nhash_ = nhash.u16[1]; } else { metadata = &MetaFile_.Get(*offset); - - if (metadata->nhash_ != nhash.u16[1] || strncmp(metadata->name_, name, length + 1) != 0) { - offset = &metadata->next_; - goto offset; - } - } + if (metadata->nhash_ != nhash.u16[1]) + continue; + if (strncmp(metadata->name_, name, length) != 0) + continue; + if (metadata->name_[length] != '\0') + continue; + } break; } return metadata; } @@ -1301,6 +1447,30 @@ static void PackageImport(const void *key, const void *value, void *context) { } // }}} +static NSDate *GetStatusDate() { + return [[[NSFileManager defaultManager] attributesOfItemAtPath:@"/var/lib/dpkg/status" error:NULL] fileModificationDate]; +} + +static void SaveConfig(NSObject *lock) { + @synchronized (lock) { + _trace(); + MetaFile_.Sync(); + _trace(); + } + + CFPreferencesSetMultiple((CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys: + Values_, @"CydiaValues", + Sections_, @"CydiaSections", + (id) Sources_, @"CydiaSources", + Version_, @"CydiaVersion", + nil], NULL, CFSTR("com.saurik.Cydia"), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost); + + if (!CFPreferencesAppSynchronize(CFSTR("com.saurik.Cydia"))) + NSLog(@"CFPreferencesAppSynchronize(com.saurik.Cydia) == false"); + + CydiaWriteSources(); +} + /* Source Class {{{ */ @interface Source : NSObject { unsigned era_; @@ -1326,9 +1496,13 @@ static void PackageImport(const void *key, const void *value, void *context) { _H record_; BOOL trusted_; + + std::set fetches_; + std::set files_; + _transient NSObject *delegate_; } -- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(apr_pool_t *)pool; +- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool; - (NSComparisonResult) compareByName:(Source *)source; @@ -1355,6 +1529,9 @@ static void PackageImport(const void *key, const void *value, void *context) { - (NSString *) defaultIcon; - (NSURL *) iconURL; +- (void) setFetch:(bool)fetch forURI:(const char *)uri; +- (void) resetFetch; + @end @implementation Source @@ -1408,7 +1585,7 @@ static void PackageImport(const void *key, const void *value, void *context) { return index_; } -- (void) setMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { +- (void) setMetaIndex:(metaIndex *)index inPool:(CYPool *)pool { trusted_ = index->IsTrusted(); uri_.set(pool, index->GetURI()); @@ -1417,7 +1594,22 @@ static void PackageImport(const void *key, const void *value, void *context) { debReleaseIndex *dindex(dynamic_cast(index)); if (dindex != NULL) { - base_.set(pool, dindex->MetaIndexURI("")); + std::string file(dindex->MetaIndexURI("")); + base_.set(pool, file); + + pkgAcquire acquire; + _profile(Source$setMetaIndex$GetIndexes) + dindex->GetIndexes(&acquire, true); + _end + _profile(Source$setMetaIndex$DescURI) + for (pkgAcquire::ItemIterator item(acquire.ItemsBegin()); item != acquire.ItemsEnd(); item++) { + std::string file((*item)->DescURI()); + auto slash(file.rfind('/')); + if (slash == std::string::npos) + continue; + files_.insert(file.substr(0, slash)); + } + _end FileFd fd; if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) @@ -1466,13 +1658,15 @@ static void PackageImport(const void *key, const void *value, void *context) { authority_ = [url path]; } -- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(apr_pool_t *)pool { +- (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool { if ((self = [super init]) != nil) { era_ = [database era]; database_ = database; index_ = index; + _profile(Source$initWithMetaIndex$setMetaIndex) [self setMetaIndex:index inPool:pool]; + _end } return self; } @@ -1536,14 +1730,10 @@ static void PackageImport(const void *key, const void *value, void *context) { if (record_ == nil) return; else if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) { - if (![sections containsObject:section]) { + if (![sections containsObject:section]) [sections addObject:section]; - Changed_ = true; - } - } else { + } else [record_ setObject:[NSMutableArray arrayWithObject:section] forKey:@"Sections"]; - Changed_ = true; - } } - (bool) addSection:(NSString *)section { @@ -1559,10 +1749,8 @@ static void PackageImport(const void *key, const void *value, void *context) { return; if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) - if ([sections containsObject:section]) { + if ([sections containsObject:section]) [sections removeObject:section]; - Changed_ = true; - } } - (bool) removeSection:(NSString *)section { @@ -1575,7 +1763,6 @@ static void PackageImport(const void *key, const void *value, void *context) { - (void) _remove { [Sources_ removeObjectForKey:[self key]]; - Changed_ = true; } - (bool) remove { @@ -1653,6 +1840,31 @@ static void PackageImport(const void *key, const void *value, void *context) { return defaultIcon_; } +- (void) setDelegate:(NSObject *)delegate { + delegate_ = delegate; +} + +- (bool) fetch { + return !fetches_.empty(); +} + +- (void) setFetch:(bool)fetch forURI:(const char *)uri { + if (!fetch) { + if (fetches_.erase(uri) == 0) + return; + } else if (files_.find(uri) == files_.end()) + return; + else if (!fetches_.insert(uri).second) + return; + + [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:[self fetch]] waitUntilDone:NO]; +} + +- (void) resetFetch { + fetches_.clear(); + [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:NO]; +} + @end /* }}} */ /* CydiaOperation Class {{{ */ @@ -1828,13 +2040,13 @@ struct ParsedPackage { @interface Package : NSObject { uint32_t era_ : 25; - uint32_t role_ : 3; + @public uint32_t role_ : 3; uint32_t essential_ : 1; uint32_t obsolete_ : 1; uint32_t ignored_ : 1; uint32_t pooled_ : 1; - apr_pool_t *pool_; + CYPool *pool_; uint32_t rank_; @@ -1846,9 +2058,11 @@ struct ParsedPackage { CYString id_; CYString name_; + CYString transform_; CYString latest_; CYString installed_; + time_t upgraded_; const char *section_; _transient NSString *section$_; @@ -1861,8 +2075,10 @@ struct ParsedPackage { _H tags_; } -- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; -+ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; +- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; ++ (Package *) newPackageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; + ++ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; - (pkgCache::PkgIterator) iterator; - (void) parse; @@ -1893,7 +2109,6 @@ struct ParsedPackage { - (NSString *) installed; - (BOOL) uninstalled; -- (BOOL) valid; - (BOOL) upgradableAndEssential:(BOOL)essential; - (BOOL) essential; - (BOOL) broken; @@ -1938,12 +2153,6 @@ struct ParsedPackage { - (void) install; - (void) remove; -- (bool) isUnfilteredAndSearchedForBy:(NSArray *)query; -- (bool) isUnfilteredAndSelectedForBy:(NSString *)search; -- (bool) isInstalledAndUnfiltered:(NSNumber *)number; -- (bool) isVisibleInSection:(NSString *)section source:(Source *)source; -- (bool) isVisibleInSource:(Source *)source; - @end uint32_t PackageChangesRadix(Package *self, void *) { @@ -1973,9 +2182,11 @@ uint32_t PackageChangesRadix(Package *self, void *) { return _not(uint32_t) - value.key; } +CYString &(*PackageName)(Package *self, SEL sel); + uint32_t PackagePrefixRadix(Package *self, void *context) { size_t offset(reinterpret_cast(context)); - CYString &name([self cyname]); + CYString &name(PackageName(self, @selector(cyname))); size_t size(name.size()); if (size == 0) @@ -2028,46 +2239,50 @@ uint32_t PackagePrefixRadix(Package *self, void *context) { return OSSwapInt32(*reinterpret_cast(data)); } -CYString &(*PackageName)(Package *self, SEL sel); - -CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { +CFComparisonResult StringNameCompare(CFStringRef lhn, CFStringRef rhn, size_t length) { _profile(PackageNameCompare) - CYString &lhi(PackageName(lhs, @selector(cyname))); - CYString &rhi(PackageName(rhs, @selector(cyname))); - CFStringRef lhn(lhi), rhn(rhi); - if (lhn == NULL) - return rhn == NULL ? NSOrderedSame : NSOrderedAscending; + return rhn == NULL ? kCFCompareEqualTo : kCFCompareLessThan; else if (rhn == NULL) - return NSOrderedDescending; + return kCFCompareGreaterThan; + + CFIndex length(CFStringGetLength(lhn)); _profile(PackageNameCompare$NumbersLast) - if (!lhi.empty() && !rhi.empty()) { + if (length != 0 && CFStringGetLength(rhn) != 0) { UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) - return lha ? NSOrderedAscending : NSOrderedDescending; + return lha ? kCFCompareLessThan : kCFCompareGreaterThan; } _end - CFIndex length = CFStringGetLength(lhn); - _profile(PackageNameCompare$Compare) - return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, Locale_); + return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, (CFLocaleRef) (id) CollationLocale_); _end _end } -CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *context) { - return PackageNameCompare(*lhs, *rhs, context); +_finline CFComparisonResult StringNameCompare(NSString *lhn, NSString*rhn, size_t length) { + return StringNameCompare((CFStringRef) lhn, (CFStringRef) rhn, length); +} + +CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { + CYString &lhn(PackageName(lhs, @selector(cyname))); + NSString *rhn(PackageName(rhs, @selector(cyname))); + return StringNameCompare(lhn, rhn, lhn.size()); +} + +CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *arg) { + return PackageNameCompare(*lhs, *rhs, arg); } struct PackageNameOrdering : std::binary_function { _finline bool operator ()(Package *lhs, Package *rhs) const { - return PackageNameCompare(lhs, rhs, NULL) == NSOrderedAscending; + return PackageNameCompare(lhs, rhs, NULL) == kCFCompareLessThan; } }; @@ -2079,7 +2294,7 @@ struct PackageNameOrdering : - (void) dealloc { if (!pooled_) - apr_pool_destroy(pool_); + delete pool_; if (parsed_ != NULL) delete parsed_; [super dealloc]; @@ -2137,6 +2352,7 @@ struct PackageNameOrdering : @"state", @"support", @"tags", + @"upgraded", @"warnings", nil]; } @@ -2239,15 +2455,7 @@ struct PackageNameOrdering : _end _profile(Package$parse$Tagline) - const char *start, *end; - if (parser->ShortDesc(start, end)) { - const char *stop(reinterpret_cast(memchr(start, '\n', end - start))); - if (stop == NULL) - stop = end; - while (stop != start && stop[-1] == '\r') - --stop; - parsed->tagline_.set(pool_, start, stop - start); - } + parsed->tagline_.set(pool_, parser->ShortDesc()); _end _profile(Package$parse$Retain) @@ -2261,11 +2469,11 @@ struct PackageNameOrdering : _end } } -- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { +- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { if ((self = [super init]) != nil) { _profile(Package$initWithVersion) if (pool == NULL) - apr_pool_create(&pool_, NULL); + pool_ = new CYPool(); else { pool_ = pool; pooled_ = true; @@ -2276,20 +2484,15 @@ struct PackageNameOrdering : version_ = version; - pkgCache::PkgIterator iterator(version.ParentPkg()); + pkgCache::PkgIterator iterator(version_.ParentPkg()); iterator_ = iterator; _profile(Package$initWithVersion$Version) - if (!version_.end()) - file_ = version_.FileList(); - else { - pkgCache &cache([database_ cache]); - file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); - } + file_ = version_.FileList(); _end _profile(Package$initWithVersion$Cache) - name_.set(NULL, iterator.Display()); + name_.set(NULL, version_.Display()); latest_.set(NULL, StripVersion_(version_.VerStr())); @@ -2298,8 +2501,66 @@ struct PackageNameOrdering : installed_.set(NULL, StripVersion_(current.VerStr())); _end + _profile(Package$initWithVersion$Transliterate) do { + if (CollationTransl_ == NULL) + break; + if (name_.empty()) + break; + + _profile(Package$initWithVersion$Transliterate$utf8) + const uint8_t *data(reinterpret_cast(name_.data())); + for (size_t i(0), e(name_.size()); i != e; ++i) + if (data[i] >= 0x80) + goto extended; + break; extended:; + _end + + UErrorCode code(U_ZERO_ERROR); + int32_t length; + + _profile(Package$initWithVersion$Transliterate$u_strFromUTF8WithSub) + CollationString_.resize(name_.size()); + u_strFromUTF8WithSub(&CollationString_[0], CollationString_.size(), &length, name_.data(), name_.size(), 0xfffd, NULL, &code); + if (!U_SUCCESS(code)) + break; + CollationString_.resize(length); + _end + + _profile(Package$initWithVersion$Transliterate$utrans_trans) + length = CollationString_.size(); + utrans_trans(CollationTransl_, reinterpret_cast(&CollationString_), &CollationUCalls_, 0, &length, &code); + if (!U_SUCCESS(code)) + break; + _assert(CollationString_.size() == length); + _end + + _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$preflight) + u_strToUTF8WithSub(NULL, 0, &length, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); + if (code == U_BUFFER_OVERFLOW_ERROR) + code = U_ZERO_ERROR; + else if (!U_SUCCESS(code)) + break; + _end + + char *transform; + _profile(Package$initWithVersion$Transliterate$apr_palloc) + transform = pool_->malloc(length); + _end + _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$transform) + u_strToUTF8WithSub(transform, length, NULL, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); + if (!U_SUCCESS(code)) + break; + _end + + transform_.set(NULL, transform, length); + } while (false); _end + _profile(Package$initWithVersion$Tags) +#ifdef __arm64__ + pkgCache::TagIterator tag(version_.TagList()); +#else pkgCache::TagIterator tag(iterator.TagList()); +#endif if (!tag.end()) { tags_ = [NSMutableArray arrayWithCapacity:8]; @@ -2337,13 +2598,21 @@ struct PackageNameOrdering : _profile(Package$initWithVersion$Metadata) const char *mixed(iterator.Name()); size_t size(strlen(mixed)); - char lower[size + 1]; + static const size_t prefix(sizeof("/var/lib/dpkg/info/") - 1); + char lower[prefix + size + 5 + 1]; for (size_t i(0); i != size; ++i) - lower[i] = mixed[i] | 0x20; - lower[size] = '\0'; + lower[prefix + i] = mixed[i] | 0x20; + + if (!installed_.empty()) { + memcpy(lower, "/var/lib/dpkg/info/", prefix); + memcpy(lower + prefix + size, ".list", 6); + struct stat info; + if (stat(lower, &info) != -1) + upgraded_ = info.st_birthtime; + } - PackageValue *metadata(PackageFind(lower, size)); + PackageValue *metadata(PackageFind(lower + prefix, size)); metadata_ = metadata; id_.set(NULL, metadata->name_, size); @@ -2378,7 +2647,7 @@ struct PackageNameOrdering : _end } return self; } -+ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { ++ (Package *) newPackageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { pkgCache::VerIterator version; _profile(Package$packageWithIterator$GetCandidateVer) @@ -2403,17 +2672,33 @@ struct PackageNameOrdering : ]; _end - _profile(Package$packageWithIterator$Autorelease) - package = [package autorelease]; - _end - return package; } +// XXX: just in case a Cydia extension is using this (I bet this is unlikely, though, due to CYPool?) ++ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { + return [[self newPackageWithIterator:iterator withZone:zone inPool:pool database:database] autorelease]; +} + - (pkgCache::PkgIterator) iterator { return iterator_; } +- (NSArray *) downgrades { + NSMutableArray *versions([NSMutableArray arrayWithCapacity:4]); + + for (auto version(iterator_.VersionList()); !version.end(); ++version) { + if (version == version_) + continue; + Package *package([[[Package allocWithZone:NULL] initWithVersion:version withZone:NULL inPool:NULL database:database_] autorelease]); + if ([package source] == nil) + continue; + [versions addObject:package]; + } + + return versions; +} + - (NSString *) section { if (section$_ == nil) { if (section_ == NULL) @@ -2433,7 +2718,10 @@ struct PackageNameOrdering : } - (NSString *) longSection { - return LocalizeSection([self section]); + if (NSString *section = [self section]) + return LocalizeSection(section); + else + return nil; } - (NSString *) shortSection { @@ -2503,23 +2791,12 @@ struct PackageNameOrdering : @synchronized (database_) { pkgRecords::Parser &parser([database_ records]->Lookup(file_)); - - const char *start, *end; - if (!parser.ShortDesc(start, end)) + std::string value(parser.ShortDesc()); + if (value.empty()) return nil; - - if (end - start > 200) - end = start + 200; - - /* - if (const char *stop = reinterpret_cast(memchr(start, '\n', end - start))) - end = stop; - - while (end != start && end[-1] == '\r') - --end; - */ - - return [(id) CYStringCreate(start, end - start) autorelease]; + if (value.size() > 200) + value.resize(200); + return [(id) CYStringCreate(value) autorelease]; } } - (unichar) index { @@ -2571,17 +2848,13 @@ struct PackageNameOrdering : return installed_.empty(); } -- (BOOL) valid { - return !version_.end(); -} - - (BOOL) upgradableAndEssential:(BOOL)essential { _profile(Package$upgradableAndEssential) pkgCache::VerIterator current(iterator_.CurrentVer()); if (current.end()) return essential && essential_; else - return !version_.end() && version_ != current; + return version_ != current; _end } @@ -2641,7 +2914,7 @@ struct PackageNameOrdering : - (BOOL) hasMode { @synchronized (database_) { if ([database_ era] != era_ || iterator_.end()) - return nil; + return NO; pkgDepCache::StateCache &state([database_ cache][iterator_]); return state.Mode != pkgDepCache::ModeKeep; @@ -2707,7 +2980,7 @@ struct PackageNameOrdering : if ([dicon hasPrefix:@"file:///"]) icon = [UIImage imageAtPath:[[dicon substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; if (icon == nil) - icon = [UIImage applicationImageNamed:@"unknown.png"]; + icon = [UIImage imageNamed:@"unknown.png"]; return icon; } @@ -2792,6 +3065,10 @@ struct PackageNameOrdering : } } - (NSArray *) warnings { +@synchronized (database_) { + if ([database_ era] != era_ || file_.end()) + return nil; + NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); const char *name(iterator_.Name()); @@ -2812,6 +3089,8 @@ struct PackageNameOrdering : bool user = false; bool _private = false; bool stash = false; + bool dbstash = false; + bool dsstore = false; bool repository = [[self section] isEqualToString:@"Repositories"]; @@ -2825,6 +3104,10 @@ struct PackageNameOrdering : _private = true; else if (!stash && [file isEqualToString:@"/var/stash"]) stash = true; + else if (!dbstash && [file isEqualToString:@"/var/db/stash"]) + dbstash = true; + else if (!dsstore && [file hasSuffix:@"/.DS_Store"]) + dsstore = true; /* XXX: this is not sensitive enough. only some folders are valid. */ if (cydia && !repository) @@ -2835,23 +3118,29 @@ struct PackageNameOrdering : [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; if (stash) [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; + if (dbstash) + [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/db/stash"]]; + if (dsstore) + [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @".DS_Store"]]; } return [warnings count] == 0 ? nil : warnings; -} +} } - (NSArray *) applications { NSString *me([[NSBundle mainBundle] bundleIdentifier]); NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); - static Pcre application_r("^/Applications/(.*)\\.app/Info.plist$"); + static RegEx application_r("/Applications/(.*)\\.app/Info.plist"); if (NSArray *files = [self files]) for (NSString *file in files) if (application_r(file)) { NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); + if (info == nil) + continue; NSString *id([info objectForKey:@"CFBundleIdentifier"]); - if ([id isEqualToString:me]) + if (id == nil || [id isEqualToString:me]) continue; NSString *display([info objectForKey:@"CFBundleDisplayName"]); @@ -2889,7 +3178,15 @@ struct PackageNameOrdering : return source_ == (Source *) [NSNull null] ? nil : source_; } -- (uint32_t) rank { +- (time_t) upgraded { + return upgraded_; +} + +- (uint32_t) recent { + return std::numeric_limits::max() - upgraded_; +} + +- (uint32_t) rank { return rank_; } @@ -2906,6 +3203,7 @@ struct PackageNameOrdering : string = [self name]; length = [string length]; + if (length != 0) for (NSString *term in query) { range = [string rangeOfString:term options:MatchCompareOptions_]; if (range.location != NSNotFound) @@ -2916,6 +3214,7 @@ struct PackageNameOrdering : string = [self id]; length = [string length]; + if (length != 0) for (NSString *term in query) { range = [string rangeOfString:term options:MatchCompareOptions_]; if (range.location != NSNotFound) @@ -2927,6 +3226,7 @@ struct PackageNameOrdering : length = [string length]; NSUInteger stop(std::min(length, 200)); + if (length != 0) for (NSString *term in query) { range = [string rangeOfString:term options:MatchCompareOptions_ range:NSMakeRange(0, stop)]; if (range.location != NSNotFound) @@ -2964,12 +3264,12 @@ struct PackageNameOrdering : } - (void) setIndex:(size_t)index { - if (metadata_->index_ != index) - metadata_->index_ = index; + if (metadata_->index_ != index + 1) + metadata_->index_ = index + 1; } - (CYString &) cyname { - return name_.empty() ? id_ : name_; + return !transform_.empty() ? transform_ : !name_.empty() ? name_ : id_; } - (uint32_t) compareBySection:(NSArray *)sections { @@ -2984,6 +3284,9 @@ struct PackageNameOrdering : - (void) clear { @synchronized (database_) { + if ([database_ era] != era_ || file_.end()) + return; + pkgProblemResolver *resolver = [database_ resolver]; resolver->Clear(iterator_); @@ -2994,11 +3297,15 @@ struct PackageNameOrdering : - (void) install { @synchronized (database_) { + if ([database_ era] != era_ || file_.end()) + return; + pkgProblemResolver *resolver = [database_ resolver]; resolver->Clear(iterator_); resolver->Protect(iterator_); pkgCacheFile &cache([database_ cache]); + cache->SetCandidateVersion(version_); cache->SetReInstall(iterator_, false); cache->MarkInstall(iterator_, false); @@ -3009,6 +3316,9 @@ struct PackageNameOrdering : - (void) remove { @synchronized (database_) { + if ([database_ era] != era_ || file_.end()) + return; + pkgProblemResolver *resolver = [database_ resolver]; resolver->Clear(iterator_); resolver->Remove(iterator_); @@ -3019,68 +3329,11 @@ struct PackageNameOrdering : cache->MarkDelete(iterator_, true); } } -- (bool) isUnfilteredAndSearchedForBy:(NSArray *)query { - _profile(Package$isUnfilteredAndSearchedForBy) - bool value(true); - - _profile(Package$isUnfilteredAndSearchedForBy$Unfiltered) - value &= [self unfiltered]; - _end - - _profile(Package$isUnfilteredAndSearchedForBy$Match) - value &= [self matches:query]; - _end - - return value; - _end -} - -- (bool) isUnfilteredAndSelectedForBy:(NSString *)search { - if ([search length] == 0) - return false; - - _profile(Package$isUnfilteredAndSelectedForBy) - bool value(true); - - _profile(Package$isUnfilteredAndSelectedForBy$Unfiltered) - value &= [self unfiltered]; - _end - - _profile(Package$isUnfilteredAndSelectedForBy$Match) - value &= [[self name] compare:search options:MatchCompareOptions_ range:NSMakeRange(0, [search length])] == NSOrderedSame; - _end - - return value; - _end -} - -- (bool) isInstalledAndUnfiltered:(NSNumber *)number { - return ![self uninstalled] && (![number boolValue] && role_ != 7 || [self unfiltered]); -} - -- (bool) isVisibleInSection:(NSString *)name source:(Source *)source { - NSString *section([self section]); - - return ( - name == nil || - section == nil && [name length] == 0 || - [name isEqualToString:section] - ) && ( - source == nil || - [self source] == source - ) && [self visible]; -} - -- (bool) isVisibleInSource:(Source *)source { - return [self source] == source && [self visible]; -} - @end /* }}} */ /* Section Class {{{ */ @interface Section : NSObject { _H name_; - unichar index_; size_t row_; size_t count_; _H localized_; @@ -3090,9 +3343,9 @@ struct PackageNameOrdering : - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; -- (Section *) initWithIndex:(unichar)index row:(size_t)row; + - (NSString *) name; -- (unichar) index; +- (void) setName:(NSString *)name; - (size_t) row; - (size_t) count; @@ -3138,28 +3391,18 @@ struct PackageNameOrdering : - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { if ((self = [super init]) != nil) { name_ = name; - index_ = '\0'; row_ = row; if (localize) localized_ = LocalizeSection(name_); } return self; } -/* XXX: localize the index thingees */ -- (Section *) initWithIndex:(unichar)index row:(size_t)row { - if ((self = [super init]) != nil) { - name_ = [NSString stringWithCharacters:&index length:1]; - index_ = index; - row_ = row; - } return self; -} - - (NSString *) name { return name_; } -- (unichar) index { - return index_; +- (void) setName:(NSString *)name { + name_ = name; } - (size_t) row { @@ -3213,25 +3456,26 @@ class CydiaLogCleaner : } - (void) releasePackages { - CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast(&CFRelease), NULL); - CFArrayRemoveAllValues(packages_); + packages_ = nil; +} + +- (bool) hasPackages { + return [packages_ count] != 0; } - (void) dealloc { // XXX: actually implement this thing _assert(false); [self releasePackages]; - apr_pool_destroy(pool_); NSRecycleZone(zone_); [super dealloc]; } - (void) _readCydia:(NSNumber *)fd { - __gnu_cxx::stdio_filebuf ib([fd intValue], std::ios::in); - std::istream is(&ib); + boost::fdistream is([fd intValue]); std::string line; - static Pcre finish_r("^finish:([^:]*)$"); + static RegEx finish_r("finish:([^:]*)"); while (std::getline(is, line)) { NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); @@ -3254,12 +3498,11 @@ class CydiaLogCleaner : } - (void) _readStatus:(NSNumber *)fd { - __gnu_cxx::stdio_filebuf ib([fd intValue], std::ios::in); - std::istream is(&ib); + boost::fdistream is([fd intValue]); std::string line; - static Pcre conffile_r("^status: [^ ]* : conffile-prompt : (.*?) *$"); - static Pcre pmstatus_r("^([^:]*):([^:]*):([^:]*):(.*)$"); + static RegEx conffile_r("status: [^ ]* : conffile-prompt : (.*?) *"); + static RegEx pmstatus_r("([^:]*):([^:]*):([^:]*):(.*)"); while (std::getline(is, line)) { NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); @@ -3311,8 +3554,7 @@ class CydiaLogCleaner : } - (void) _readOutput:(NSNumber *)fd { - __gnu_cxx::stdio_filebuf ib([fd intValue], std::ios::in); - std::istream is(&ib); + boost::fdistream is([fd intValue]); std::string line; while (std::getline(is, line)) { @@ -3339,8 +3581,12 @@ class CydiaLogCleaner : @synchronized (self) { if (static_cast(cache_) == NULL) return nil; - pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); - return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:NULL database:self]; + pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String] +#ifdef __arm64__ + , "any" +#endif + )); + return iterator.end() ? nil : [[Package newPackageWithIterator:iterator withZone:NULL inPool:NULL database:self] autorelease]; } } - (id) init { @@ -3352,15 +3598,7 @@ class CydiaLogCleaner : lock_ = NULL; zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); - apr_pool_create(&pool_, NULL); - - size_t capacity(MetaFile_->active_); - if (capacity == 0) - capacity = 16384; - else - capacity += 1024; - packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); sourceList_ = [NSMutableArray arrayWithCapacity:16]; int fds[2]; @@ -3429,7 +3667,7 @@ class CydiaLogCleaner : } - (NSArray *) packages { - return (NSArray *) packages_; + return packages_; } - (NSArray *) sources { @@ -3461,7 +3699,7 @@ class CydiaLogCleaner : lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); - static Pcre no_pubkey("^GPG error:.* NO_PUBKEY .*$"); + static RegEx no_pubkey("GPG error:.* NO_PUBKEY .*"); if (warning && no_pubkey(error.c_str())) continue; @@ -3475,6 +3713,31 @@ class CydiaLogCleaner : return [self popErrorWithTitle:title] || !success; } +- (bool) popErrorWithTitle:(NSString *)title forReadList:(pkgSourceList &)list { + if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) + return true; + return false; + + list.Reset(); + + bool error(false); + + if (access("/etc/apt/sources.list", F_OK) == 0) + error |= [self popErrorWithTitle:title forOperation:list.ReadAppend("/etc/apt/sources.list")]; + + std::string base("/etc/apt/sources.list.d"); + if (DIR *sources = opendir(base.c_str())) { + while (dirent *source = readdir(sources)) + if (source->d_name[0] != '.' && source->d_namlen > 5 && strcmp(source->d_name + source->d_namlen - 5, ".list") == 0 && strcmp(source->d_name, "cydia.list") != 0) + error |= [self popErrorWithTitle:title forOperation:list.ReadAppend((base + "/" + source->d_name).c_str())]; + closedir(sources); + } + + error |= [self popErrorWithTitle:title forOperation:list.ReadAppend(SOURCES_LIST)]; + + return error; +} + - (void) reloadDataWithInvocation:(NSInvocation *)invocation { @synchronized (self) { ++era_; @@ -3502,7 +3765,8 @@ class CydiaLogCleaner : cache_.Close(); - apr_pool_clear(pool_); + pool_.~CYPool(); + new (&pool_) CYPool(); NSRecycleZone(zone_); zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); @@ -3517,19 +3781,28 @@ class CydiaLogCleaner : NSString *title(UCLocalize("DATABASE")); list_ = new pkgSourceList(); - if ([self popErrorWithTitle:title forOperation:list_->ReadMainList()]) + _profile(reloadDataWithInvocation$ReadMainList) + if ([self popErrorWithTitle:title forReadList:*list_]) return; + _end + _profile(reloadDataWithInvocation$Source$initWithMetaIndex) for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { - Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:pool_] autorelease]); + Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:&pool_] autorelease]); [sourceList_ addObject:object]; } + _end _trace(); OpProgress progress; + bool opened; open: - if (!cache_.Open(progress, true)) { - // XXX: what if there are errors, but Open() == true? this should be merged with popError: + delock_ = GetStatusDate(); + _profile(reloadDataWithInvocation$pkgCacheFile) + opened = cache_.Open(progress, false); + _end + if (!opened) { + // XXX: this block should probably be merged with popError: in some way while (!_error->empty()) { std::string error; bool warning(!_error->PopMessage(error)); @@ -3557,7 +3830,8 @@ class CydiaLogCleaner : } return; - } + } else if ([self popErrorWithTitle:title forOperation:true]) + return; _trace(); unlink("/tmp/cydia.chk"); @@ -3575,20 +3849,26 @@ class CydiaLogCleaner : return; } + _profile(reloadDataWithInvocation$pkgApplyStatus) if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) return; + _end if (cache_->BrokenCount() != 0) { + _profile(pkgApplyStatus$pkgFixBroken) if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) return; + _end if (cache_->BrokenCount() != 0) { [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("STILL_BROKEN_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; return; } + _profile(pkgApplyStatus$pkgMinimizeUpgrade) if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) return; + _end } for (Source *object in (id) sourceList_) { @@ -3604,54 +3884,111 @@ class CydiaLogCleaner : } { - /*std::vector packages; - packages.reserve(std::max(10000U, [packages_ count] + 1000)); - packages_ = nil;*/ + size_t capacity(MetaFile_->active_); + if (capacity == 0) + capacity = 128*1024; + else + capacity += 1024; - _trace(); + std::vector packages; + packages.reserve(capacity); + size_t lost(0); + size_t last(0); + _profile(reloadDataWithInvocation$packageWithIterator) for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) - if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:pool_ database:self]) - //packages.push_back(package); - CFArrayAppendValue(packages_, CFRetain(package)); + if (Package *package = [Package newPackageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self]) { + if (unsigned index = package.metadata->index_) { + --index; + if (packages.size() == index) { + packages.push_back(package); + } else if (packages.size() <= index) { + packages.resize(index + 1, nil); + packages[index] = package; + continue; + } else { + std::swap(package, packages[index]); + if (package != nil) { + if (package.metadata->index_ == index + 1) + ++lost; + goto lost; + } + if (last != index) + continue; + } + } else { + ++lost; + lost: if (last == packages.size()) + packages.push_back(package); + else + packages[last] = package; + ++last; + } - _trace(); + for (; last != packages.size(); ++last) + if (packages[last] == nil) + break; + } + _end - /*if (packages.empty()) - packages_ = [[NSArray alloc] init]; - else - packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; - _trace();*/ + for (size_t next(last + 1); last != packages.size(); ++last, ++next) { + while (true) { + if (next == packages.size()) + goto done; + if (packages[next] != nil) + break; + ++next; + } - [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast(&PackagePrefixRadix) withContext:reinterpret_cast(16)]; - [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast(&PackagePrefixRadix) withContext:reinterpret_cast(4)]; - [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast(&PackagePrefixRadix) withContext:reinterpret_cast(0)]; + std::swap(packages[last], packages[next]); + } done:; - /*_trace(); - PrintTimes(); - _trace();*/ + packages.resize(last); - _trace(); + if (lost > 128) { + NSLog(@"lost = %zu", lost); + + _profile(reloadDataWithInvocation$radix$8) + CYRadixSortUsingFunction(packages.data(), packages.size(), reinterpret_cast(&PackagePrefixRadix), reinterpret_cast(8)); + _end - /*if (!packages.empty()) - CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast(&PackageNameCompare_), NULL);*/ - //std::sort(packages.begin(), packages.end(), PackageNameOrdering()); + _profile(reloadDataWithInvocation$radix$4) + CYRadixSortUsingFunction(packages.data(), packages.size(), reinterpret_cast(&PackagePrefixRadix), reinterpret_cast(4)); + _end - //CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast(&PackageNameCompare), NULL); + _profile(reloadDataWithInvocation$radix$0) + CYRadixSortUsingFunction(packages.data(), packages.size(), reinterpret_cast(&PackagePrefixRadix), reinterpret_cast(0)); + _end + } - CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast(&PackageNameCompare), NULL); + _profile(reloadDataWithInvocation$insertion) + CYArrayInsertionSortValues(packages.data(), packages.size(), &PackageNameCompare, NULL); + _end - //[packages_ sortUsingFunction:reinterpret_cast(&PackageNameCompare) context:NULL]; + packages_ = [[[NSArray alloc] initWithObjects:packages.data() count:packages.size()] autorelease]; - _trace(); + /*_profile(reloadDataWithInvocation$CFQSortArray) + CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast(&PackageNameCompare_), NULL); + _end*/ - size_t count(CFArrayGetCount(packages_)); - MetaFile_->active_ = count; + /*_profile(reloadDataWithInvocation$stdsort) + std::sort(packages.begin(), packages.end(), PackageNameOrdering()); + _end*/ - for (size_t index(0); index != count; ++index) - [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; + /*_profile(reloadDataWithInvocation$CFArraySortValues) + CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast(&PackageNameCompare), NULL); + _end*/ - _trace(); + /*_profile(reloadDataWithInvocation$sortUsingFunction) + [packages_ sortUsingFunction:reinterpret_cast(&PackageNameCompare) context:NULL]; + _end*/ + + MetaFile_->active_ = packages.size(); + for (size_t index(0), count(packages.size()); index != count; ++index) { + auto package(packages[index]); + [package setIndex:index]; + [package release]; + } } } } @@ -3668,7 +4005,7 @@ class CydiaLogCleaner : } } - (void) configure { - NSString *dpkg = [NSString stringWithFormat:@"dpkg --configure -a --status-fd %u", statusfd_]; + NSString *dpkg = [NSString stringWithFormat:@"/usr/libexec/cydo --configure -a --status-fd %u", statusfd_]; _trace(); system([dpkg UTF8String]); _trace(); @@ -3712,7 +4049,7 @@ class CydiaLogCleaner : return false; pkgSourceList list; - if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) + if ([self popErrorWithTitle:title forReadList:list]) return false; manager_ = (_system->CreatePM(cache_)); @@ -3730,7 +4067,7 @@ class CydiaLogCleaner : NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { pkgSourceList list; - if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) + if ([self popErrorWithTitle:title forReadList:list]) return; for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; @@ -3771,8 +4108,25 @@ class CydiaLogCleaner : if (substrate) RestartSubstrate_ = true; - _system->UnLock(); - pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_); + if (![delock_ isEqual:GetStatusDate()]) { + [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("DPKG_LOCKED") ofType:kCydiaProgressEventTypeError] forTask:title]; + return; + } + + delock_ = nil; + + pkgPackageManager::OrderResult result(manager_->DoInstall(statusfd_)); + + NSString *oextended(@"/var/lib/apt/extended_states"); + NSString *nextended(Cache("extended_states")); + + struct stat info; + if (stat([nextended UTF8String], &info) != -1 && (info.st_mode & S_IFMT) == S_IFREG) + system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/cp --remove-destination %@ %@", ShellEscape(nextended), ShellEscape(oextended)] UTF8String]); + + unlink([nextended UTF8String]); + symlink([oextended UTF8String], [nextended UTF8String]); + if ([self popErrorWithTitle:title]) return; @@ -3788,7 +4142,7 @@ class CydiaLogCleaner : NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { pkgSourceList list; - if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) + if ([self popErrorWithTitle:title forReadList:list]) return; for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; @@ -3798,6 +4152,10 @@ class CydiaLogCleaner : [self update]; } +- (bool) delocked { + return ![delock_ isEqual:GetStatusDate()]; +} + - (bool) upgrade { NSString *title(UCLocalize("UPGRADE")); if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) @@ -3809,11 +4167,11 @@ class CydiaLogCleaner : [self updateWithStatus:status_]; } -- (void) updateWithStatus:(Status &)status { +- (void) updateWithStatus:(CancelStatus &)status { NSString *title(UCLocalize("REFRESHING_DATA")); pkgSourceList list; - if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) + if ([self popErrorWithTitle:title forReadList:list]) return; FileFd lock; @@ -3828,8 +4186,10 @@ class CydiaLogCleaner : _error->Discard(); else { [self popErrorWithTitle:title forOperation:success]; - [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"]; - Changed_ = true; + + [[NSDictionary dictionaryWithObjectsAndKeys: + [NSDate date], @"LastUpdate", + nil] writeToFile:@ CacheState_ atomically:YES]; } [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; @@ -3853,6 +4213,16 @@ class CydiaLogCleaner : return i == sourceMap_.end() ? nil : i->second; } +- (void) setFetch:(bool)fetch forURI:(const char *)uri { + for (Source *source in (id) sourceList_) + [source setFetch:fetch forURI:uri]; +} + +- (void) resetFetch { + for (Source *source in (id) sourceList_) + [source resetFetch]; +} + - (NSString *) mappedSectionForPointer:(const char *)section { _H *mapped; @@ -3890,7 +4260,7 @@ class CydiaLogCleaner : static _H Diversions_; @interface Diversion : NSObject { - Pcre pattern_; + RegEx pattern_; _H key_; _H format_; } @@ -3946,7 +4316,7 @@ static _H Diversions_; _transient id delegate_; } -- (id) initWithDelegate:(IndirectDelegate *)indirect; +- (id) initWithDelegate:(CyteWebViewController *)indirect; @end @@ -3966,9 +4336,9 @@ static _H Diversions_; /* Web Scripting {{{ */ @implementation CydiaObject -- (id) initWithDelegate:(IndirectDelegate *)indirect { +- (id) initWithDelegate:(CyteWebViewController *)indirect { if ((self = [super init]) != nil) { - indirect_ = (CyteWebViewController *) indirect; + indirect_ = indirect; } return self; } @@ -3978,8 +4348,10 @@ static _H Diversions_; + (NSArray *) _attributeKeys { return [NSArray arrayWithObjects: + @"bittage", @"bbsnum", @"build", + @"cells", @"coreFoundationVersionNumber", @"device", @"ecid", @@ -3992,7 +4364,6 @@ static _H Diversions_; @"operator", @"role", @"serial", - @"token", @"version", nil]; } @@ -4009,6 +4380,17 @@ static _H Diversions_; return Cydia_; } +- (unsigned) bittage { +#if 0 +#elif defined(__arm64__) + return 64; +#elif defined(__arm__) + return 32; +#else + return 0; +#endif +} + - (NSString *) build { return System_; } @@ -4033,6 +4415,29 @@ static _H Diversions_; return (id) Idiom_ ?: [NSNull null]; } +- (NSArray *) cells { + auto *$_CTServerConnectionCreate(reinterpret_cast(dlsym(RTLD_DEFAULT, "_CTServerConnectionCreate"))); + if ($_CTServerConnectionCreate == NULL) + return nil; + + struct CTResult { int flag; int error; }; + auto *$_CTServerConnectionCellMonitorCopyCellInfo(reinterpret_cast(dlsym(RTLD_DEFAULT, "_CTServerConnectionCellMonitorCopyCellInfo"))); + if ($_CTServerConnectionCellMonitorCopyCellInfo == NULL) + return nil; + + _H connection($_CTServerConnectionCreate(NULL, NULL, NULL), true); + if (connection == nil) + return nil; + + int count(0); + CFArrayRef cells(NULL); + auto result($_CTServerConnectionCellMonitorCopyCellInfo(connection, &count, &cells)); + if (result.flag != 0) + return nil; + + return [(NSArray *) cells autorelease]; +} + - (NSString *) mcc { if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef) = reinterpret_cast(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode"))) return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault) autorelease]; @@ -4071,10 +4476,6 @@ static _H Diversions_; return [NSString stringWithUTF8String:Machine_]; } -- (NSString *) token { - return (id) Token_ ?: [NSNull null]; -} - + (NSString *) webScriptNameForSelector:(SEL)selector { if (false); else if (selector == @selector(addBridgedHost:)) @@ -4083,12 +4484,8 @@ static _H Diversions_; return @"addInsecureHost"; else if (selector == @selector(addInternalRedirect::)) return @"addInternalRedirect"; - else if (selector == @selector(addPipelinedHost:scheme:)) - return @"addPipelinedHost"; else if (selector == @selector(addSource:::)) return @"addSource"; - else if (selector == @selector(addTokenHost:)) - return @"addTokenHost"; else if (selector == @selector(addTrivialSource:)) return @"addTrivialSource"; else if (selector == @selector(close)) @@ -4101,6 +4498,10 @@ static _H Diversions_; return @"getAllSources"; else if (selector == @selector(getApplicationInfo:value:)) return @"getApplicationInfoValue"; + else if (selector == @selector(getDisplayIdentifiers)) + return @"getDisplayIdentifiers"; + else if (selector == @selector(getLocalizedNameForDisplayIdentifier:)) + return @"getLocalizedNameForDisplayIdentifier"; else if (selector == @selector(getKernelNumber:)) return @"getKernelNumber"; else if (selector == @selector(getKernelString:)) @@ -4192,7 +4593,7 @@ static _H Diversions_; } - (void) unload { - [delegate_ performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; + [[indirect_ rootViewController] performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; } - (void) setScrollAlwaysBounceVertical:(NSNumber *)value { @@ -4217,6 +4618,14 @@ static _H Diversions_; return [info objectForKey:key]; } +- (NSArray *) getDisplayIdentifiers { + return SBSCopyApplicationDisplayIdentifiers(false, false); +} + +- (NSString *) getLocalizedNameForDisplayIdentifier:(NSString *)identifier { + return [SBSCopyLocalizedApplicationNameForDisplayIdentifier(identifier) autorelease] ?: (id) [NSNull null]; +} + - (NSNumber *) getKernelNumber:(NSString *)name { const char *string([name UTF8String]); @@ -4282,8 +4691,6 @@ static _H Diversions_; [Values_ removeObjectForKey:key]; else [Values_ setObject:value forKey:key]; - - [delegate_ performSelectorOnMainThread:@selector(updateValues) withObject:nil waitUntilDone:YES]; } } - (id) getSessionValue:(NSString *)key { @@ -4309,19 +4716,6 @@ static _H Diversions_; [InsecureHosts_ addObject:host]; } } -- (void) addTokenHost:(NSString *)host { -@synchronized (HostConfig_) { - [TokenHosts_ addObject:host]; -} } - -- (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme { -@synchronized (HostConfig_) { - if (scheme != (id) [WebUndefined undefined]) - host = [NSString stringWithFormat:@"%@:%@", [scheme lowercaseString], host]; - - [PipelinedHosts_ addObject:host]; -} } - - (void) popViewController:(NSNumber *)value { if (value == (id) [WebUndefined undefined]) value = [NSNumber numberWithBool:YES]; @@ -4342,8 +4736,12 @@ static _H Diversions_; nil] waitUntilDone:NO]; } -- (void) addTrivialSource:(NSString *)href { +- (BOOL) addTrivialSource:(NSString *)href { + href = VerifySource(href); + if (href == nil) + return NO; [delegate_ performSelectorOnMainThread:@selector(addTrivialSource:) withObject:href waitUntilDone:NO]; + return YES; } - (void) refreshSources { @@ -4401,36 +4799,20 @@ static _H Diversions_; - (NSNumber *) du:(NSString *)path { NSNumber *value(nil); - int fds[2]; - _assert(pipe(fds) != -1); - - pid_t pid(ExecFork()); - if (pid == 0) { - _assert(dup2(fds[1], 1) != -1); - _assert(close(fds[0]) != -1); - _assert(close(fds[1]) != -1); - /* XXX: this should probably not use du */ - execl("/usr/libexec/cydia/du", "du", "-s", [path UTF8String], NULL); - exit(1); - } else { - _assert(close(fds[1]) != -1); - - if (FILE *du = fdopen(fds[0], "r")) { - char line[1024]; - while (fgets(line, sizeof(line), du) != NULL) { - size_t length(strlen(line)); - while (length != 0 && line[length - 1] == '\n') - line[--length] = '\0'; - if (char *tab = strchr(line, '\t')) { - *tab = '\0'; - value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; - } + FILE *du(popen([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/du -ks %@", ShellEscape(path)] UTF8String], "r")); + if (du != NULL) { + char line[1024]; + while (fgets(line, sizeof(line), du) != NULL) { + size_t length(strlen(line)); + while (length != 0 && line[length - 1] == '\n') + line[--length] = '\0'; + if (char *tab = strchr(line, '\t')) { + *tab = '\0'; + value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; } - - fclose(du); - } else - _assert(close(fds[0]) != -1); - } ReapZombie(pid); + } + pclose(du); + } return value; } @@ -4448,10 +4830,16 @@ static _H Diversions_; } - (NSString *) substitutePackageNames:(NSString *)message { + auto database([Database sharedInstance]); + + // XXX: this check is less racy than you'd expect, but this entire concept is a little awkward + if (![database hasPackages]) + return message; + NSMutableArray *words([[[message componentsSeparatedByString:@" "] mutableCopy] autorelease]); for (size_t i(0), e([words count]); i != e; ++i) { NSString *word([words objectAtIndex:i]); - if (Package *package = [[Database sharedInstance] packageWithName:word]) + if (Package *package = [database packageWithName:word]) [words replaceObjectAtIndex:i withObject:[package name]]; } @@ -4504,19 +4892,8 @@ static _H Diversions_; [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]]; } -- (void) _setToken:(NSString *)token { - Token_ = token; - - if (token == nil) - [Metadata_ removeObjectForKey:@"Token"]; - else - [Metadata_ setObject:Token_ forKey:@"Token"]; - - Changed_ = true; -} - - (void) setToken:(NSString *)token { - [self performSelectorOnMainThread:@selector(_setToken:) withObject:token waitUntilDone:NO]; + // XXX: the website expects this :/ } - (void) scrollToBottom:(NSNumber *)animated { @@ -4570,7 +4947,10 @@ static _H Diversions_; @implementation CydiaWebViewController - (NSURL *) navigationURL { - return request_ == nil ? nil : [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request_ URL] absoluteString]]]; + if (NSURLRequest *request = self.request) + return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request URL] absoluteString]]]; + else + return nil; } + (void) _initialize { @@ -4623,6 +5003,10 @@ static _H Diversions_; return [CydiaWebViewController requestWithHeaders:[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; } +- (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { + return [CydiaWebViewController requestWithHeaders:[super webThreadWebView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; +} + + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request { NSMutableURLRequest *copy([[request mutableCopy] autorelease]); @@ -4648,23 +5032,12 @@ static _H Diversions_; if (Machine_ != NULL && [copy valueForHTTPHeaderField:@"X-Machine"] == nil) [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; - bool bridged; - bool token; - - @synchronized (HostConfig_) { + bool bridged; @synchronized (HostConfig_) { bridged = [BridgedHosts_ containsObject:host]; - token = [TokenHosts_ containsObject:host]; } - if ([url isCydiaSecure]) { - if (bridged) { - if (UniqueID_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Id"] == nil) - [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; - } else if (token) { - if (Token_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Token"] == nil) - [copy setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"]; - } - } + if ([url isCydiaSecure] && bridged && UniqueID_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Id"] == nil) + [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; return copy; } @@ -4680,7 +5053,7 @@ static _H Diversions_; - (id) init { if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) { - cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease]; + cydia_ = [[[CydiaObject alloc] initWithDelegate:self.indirect] autorelease]; } return self; } @@ -4704,42 +5077,6 @@ static _H Diversions_; @end /* }}} */ -// CydiaScript {{{ -@interface NSObject (CydiaScript) -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; -@end - -@implementation NSObject (CydiaScript) - -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { - return self; -} - -@end - -@implementation NSArray (CydiaScript) - -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { - WebScriptObject *object([context evaluateWebScript:@"[]"]); - for (size_t i(0), e([self count]); i != e; ++i) - [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; - return object; -} - -@end - -@implementation NSDictionary (CydiaScript) - -- (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { - WebScriptObject *object([context evaluateWebScript:@"({})"]); - for (id i in self) - [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; - return object; -} - -@end -// }}} - /* Confirmation Controller {{{ */ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if (!iterator.end()) @@ -4783,7 +5120,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { - (void) complete { if (substrate_) RestartSubstrate_ = true; - [delegate_ confirmWithNavigationController:[self navigationController]]; + [self.delegate confirmWithNavigationController:[self navigationController]]; } - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { @@ -4791,7 +5128,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if ([context isEqualToString:@"remove"]) { if (button == [alert cancelButtonIndex]) - [self dismissModalViewControllerAnimated:YES]; + [self _doContinue]; else if (button == [alert firstOtherButtonIndex]) { [self performSelector:@selector(complete) withObject:nil afterDelay:0]; } @@ -4806,7 +5143,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } - (void) _doContinue { - [delegate_ cancelAndClear:NO]; + [self.delegate cancelAndClear:NO]; [self dismissModalViewControllerAnimated:YES]; } @@ -4896,7 +5233,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { reason = @"virtual"; } - NSDictionary *version(start.TargetVer() == 0 ? [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: + NSDictionary *version(start.TargetVer() == 0 ? (NSDictionary *) [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithUTF8String:start.CompType()], @"operator", [NSString stringWithUTF8String:start.TargetVer()], @"value", nil]); @@ -4918,7 +5255,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { pkgDepCache::StateCache &state(cache[iterator]); - static Pcre special_r("^(firmware$|gsc\\.|cy\\+)"); + static RegEx special_r("(firmware|gsc\\..*|cy\\+.*)"); if (state.NewInstall()) [installs addObject:name]; @@ -5029,7 +5366,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { #endif - (void) cancelButtonClicked { - [delegate_ cancelAndClear:YES]; + [self.delegate cancelAndClear:YES]; [self dismissModalViewControllerAnimated:YES]; } @@ -5213,7 +5550,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { - (id) initWithDatabase:(Database *)database delegate:(id)delegate { if ((self = [super init]) != nil) { database_ = database; - delegate_ = delegate; + self.delegate = delegate; [database_ setProgressDelegate:self]; @@ -5222,7 +5559,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/progress/", UI_]]]; - [scroller_ setBackgroundColor:[UIColor blackColor]]; + [self setPageColor:[UIColor blackColor]]; [[self navigationItem] setHidesBackButton:YES]; @@ -5244,51 +5581,23 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { [super viewWillAppear:animated]; } -- (void) reloadSpringBoard { - if (kCFCoreFoundationVersionNumber > 700) { // XXX: iOS 6.x - system("/bin/launchctl stop com.apple.backboardd"); - sleep(15); - system("/usr/bin/killall backboardd SpringBoard sbreload"); - return; - } - - pid_t pid(ExecFork()); - if (pid == 0) { - if (setsid() == -1) - perror("setsid"); - - pid_t pid(ExecFork()); - if (pid == 0) { - execl("/usr/bin/sbreload", "sbreload", NULL); - perror("sbreload"); - - exit(0); - } ReapZombie(pid); - - exit(0); - } ReapZombie(pid); - - sleep(15); - system("/usr/bin/killall backboardd SpringBoard sbreload"); -} - - (void) close { UpdateExternalStatus(0); if (Finish_ > 1) - [delegate_ saveState]; + [self.delegate saveState]; switch (Finish_) { case 0: - [delegate_ returnToCydia]; + [self.delegate returnToCydia]; break; case 1: - [delegate_ terminateWithSuccess]; - /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) - [delegate_ suspendWithAnimation:YES]; + [self.delegate terminateWithSuccess]; + /*if ([self.delegate respondsToSelector:@selector(suspendWithAnimation:)]) + [self.delegate suspendWithAnimation:YES]; else - [delegate_ suspend];*/ + [self.delegate suspend];*/ break; case 2: @@ -5300,9 +5609,9 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { goto reload; reload: { - UIProgressHUD *hud([delegate_ addProgressHUD]); + UIProgressHUD *hud([self.delegate addProgressHUD]); [hud setText:UCLocalize("LOADING")]; - [self performSelector:@selector(reloadSpringBoard) withObject:nil afterDelay:0.5]; + [self.delegate performSelector:@selector(reloadSpringBoard) withObject:nil afterDelay:0.5]; return; } @@ -5500,12 +5809,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { UIView *content([self contentView]); CGRect bounds([content bounds]); - content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; - [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; - [content addSubview:content_]; + self.content = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; + [self.content setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; + [content addSubview:self.content]; - [content_ setDelegate:self]; - [content_ setOpaque:YES]; + [self.content setDelegate:self]; + [self.content setOpaque:YES]; } return self; } @@ -5524,7 +5833,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { placard_ = nil; if (package == nil) - [content_ setBackgroundColor:[UIColor whiteColor]]; + [self.content setBackgroundColor:[UIColor whiteColor]]; else { [package parse]; @@ -5584,18 +5893,18 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { placard = nil; } - [content_ setBackgroundColor:color]; + [self.content setBackgroundColor:color]; if (placard != nil) placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/%@.png", App_, placard]]; } [self setNeedsDisplay]; - [content_ setNeedsDisplay]; + [self.content setNeedsDisplay]; } - (void) drawSummaryContentRect:(CGRect)rect { - bool highlighted(highlighted_); + bool highlighted(self.highlighted); float width([self bounds].size.width); if (icon_ != nil) { @@ -5610,7 +5919,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { rect.origin.x = 19 - rect.size.width / 2; rect.origin.y = 19 - rect.size.height / 2; - [icon_ drawInRect:rect]; + [icon_ drawInRect:Retina(rect)]; } if (badge_ != nil) { @@ -5623,7 +5932,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { rect.origin.x = 25 - rect.size.width / 2; rect.origin.y = 25 - rect.size.height / 2; - [badge_ drawInRect:rect]; + [badge_ drawInRect:Retina(rect)]; } if (highlighted && kCFCoreFoundationVersionNumber < 800) @@ -5631,14 +5940,14 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if (!highlighted) UISetColor(commercial_ ? Purple_ : Black_); - [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; + [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; if (placard_ != nil) [placard_ drawAtPoint:CGPointMake(width - 52, 11)]; } - (void) drawNormalContentRect:(CGRect)rect { - bool highlighted(highlighted_); + bool highlighted(self.highlighted); float width([self bounds].size.width); if (icon_ != nil) { @@ -5653,7 +5962,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { rect.origin.x = 25 - rect.size.width / 2; rect.origin.y = 25 - rect.size.height / 2; - [icon_ drawInRect:rect]; + [icon_ drawInRect:Retina(rect)]; } if (badge_ != nil) { @@ -5666,7 +5975,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { rect.origin.x = 36 - rect.size.width / 2; rect.origin.y = 36 - rect.size.height / 2; - [badge_ drawInRect:rect]; + [badge_ drawInRect:Retina(rect)]; } if (highlighted && kCFCoreFoundationVersionNumber < 800) @@ -5674,12 +5983,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if (!highlighted) UISetColor(commercial_ ? Purple_ : Black_); - [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; - [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; + [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; + [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; if (!highlighted) UISetColor(commercial_ ? Purplish_ : Gray_); - [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:UILineBreakModeTailTruncation]; + [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:NSLineBreakByTruncatingTail]; if (placard_ != nil) [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; @@ -5715,7 +6024,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { - icon_ = [UIImage applicationImageNamed:@"folder.png"]; + icon_ = [UIImage imageNamed:@"folder.png"]; // XXX: this initial frame is wrong, but is fixed later switch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)] autorelease]; [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; @@ -5723,12 +6032,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { UIView *content([self contentView]); CGRect bounds([content bounds]); - content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; - [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; - [content addSubview:content_]; - [content_ setBackgroundColor:[UIColor whiteColor]]; + self.content = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; + [self.content setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; + [content addSubview:self.content]; + [self.content setBackgroundColor:[UIColor whiteColor]]; - [content_ setDelegate:self]; + [self.content setDelegate:self]; } return self; } @@ -5740,7 +6049,6 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; - Changed_ = true; } - (void) setSection:(Section *)section editing:(BOOL)editing { @@ -5765,7 +6073,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { section_ = [section localized]; name_ = section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : (NSString *) section_; - count_ = [NSString stringWithFormat:@"%d", [section count]]; + count_ = [NSString stringWithFormat:@"%zd", [section count]]; if (editing_) [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; @@ -5774,7 +6082,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; - [content_ setNeedsDisplay]; + [self.content setNeedsDisplay]; } - (void) setFrame:(CGRect)frame { @@ -5789,7 +6097,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } - (void) drawContentRect:(CGRect)rect { - bool highlighted(highlighted_ && !editing_); + bool highlighted(self.highlighted && !editing_); [icon_ drawInRect:CGRectMake(7, 7, 32, 32)]; @@ -5802,13 +6110,13 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if (!highlighted) UISetColor(Black_); - [name_ drawAtPoint:CGPointMake(48, 12) forWidth:(width - 58) withFont:Font18_ lineBreakMode:UILineBreakModeTailTruncation]; + [name_ drawAtPoint:CGPointMake(48, 12) forWidth:(width - 58) withFont:Font18_ lineBreakMode:NSLineBreakByTruncatingTail]; CGSize size = [count_ sizeWithFont:Font14_]; UISetColor(Folder_); if (count_ != nil) - [count_ drawAtPoint:CGPointMake(10 + (30 - size.width) / 2, 18) withFont:Font12Bold_]; + [count_ drawAtPoint:CGPointMake(Retina(10 + (30 - size.width) / 2), 18) withFont:Font12Bold_]; } @end @@ -5917,7 +6225,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { NSString *directory = [stack lastObject]; [stack addObject:[file stringByAppendingString:@"/"]]; [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", - ([stack count] - 2) * 3, "", + int(([stack count] - 2) * 3), "", [file substringFromIndex:[directory length]] ]]; } @@ -5943,8 +6251,10 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { _H package_; _H name_; bool commercial_; - _H buttons_; + std::vector, _H>> buttons_; + _H sheet_; _H button_; + _H versions_; } - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer; @@ -5957,31 +6267,64 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@", (id) name_]]; } -/* XXX: this is not safe at all... localization of /fail/ */ +- (void) _clickButtonWithPackage:(Package *)package { + [self.delegate installPackage:package]; +} + - (void) _clickButtonWithName:(NSString *)name { - if ([name isEqualToString:UCLocalize("CLEAR")]) - [delegate_ clearPackage:package_]; - else if ([name isEqualToString:UCLocalize("INSTALL")]) - [delegate_ installPackage:package_]; - else if ([name isEqualToString:UCLocalize("REINSTALL")]) - [delegate_ installPackage:package_]; - else if ([name isEqualToString:UCLocalize("REMOVE")]) - [delegate_ removePackage:package_]; - else if ([name isEqualToString:UCLocalize("UPGRADE")]) - [delegate_ installPackage:package_]; + if ([name isEqualToString:@"CLEAR"]) + return [self.delegate clearPackage:package_]; + else if ([name isEqualToString:@"REMOVE"]) + return [self.delegate removePackage:package_]; + else if ([name isEqualToString:@"DOWNGRADE"]) { + sheet_ = [[[UIActionSheet alloc] + initWithTitle:nil + delegate:self + cancelButtonTitle:nil + destructiveButtonTitle:nil + otherButtonTitles:nil + ] autorelease]; + + for (Package *version in (id) versions_) + [sheet_ addButtonWithTitle:[version latest]]; + [sheet_ setContext:@"version"]; + + [self.delegate showActionSheet:sheet_ fromItem:[[self navigationItem] rightBarButtonItem]]; + return; + } + + else if ([name isEqualToString:@"INSTALL"]); + else if ([name isEqualToString:@"REINSTALL"]); + else if ([name isEqualToString:@"UPGRADE"]); else _assert(false); + + [self.delegate installPackage:package_]; } - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { NSString *context([sheet context]); + if (sheet_ == sheet) + sheet_ = nil; if ([context isEqualToString:@"modify"]) { if (button != [sheet cancelButtonIndex]) { - NSString *buttonName = [buttons_ objectAtIndex:button]; - [self _clickButtonWithName:buttonName]; + if (IsWildcat_) + [self performSelector:@selector(_clickButtonWithName:) withObject:buttons_[button].first afterDelay:0]; + else + [self _clickButtonWithName:buttons_[button].first]; + } + + [sheet dismissWithClickedButtonIndex:button animated:YES]; + } else if ([context isEqualToString:@"version"]) { + if (button != [sheet cancelButtonIndex]) { + Package *version([versions_ objectAtIndex:button]); + if (IsWildcat_) + [self performSelector:@selector(_clickButtonWithPackage:) withObject:version afterDelay:0]; + else + [self _clickButtonWithPackage:version]; } - [sheet dismissWithClickedButtonIndex:-1 animated:YES]; + [sheet dismissWithClickedButtonIndex:button animated:YES]; } } @@ -5991,17 +6334,21 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { #if !AlwaysReload - (void) _customButtonClicked { - int count([buttons_ count]); + if (commercial_ && self.isLoading && [package_ uninstalled]) + return [self reloadURLWithCache:NO]; + + size_t count(buttons_.size()); if (count == 0) return; if (count == 1) - [self _clickButtonWithName:[buttons_ objectAtIndex:0]]; + [self _clickButtonWithName:buttons_[0].first]; else { NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; - [buttons addObjectsFromArray:buttons_]; + for (const auto &button : buttons_) + [buttons addObject:button.second]; - UIActionSheet *sheet = [[[UIActionSheet alloc] + sheet_ = [[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil @@ -6009,23 +6356,14 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { otherButtonTitles:nil ] autorelease]; - for (NSString *button in buttons) [sheet addButtonWithTitle:button]; - if (!IsWildcat_) { - [sheet addButtonWithTitle:UCLocalize("CANCEL")]; - [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; - } - [sheet setContext:@"modify"]; + for (NSString *button in buttons) + [sheet_ addButtonWithTitle:button]; + [sheet_ setContext:@"modify"]; - [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; + [self.delegate showActionSheet:sheet_ fromItem:[[self navigationItem] rightBarButtonItem]]; } } -- (void) reloadButtonClicked { - if (commercial_ && function_ == nil && [package_ uninstalled]) - return; - [self customButtonClicked]; -} - - (void) applyLoadingTitle { // Don't show "Loading" as the title. Ever. } @@ -6035,10 +6373,13 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } #endif +- (void) setPageColor:(UIColor *)color { + return [super setPageColor:nil]; +} + - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer { if ((self = [super init]) != nil) { database_ = database; - buttons_ = [NSMutableArray arrayWithCapacity:4]; name_ = name == nil ? @"" : [NSString stringWithString:name]; [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]] withReferrer:referrer]; } return self; @@ -6047,9 +6388,13 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { - (void) reloadData { [super reloadData]; + [sheet_ dismissWithClickedButtonIndex:[sheet_ cancelButtonIndex] animated:YES]; + sheet_ = nil; + package_ = [database_ packageWithName:name_]; + versions_ = [package_ downgrades]; - [buttons_ removeAllObjects]; + buttons_.clear(); if (package_ != nil) { [(Package *) package_ parse]; @@ -6057,22 +6402,24 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { commercial_ = [package_ isCommercial]; if ([package_ mode] != nil) - [buttons_ addObject:UCLocalize("CLEAR")]; + buttons_.push_back(std::make_pair(@"CLEAR", UCLocalize("CLEAR"))); if ([package_ source] == nil); else if ([package_ upgradableAndEssential:NO]) - [buttons_ addObject:UCLocalize("UPGRADE")]; + buttons_.push_back(std::make_pair(@"UPGRADE", UCLocalize("UPGRADE"))); else if ([package_ uninstalled]) - [buttons_ addObject:UCLocalize("INSTALL")]; + buttons_.push_back(std::make_pair(@"INSTALL", UCLocalize("INSTALL"))); else - [buttons_ addObject:UCLocalize("REINSTALL")]; + buttons_.push_back(std::make_pair(@"REINSTALL", UCLocalize("REINSTALL"))); if (![package_ uninstalled]) - [buttons_ addObject:UCLocalize("REMOVE")]; + buttons_.push_back(std::make_pair(@"REMOVE", UCLocalize("REMOVE"))); + if ([versions_ count] != 0) + buttons_.push_back(std::make_pair(@"DOWNGRADE", UCLocalize("DOWNGRADE"))); } NSString *title; - switch ([buttons_ count]) { + switch (buttons_.size()) { case 0: title = nil; break; - case 1: title = [buttons_ objectAtIndex:0]; break; + case 1: title = buttons_[0].second; break; default: title = UCLocalize("MODIFY"); break; } @@ -6099,19 +6446,22 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { _transient Database *database_; unsigned era_; _H packages_; - _H sections_; + _H sections_; _H list_; - _H index_; - _H indices_; + + _H thumbs_; + std::vector offset_; + _H title_; unsigned reloading_; } - (id) initWithDatabase:(Database *)database title:(NSString *)title; -- (void) setDelegate:(id)delegate; - (void) resetCursor; - (void) clearData; +- (NSArray *) sectionsForPackages:(NSMutableArray *)packages; + @end @implementation PackageListController @@ -6175,10 +6525,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { UIViewAnimationCurve curve; [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; - CGRect kbframe = CGRectMake(round(center.x - bounds.size.width / 2.0), round(center.y - bounds.size.height / 2.0), bounds.size.width, bounds.size.height); - UIViewController *base = self; - while ([base parentOrPresentingViewController] != nil) - base = [base parentOrPresentingViewController]; + CGRect kbframe = CGRectMake(Retina(center.x - bounds.size.width / 2), Retina(center.y - bounds.size.height / 2), bounds.size.width, bounds.size.height); + UIViewController *base([self rootViewController]); CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]]; CGRect intersection = CGRectIntersection(viewframe, kbframe); @@ -6219,16 +6567,10 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { - (void) didSelectPackage:(Package *)package { CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[[self referrerURL] absoluteString]] autorelease]); - [view setDelegate:delegate_]; + [view setDelegate:self.delegate]; [[self navigationController] pushViewController:view animated:YES]; } -#if TryIndexedCollation -+ (BOOL) hasIndexedCollation { - return NO; // XXX: objc_getClass("UILocalizedIndexedCollation") != nil; -} -#endif - - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { NSInteger count([sections_ count]); return count == 0 ? 1 : count; @@ -6274,20 +6616,11 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { } - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { - if (![self showsSections]) - return nil; - - return index_; + return thumbs_; } - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { -#if TryIndexedCollation - if ([[self class] hasIndexedCollation]) { - return [[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionForSectionIndexTitleAtIndex:index]; - } -#endif - - return index; + return offset_[index]; } - (void) updateHeight { @@ -6325,14 +6658,11 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { packages_ = nil; sections_ = nil; - index_ = nil; - indices_ = nil; - [super releaseSubviews]; -} + thumbs_ = nil; + offset_.clear(); -- (void) setDelegate:(id)delegate { - delegate_ = delegate; + [super releaseSubviews]; } - (bool) shouldYield { @@ -6357,7 +6687,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { return; } - NSArray *packages; + NSMutableArray *packages; reload: if ([self shouldYield]) { @@ -6367,7 +6697,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { if (![self shouldBlock]) hud = nil; else { - hud = [delegate_ addProgressHUD]; + hud = [self.delegate addProgressHUD]; [hud setText:UCLocalize("LOADING")]; } @@ -6375,7 +6705,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { packages = [self yieldToSelector:@selector(_reloadPackages)]; if (hud != nil) - [delegate_ removeProgressHUD:hud]; + [self.delegate removeProgressHUD:hud]; } while (reloading_ == 2); } else { packages = [self _reloadPackages]; @@ -6386,92 +6716,82 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { goto reload; reloading_ = 0; - packages_ = packages; - - indices_ = [NSMutableDictionary dictionaryWithCapacity:32]; - sections_ = [NSMutableArray arrayWithCapacity:16]; - - Section *section = nil; + thumbs_ = nil; + offset_.clear(); -#if TryIndexedCollation - if ([[self class] hasIndexedCollation]) { - index_ = [[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionIndexTitles]; + packages_ = packages; - id collation = [objc_getClass("UILocalizedIndexedCollation") currentCollation]; - NSArray *titles = [collation sectionIndexTitles]; - int secidx = -1; + if ([self showsSections]) + sections_ = [self sectionsForPackages:packages]; + else { + Section *section([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); + [section setCount:[packages_ count]]; + sections_ = [NSArray arrayWithObject:section]; + } - _profile(PackageTable$reloadData$Section) - for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { - Package *package; - int index; + [self updateHeight]; - _profile(PackageTable$reloadData$Section$Package) - package = [packages_ objectAtIndex:offset]; - index = [collation sectionForObject:package collationStringSelector:@selector(name)]; - _end + _profile(PackageTable$reloadData$List) + [(UITableView *) list_ setDataSource:self]; + [list_ reloadData]; + _end +} - while (secidx < index) { - secidx += 1; + PrintTimes(); +} - _profile(PackageTable$reloadData$Section$Allocate) - section = [[[Section alloc] initWithName:[titles objectAtIndex:secidx] row:offset localize:NO] autorelease]; - _end +- (NSArray *) sectionsForPackages:(NSMutableArray *)packages { + Section *prefix([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); + size_t end([packages count]); - _profile(PackageTable$reloadData$Section$Add) - [sections_ addObject:section]; - _end - } + NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); + Section *section(prefix); - [section addToCount]; - } - _end - } else -#endif - { - index_ = [NSMutableArray arrayWithCapacity:32]; + thumbs_ = CollationThumbs_; + offset_ = CollationOffset_; - bool sectioned([self showsSections]); - if (!sectioned) { - section = [[[Section alloc] initWithName:nil localize:false] autorelease]; - [sections_ addObject:section]; - } + size_t offset(0); + size_t offsets([CollationStarts_ count]); - _profile(PackageTable$reloadData$Section) - for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { - Package *package; - unichar index; + NSString *start([CollationStarts_ objectAtIndex:offset]); + size_t length([start length]); - _profile(PackageTable$reloadData$Section$Package) - package = [packages_ objectAtIndex:offset]; - index = [package index]; - _end + for (size_t index(0); index != end; ++index) { + if (start != nil) { + Package *package([packages objectAtIndex:index]); + NSString *name(PackageName(package, @selector(cyname))); - if (sectioned && (section == nil || [section index] != index)) { - _profile(PackageTable$reloadData$Section$Allocate) - section = [[[Section alloc] initWithIndex:index row:offset] autorelease]; - _end + //while ([start compare:name options:NSNumericSearch range:NSMakeRange(0, length) locale:CollationLocale_] != NSOrderedDescending) { + while (StringNameCompare(start, name, length) != kCFCompareGreaterThan) { + NSString *title([CollationTitles_ objectAtIndex:offset]); + section = [[[Section alloc] initWithName:title row:index localize:NO] autorelease]; + [sections addObject:section]; - [index_ addObject:[section name]]; - //[indices_ setObject:[NSNumber numberForInt:[sections_ count]] forKey:index]; + start = ++offset == offsets ? nil : [CollationStarts_ objectAtIndex:offset]; + if (start == nil) + break; + length = [start length]; + } + } - _profile(PackageTable$reloadData$Section$Add) - [sections_ addObject:section]; - _end - } + [section addToCount]; + } - [section addToCount]; - } - _end + for (; offset != offsets; ++offset) { + NSString *title([CollationTitles_ objectAtIndex:offset]); + Section *section([[[Section alloc] initWithName:title row:end localize:NO] autorelease]); + [sections addObject:section]; } - [self updateHeight]; + if ([prefix count] != 0) { + Section *suffix([sections lastObject]); + [prefix setName:[suffix name]]; + [suffix setName:nil]; + [sections insertObject:prefix atIndex:(offsets - 1)]; + } - _profile(PackageTable$reloadData$List) - [(UITableView *) list_ setDataSource:self]; - [list_ reloadData]; - _end -} } + return sections; +} - (void) reloadData { [super reloadData]; @@ -6498,113 +6818,61 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) { @end /* }}} */ /* Filtered Package List Controller {{{ */ +typedef Function PackageFilter; +typedef Function PackageSorter; @interface FilteredPackageListController : PackageListController { - SEL filter_; - IMP imp_; - _H object_; - _H stuff_; + PackageFilter filter_; + PackageSorter sorter_; } -- (void) setObject:(id)object; -- (void) setStuff:(id)object; -- (void) setObject:(id)object andStuff:(id)stuff; - -- (void) setObject:(id)object forFilter:(SEL)filter; -- (void) setObject:(id)object andStuff:(id)stuff forFilter:(SEL)filter; +- (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter; -- (SEL) filter; -- (void) setFilter:(SEL)filter; - -- (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object; +- (void) setFilter:(PackageFilter)filter; +- (void) setSorter:(PackageSorter)sorter; @end @implementation FilteredPackageListController -- (SEL) filter { - return filter_; -} - -- (void) setFilter:(SEL)filter { +- (void) setFilter:(PackageFilter)filter { @synchronized (self) { filter_ = filter; - - /* XXX: this is an unsafe optimization of doomy hell */ - Method method(class_getInstanceMethod([Package class], filter)); - _assert(method != NULL); - imp_ = method_getImplementation(method); - _assert(imp_ != NULL); -} } - -- (void) setObject:(id)object { -@synchronized (self) { - object_ = object; } } -- (void) setStuff:(id)stuff { +- (void) setSorter:(PackageSorter)sorter { @synchronized (self) { - stuff_ = stuff; -} } - -- (void) setObject:(id)object andStuff:(id)stuff { -@synchronized (self) { - object_ = object; - stuff_ = stuff; -} } - -- (void) setObject:(id)object forFilter:(SEL)filter { -@synchronized (self) { - [self setFilter:filter]; - object_ = object; -} } - -- (void) setObject:(id)object andStuff:(id)stuff forFilter:(SEL)filter { -@synchronized (self) { - [self setFilter:filter]; - object_ = object; - stuff_ = stuff; + sorter_ = sorter; } } - (NSMutableArray *) _reloadPackages { @synchronized (database_) { era_ = [database_ era]; - NSArray *packages([database_ packages]); + NSArray *packages([database_ packages]); NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); - IMP imp; - SEL filter; - _H object; - _H stuff; + PackageFilter filter; + PackageSorter sorter; @synchronized (self) { - imp = imp_; filter = filter_; - object = object_; - stuff = stuff_; + sorter = sorter_; } _profile(PackageTable$reloadData$Filter) for (Package *package in packages) - if ([package valid] && (*reinterpret_cast(imp))(package, filter, object, stuff)) + if (filter(package)) [filtered addObject:package]; _end + if (sorter) + sorter(filtered); return filtered; } } -- (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object { - if ((self = [super initWithDatabase:database title:title]) != nil) { - [self setFilter:filter]; - object_ = object; - } return self; -} - -- (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object with:(id)stuff { +- (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter { if ((self = [super initWithDatabase:database title:title]) != nil) { [self setFilter:filter]; - object_ = object; - stuff_ = stuff; } return self; } @@ -6660,7 +6928,7 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi [alert setCancelButtonIndex:0]; [alert setMessage: - @"Copyright \u00a9 2008-2013\n" + @"Copyright \u00a9 2008-2015\n" "SaurikIT, LLC\n" "\n" "Jay Freeman (saurik)\n" @@ -6683,171 +6951,51 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi @end /* }}} */ -/* Refresh Bar {{{ */ -@interface RefreshBar : UINavigationBar { - _H indicator_; - _H prompt_; - _H cancel_; -} - -@end - -@implementation RefreshBar - -- (void) positionViews { - CGRect frame = [cancel_ frame]; - frame.size = [cancel_ sizeThatFits:frame.size]; - frame.origin.x = [self frame].size.width - frame.size.width - 5; - frame.origin.y = ([self frame].size.height - frame.size.height) / 2; - [cancel_ setFrame:frame]; - - CGSize indsize([UIProgressIndicator defaultSizeForStyle:[indicator_ activityIndicatorViewStyle]]); - unsigned indoffset = ([self frame].size.height - indsize.height) / 2; - CGRect indrect = {{indoffset, indoffset}, indsize}; - [indicator_ setFrame:indrect]; - - CGSize prmsize = {215, indsize.height + 4}; - CGRect prmrect = {{ - indoffset * 2 + indsize.width, - unsigned([self frame].size.height - prmsize.height) / 2 - 1 - }, prmsize}; - [prompt_ setFrame:prmrect]; -} - -- (void) setFrame:(CGRect)frame { - [super setFrame:frame]; - [self positionViews]; -} - -- (id) initWithFrame:(CGRect)frame delegate:(id)delegate { - if ((self = [super initWithFrame:frame]) != nil) { - [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - - [self setBarStyle:UIBarStyleBlack]; - - UIBarStyle barstyle([self _barStyle:NO]); - bool ugly(barstyle == UIBarStyleDefault); - - UIProgressIndicatorStyle style = ugly ? - UIProgressIndicatorStyleMediumBrown : - UIProgressIndicatorStyleMediumWhite; - - indicator_ = [[[UIProgressIndicator alloc] initWithFrame:CGRectZero] autorelease]; - [(UIProgressIndicator *) indicator_ setStyle:style]; - [indicator_ startAnimation]; - [self addSubview:indicator_]; - - prompt_ = [[[UITextLabel alloc] initWithFrame:CGRectZero] autorelease]; - [prompt_ setColor:[UIColor colorWithCGColor:(ugly ? Blueish_ : Off_)]]; - [prompt_ setBackgroundColor:[UIColor clearColor]]; - [prompt_ setFont:[UIFont systemFontOfSize:15]]; - [self addSubview:prompt_]; - - cancel_ = [[[UINavigationButton alloc] initWithTitle:UCLocalize("CANCEL") style:UINavigationButtonStyleHighlighted] autorelease]; - [cancel_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; - [cancel_ addTarget:delegate action:@selector(cancelPressed) forControlEvents:UIControlEventTouchUpInside]; - [cancel_ setBarStyle:barstyle]; - - [self positionViews]; - } return self; -} - -- (void) setCancellable:(bool)cancellable { - if (cancellable) - [self addSubview:cancel_]; - else - [cancel_ removeFromSuperview]; -} - -- (void) start { - [prompt_ setText:UCLocalize("UPDATING_DATABASE")]; -} - -- (void) stop { - [self setCancellable:NO]; -} - -- (void) setPrompt:(NSString *)prompt { - [prompt_ setText:prompt]; -} - -- (void) setProgress:(float)progress { -} - -@end -/* }}} */ - -/* Cydia Navigation Controller Interface {{{ */ -@interface UINavigationController (Cydia) - -- (NSArray *) navigationURLCollection; -- (void) unloadData; - -@end -/* }}} */ - /* Cydia Tab Bar Controller {{{ */ @interface CydiaTabBarController : CyteTabBarController < UITabBarControllerDelegate, - ProgressDelegate + FetchDelegate > { _transient Database *database_; - _H refreshbar_; - bool dropped_; + _H indicator_; + bool updating_; // XXX: ok, "updatedelegate_"?... _transient NSObject *updatedelegate_; } -- (NSArray *) navigationURLCollection; -- (void) dropBar:(BOOL)animated; - (void) beginUpdate; -- (void) raiseBar:(BOOL)animated; - (BOOL) updating; @end @implementation CydiaTabBarController -- (NSArray *) navigationURLCollection { - NSMutableArray *items([NSMutableArray array]); - - // XXX: Should this deal with transient view controllers? - for (id navigation in [self viewControllers]) { - NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)]; - if (stack != nil) - [items addObject:stack]; - } - - return items; -} - -- (void) dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; - - [super dealloc]; -} - - (id) initWithDatabase:(Database *)database { if ((self = [super init]) != nil) { database_ = database; [self setDelegate:self]; - [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; + indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteTiny] autorelease]; + [indicator_ setOrigin:CGPointMake(kCFCoreFoundationVersionNumber >= 800 ? 2 : 4, 2)]; - refreshbar_ = [[[RefreshBar alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, [UINavigationBar defaultSize].height) delegate:self] autorelease]; + [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; } return self; } -- (void) setUpdate:(NSDate *)date { - [self beginUpdate]; -} - - (void) beginUpdate { - [(RefreshBar *) refreshbar_ start]; - [self dropBar:YES]; + if (updating_) + return; + + UIViewController *controller([[self viewControllers] objectAtIndex:1]); + UITabBarItem *item([controller tabBarItem]); + + [item setBadgeValue:@""]; + UIView *badge(MSHookIvar([item view], "_badge")); + + [indicator_ startAnimating]; + [badge addSubview:indicator_]; [updatedelegate_ retainNetworkActivityIndicator]; updating_ = true; @@ -6862,8 +7010,7 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi - (void) performUpdate { NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); - Status status; - status.setDelegate(self); + SourceStatus status(self, database_); [database_ updateWithStatus:status]; [self @@ -6879,8 +7026,11 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi updating_ = false; [updatedelegate_ releaseNetworkActivityIndicator]; - [self raiseBar:YES]; - [refreshbar_ stop]; + UIViewController *controller([[self viewControllers] objectAtIndex:1]); + [[controller tabBarItem] setBadgeValue:nil]; + + [indicator_ removeFromSuperview]; + [indicator_ stopAnimating]; [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; } @@ -6903,166 +7053,20 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi return updating_; } -- (void) addProgressEvent:(CydiaProgressEvent *)event { - [refreshbar_ setPrompt:[event compoundMessage]]; -} - -- (bool) isProgressCancelled { +- (bool) isSourceCancelled { return !updating_; } -- (void) setProgressCancellable:(NSNumber *)cancellable { - [refreshbar_ setCancellable:(updating_ && [cancellable boolValue])]; +- (void) startSourceFetch:(NSString *)uri { } -- (void) setProgressPercent:(NSNumber *)percent { - [refreshbar_ setProgress:[percent floatValue]]; -} - -- (void) setProgressStatus:(NSDictionary *)status { - if (status != nil) - [self setProgressPercent:[status objectForKey:@"Percent"]]; +- (void) stopSourceFetch:(NSString *)uri { } - (void) setUpdateDelegate:(id)delegate { updatedelegate_ = delegate; } -- (UIView *) transitionView { - if (![self respondsToSelector:@selector(_transitionView)]) - return MSHookIvar(self, "_viewControllerTransitionView"); - else if (kCFCoreFoundationVersionNumber < 800) - return [self _transitionView]; - else - return [[[self _transitionView] superview] superview]; -} - -- (void) dropBar:(BOOL)animated { - if (dropped_) - return; - dropped_ = true; - - UIView *transition([self transitionView]); - [[self view] addSubview:refreshbar_]; - - CGRect barframe([refreshbar_ frame]); - - if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) - barframe.origin.y = 0; - else if (kCFCoreFoundationVersionNumber < 800) - barframe.origin.y = CYStatusBarHeight(); - else - barframe.origin.y = 0; //-barframe.size.height + CYStatusBarHeight(); - - [refreshbar_ setFrame:barframe]; - - CGRect viewframe = [transition frame]; - -if (kCFCoreFoundationVersionNumber < 800) { - if (animated) - [UIView beginAnimations:nil context:NULL]; - - float adjust(barframe.size.height); - if (kCFCoreFoundationVersionNumber >= 800) - adjust -= CYStatusBarHeight(); - viewframe.origin.y += adjust; - viewframe.size.height -= adjust; - [transition setFrame:viewframe]; - - if (animated) - [UIView commitAnimations]; -} - - // Ensure bar has the proper width for our view, it might have changed - barframe.size.width = viewframe.size.width; - [refreshbar_ setFrame:barframe]; -} - -- (void) raiseBar:(BOOL)animated { - if (!dropped_) - return; - dropped_ = false; - - UIView *transition([self transitionView]); - [refreshbar_ removeFromSuperview]; - - CGRect barframe([refreshbar_ frame]); - -if (kCFCoreFoundationVersionNumber < 800) { - if (animated) - [UIView beginAnimations:nil context:NULL]; - - CGRect viewframe = [transition frame]; - float adjust(barframe.size.height); - if (kCFCoreFoundationVersionNumber >= 800) - adjust -= CYStatusBarHeight(); - viewframe.origin.y -= adjust; - viewframe.size.height += adjust; - [transition setFrame:viewframe]; - - if (animated) - [UIView commitAnimations]; -} -} - -- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { - bool dropped(dropped_); - - if (dropped) - [self raiseBar:NO]; - - [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; - - if (dropped) - [self dropBar:NO]; -} - -- (void) statusBarFrameChanged:(NSNotification *)notification { - if (dropped_) { - [self raiseBar:NO]; - [self dropBar:NO]; - } -} - -@end -/* }}} */ - -/* Cydia Navigation Controller Implementation {{{ */ -@implementation UINavigationController (Cydia) - -- (NSArray *) navigationURLCollection { - NSMutableArray *stack([NSMutableArray array]); - - for (CyteViewController *controller in [self viewControllers]) { - NSString *url = [[controller navigationURL] absoluteString]; - if (url != nil) - [stack addObject:url]; - } - - return stack; -} - -- (void) reloadData { - [super reloadData]; - - UIViewController *visible([self visibleViewController]); - if (visible != nil) - [visible reloadData]; - - // on the iPad, this view controller is ALSO visible. :( - if (IsWildcat_) - if (UIViewController *top = [self topViewController]) - if (top != visible) - [top reloadData]; -} - -- (void) unloadData { - for (CyteViewController *page in [self viewControllers]) - [page unloadData]; - - [super unloadData]; -} - @end /* }}} */ @@ -7135,7 +7139,28 @@ if (kCFCoreFoundationVersionNumber < 800) { Database *database([Database sharedInstance]); - if ([command isEqualToString:@"package-icon"]) { + if (false); + else if ([command isEqualToString:@"application-icon"]) { + if (path == nil) + goto fail; + path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + + UIImage *icon(nil); + + if (icon == nil && $SBSCopyIconImagePNGDataForDisplayIdentifier != NULL) { + NSData *data([$SBSCopyIconImagePNGDataForDisplayIdentifier(path) autorelease]); + icon = [UIImage imageWithData:data]; + } + + if (icon == nil) + if (NSString *file = SBSCopyIconImagePathForDisplayIdentifier(path)) + icon = [UIImage imageAtPath:file]; + + if (icon == nil) + icon = [UIImage imageNamed:@"unknown.png"]; + + [self _returnPNGWithImage:icon forRequest:request]; + } else if ([command isEqualToString:@"package-icon"]) { if (path == nil) goto fail; path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; @@ -7157,7 +7182,7 @@ if (kCFCoreFoundationVersionNumber < 800) { path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [path stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]); if (icon == nil) - icon = [UIImage applicationImageNamed:@"unknown.png"]; + icon = [UIImage imageNamed:@"unknown.png"]; [self _returnPNGWithImage:icon forRequest:request]; } else fail: { [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; @@ -7207,14 +7232,29 @@ if (kCFCoreFoundationVersionNumber < 800) { else title = UCLocalize("NO_SECTION"); - if ((self = [super initWithDatabase:database title:title filter:@selector(isVisibleInSection:source:) with:section with:source]) != nil) { + if ((self = [super initWithDatabase:database title:title]) != nil) { key_ = [source key]; section_ = section; } return self; } - (void) reloadData { - [super setStuff:[database_ sourceWithKey:key_]]; + Source *source([database_ sourceWithKey:key_]); + _H name(section_); + + [self setFilter:[=](Package *package) { + NSString *section([package section]); + + return ( + name == nil || + section == nil && [name length] == 0 || + [name isEqualToString:section] + ) && ( + source == nil || + [package source] == source + ) && [package visible]; + }]; + [super reloadData]; } @@ -7268,7 +7308,7 @@ if (kCFCoreFoundationVersionNumber < 800) { if (editing) [list_ reloadData]; else - [delegate_ updateData]; + [self.delegate updateData]; [self updateNavigationItem]; } @@ -7330,7 +7370,7 @@ if (kCFCoreFoundationVersionNumber < 800) { source:[self source] section:[section name] ] autorelease]; - [controller setDelegate:delegate_]; + [controller setDelegate:self.delegate]; [[self navigationController] pushViewController:controller animated:YES]; } @@ -7401,7 +7441,7 @@ if (kCFCoreFoundationVersionNumber < 800) { [section addToCount]; _profile(SectionsView$reloadData$Filter) - if (![package valid] || ![package visible]) + if (![package visible]) continue; _end @@ -7436,15 +7476,7 @@ if (kCFCoreFoundationVersionNumber < 800) { /* }}} */ /* Changes Controller {{{ */ -@interface ChangesController : CyteViewController < - UITableViewDataSource, - UITableViewDelegate -> { - _transient Database *database_; - unsigned era_; - _H packages_; - _H sections_; - _H list_; +@interface ChangesController : FilteredPackageListController { unsigned upgrades_; } @@ -7454,30 +7486,12 @@ if (kCFCoreFoundationVersionNumber < 800) { @implementation ChangesController -- (NSURL *) navigationURL { - return [NSURL URLWithString:@"cydia://changes"]; -} - -- (void) viewDidAppear:(BOOL)animated { - [super viewDidAppear:animated]; - [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; -} - -- (NSInteger) numberOfSectionsInTableView:(UITableView *)list { - NSInteger count([sections_ count]); - return count == 0 ? 1 : count; -} - -- (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { - if ([sections_ count] == 0) - return nil; - return [[sections_ objectAtIndex:section] name]; +- (NSURL *) referrerURL { + return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/changes/", UI_]]; } -- (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { - if ([sections_ count] == 0) - return 0; - return [[sections_ objectAtIndex:section] count]; +- (NSURL *) navigationURL { + return [NSURL URLWithString:@"cydia://changes"]; } - (Package *) packageAtIndexPath:(NSIndexPath *)path { @@ -7493,24 +7507,6 @@ if (kCFCoreFoundationVersionNumber < 800) { return [[[packages_ objectAtIndex:([section row] + row)] retain] autorelease]; } } -- (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { - PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); - if (cell == nil) - cell = [[[PackageCell alloc] init] autorelease]; - - Package *package([database_ packageWithName:[[self packageAtIndexPath:path] id]]); - [cell setPackage:package asSummary:false]; - return cell; -} - -- (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path { - Package *package([self packageAtIndexPath:path]); - CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[NSString stringWithFormat:@"%@/#!/changes/", UI_]] autorelease]); - [view setDelegate:delegate_]; - [[self navigationController] pushViewController:view animated:YES]; - return path; -} - - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { NSString *context([alert context]); @@ -7518,105 +7514,79 @@ if (kCFCoreFoundationVersionNumber < 800) { [alert dismissWithClickedButtonIndex:-1 animated:YES]; } -- (void) refreshButtonClicked { - if (IsReachable("cydia.saurik.com")) { - [delegate_ beginUpdate]; - [[self navigationItem] setLeftBarButtonItem:nil animated:YES]; - } else { - UIAlertView *alert = [[[UIAlertView alloc] - initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("REFRESH")] - message:@"Host Unreachable" // XXX: Localize - delegate:self - cancelButtonTitle:UCLocalize("OK") - otherButtonTitles:nil - ] autorelease]; +- (void) setLeftBarButtonItem { + if ([self.delegate updating]) + [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] + initWithTitle:UCLocalize("CANCEL") + style:UIBarButtonItemStyleDone + target:self + action:@selector(cancelButtonClicked) + ] autorelease] animated:YES]; + else + [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] + initWithTitle:UCLocalize("REFRESH") + style:UIBarButtonItemStylePlain + target:self + action:@selector(refreshButtonClicked) + ] autorelease] animated:YES]; +} - [alert setContext:@"norefresh"]; - [alert show]; - } +- (void) refreshButtonClicked { + if ([self.delegate requestUpdate]) + [self setLeftBarButtonItem]; +} + +- (void) cancelButtonClicked { + [self.delegate cancelUpdate]; } - (void) upgradeButtonClicked { - [delegate_ distUpgrade]; + [self.delegate distUpgrade]; [[self navigationItem] setRightBarButtonItem:nil animated:YES]; } -- (void) loadView { - UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); - [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; - [self setView:view]; - - list_ = [[[UITableView alloc] initWithFrame:[view bounds] style:UITableViewStylePlain] autorelease]; - [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; - [list_ setRowHeight:73]; - [(UITableView *) list_ setDataSource:self]; - [list_ setDelegate:self]; - [view addSubview:list_]; +- (bool) shouldYield { + return true; } -- (void) viewDidLoad { - [super viewDidLoad]; - - [[self navigationItem] setTitle:UCLocalize("CHANGES")]; +- (bool) shouldBlock { + return true; } -- (void) releaseSubviews { - list_ = nil; - - packages_ = nil; - sections_ = nil; +- (void) useFilter { +@synchronized (self) { + [self setFilter:[](Package *package) { + return [package upgradableAndEssential:YES] || [package visible]; + }]; - [super releaseSubviews]; -} + [self setSorter:[](NSMutableArray *packages) { + [packages radixSortUsingFunction:reinterpret_cast(&PackageChangesRadix) withContext:NULL]; + }]; +} } - (id) initWithDatabase:(Database *)database { - if ((self = [super init]) != nil) { - database_ = database; + if ((self = [super initWithDatabase:database title:UCLocalize("CHANGES")]) != nil) { + [self useFilter]; } return self; } -- (NSMutableArray *) _reloadPackages { -@synchronized (database_) { - era_ = [database_ era]; - NSArray *packages([database_ packages]); - - NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); - - _trace(); - _profile(ChangesController$_reloadPackages$Filter) - for (Package *package in packages) - if ([package upgradableAndEssential:YES] || [package visible]) - CFArrayAppendValue((CFMutableArrayRef) filtered, package); - _end - _trace(); - _profile(ChangesController$_reloadPackages$radixSort) - [filtered radixSortUsingFunction:reinterpret_cast(&PackageChangesRadix) withContext:NULL]; - _end - _trace(); - - return filtered; -} } - -- (void) _reloadData { - NSMutableArray *packages; +- (void) viewDidLoad { + [super viewDidLoad]; + [self setLeftBarButtonItem]; +} - reload: - if (true) { - UIProgressHUD *hud([delegate_ addProgressHUD]); - [hud setText:UCLocalize("LOADING")]; - //NSLog(@"HUD:%@::%@", delegate_, hud); - packages = [self yieldToSelector:@selector(_reloadPackages)]; - [delegate_ removeProgressHUD:hud]; - } else { - packages = [self _reloadPackages]; - } +- (void) viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; + [self setLeftBarButtonItem]; +} -@synchronized (database_) { - if (era_ != [database_ era]) - goto reload; +- (void) reloadData { + [self setLeftBarButtonItem]; + [super reloadData]; +} - packages_ = packages; - sections_ = [NSMutableArray arrayWithCapacity:16]; +- (NSArray *) sectionsForPackages:(NSMutableArray *)packages { + NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; Section *ignored = nil; @@ -7628,8 +7598,8 @@ if (kCFCoreFoundationVersionNumber < 800) { CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); - for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) { - Package *package = [packages_ objectAtIndex:offset]; + for (size_t offset = 0, count = [packages count]; offset != count; ++offset) { + Package *package = [packages objectAtIndex:offset]; BOOL uae = [package upgradableAndEssential:YES]; @@ -7647,7 +7617,7 @@ if (kCFCoreFoundationVersionNumber < 800) { _profile(ChangesController$reloadData$Allocate) name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; - [sections_ addObject:section]; + [sections addObject:section]; _end } @@ -7667,16 +7637,16 @@ if (kCFCoreFoundationVersionNumber < 800) { CFRelease(formatter); if (unseens) { - Section *last = [sections_ lastObject]; + Section *last = [sections lastObject]; size_t count = [last count]; - [packages_ removeObjectsInRange:NSMakeRange([packages_ count] - count, count)]; - [sections_ removeLastObject]; + [packages removeObjectsInRange:NSMakeRange([packages count] - count, count)]; + [sections removeLastObject]; } if ([ignored count] != 0) - [sections_ insertObject:ignored atIndex:0]; + [sections insertObject:ignored atIndex:0]; if (upgrades_ != 0) - [sections_ insertObject:upgradable atIndex:0]; + [sections insertObject:upgradable atIndex:0]; [list_ reloadData]; @@ -7687,19 +7657,7 @@ if (kCFCoreFoundationVersionNumber < 800) { action:@selector(upgradeButtonClicked) ] autorelease]) animated:YES]; - [[self navigationItem] setLeftBarButtonItem:([delegate_ updating] ? nil : [[[UIBarButtonItem alloc] - initWithTitle:UCLocalize("REFRESH") - style:UIBarButtonItemStylePlain - target:self - action:@selector(refreshButtonClicked) - ] autorelease]) animated:YES]; - - PrintTimes(); -} } - -- (void) reloadData { - [super reloadData]; - [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; + return sections; } @end @@ -7710,6 +7668,7 @@ if (kCFCoreFoundationVersionNumber < 800) { > { _H search_; BOOL searchloaded_; + bool summary_; } - (id) initWithDatabase:(Database *)database query:(NSString *)query; @@ -7740,15 +7699,51 @@ if (kCFCoreFoundationVersionNumber < 800) { } - (void) useSearch { - [self setObject:[self termsForQuery:[search_ text]] forFilter:@selector(isUnfilteredAndSearchedForBy:)]; + _H query([self termsForQuery:[search_ text]]); + summary_ = false; + +@synchronized (self) { + [self setFilter:[=](Package *package) { + if (![package unfiltered]) + return false; + if (![package matches:query]) + return false; + return true; + }]; + + [self setSorter:[](NSMutableArray *packages) { + [packages radixSortUsingSelector:@selector(rank)]; + }]; +} + [self clearData]; [self reloadData]; } +- (void) usePrefix:(NSString *)prefix { + _H query(prefix); + summary_ = true; + +@synchronized (self) { + [self setFilter:[=](Package *package) { + if ([query length] == 0) + return false; + if (![package unfiltered]) + return false; + if ([[package name] compare:query options:MatchCompareOptions_ range:NSMakeRange(0, [query length])] != NSOrderedSame) + return false; + return true; + }]; + + [self setSorter:nullptr]; +} + + [self reloadData]; +} + - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar { - [self setObject:[search_ text] forFilter:@selector(isUnfilteredAndSelectedForBy:)]; [self clearData]; - [self reloadData]; + [self usePrefix:[search_ text]]; } - (void) searchBarButtonClicked:(UISearchBar *)searchBar { @@ -7766,8 +7761,7 @@ if (kCFCoreFoundationVersionNumber < 800) { } - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { - [self setObject:text forFilter:@selector(isUnfilteredAndSelectedForBy:)]; - [self reloadData]; + [self usePrefix:text]; } - (bool) shouldYield { @@ -7775,31 +7769,36 @@ if (kCFCoreFoundationVersionNumber < 800) { } - (bool) shouldBlock { - return [self filter] == @selector(isUnfilteredAndSearchedForBy:); + return !summary_; } - (bool) isSummarized { - return [self filter] == @selector(isUnfilteredAndSelectedForBy:); + return summary_; } - (bool) showsSections { return false; } -- (NSMutableArray *) _reloadPackages { - NSMutableArray *packages([super _reloadPackages]); - if ([self filter] == @selector(isUnfilteredAndSearchedForBy:)) - [packages radixSortUsingSelector:@selector(rank)]; - return packages; -} - - (id) initWithDatabase:(Database *)database query:(NSString *)query { - if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH") filter:@selector(isUnfilteredAndSearchedForBy:) with:[self termsForQuery:query]])) { + if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH")])) { search_ = [[[UISearchBar alloc] init] autorelease]; + [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; [search_ setDelegate:self]; + UITextField *textField; + if ([search_ respondsToSelector:@selector(searchField)]) + textField = [search_ searchField]; + else + textField = MSHookIvar(search_, "_searchField"); + + [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; + [textField setEnablesReturnKeyAutomatically:NO]; + [[self navigationItem] setTitleView:textField]; + if (query != nil) [search_ setText:query]; + [self useSearch]; } return self; } @@ -7810,17 +7809,6 @@ if (kCFCoreFoundationVersionNumber < 800) { searchloaded_ = YES; [search_ setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; [search_ layoutSubviews]; - [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; - - UITextField *textField; - if ([search_ respondsToSelector:@selector(searchField)]) - textField = [search_ searchField]; - else - textField = MSHookIvar(search_, "_searchField"); - - [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; - [textField setEnablesReturnKeyAutomatically:NO]; - [[self navigationItem] setTitleView:textField]; } if ([self isSummarized]) @@ -7828,13 +7816,7 @@ if (kCFCoreFoundationVersionNumber < 800) { } - (void) reloadData { - id object([search_ text]); - if ([self filter] == @selector(isUnfilteredAndSearchedForBy:)) - object = [self termsForQuery:object]; - - [self setObject:object]; [self resetCursor]; - [super reloadData]; } @@ -7904,27 +7886,22 @@ if (kCFCoreFoundationVersionNumber < 800) { if (package_ == nil) return; if ([package_ setSubscribed:value]) - [delegate_ updateData]; + [self.delegate updateData]; } - (void) _updateIgnored { const char *package([name_ UTF8String]); bool on([ignoredSwitch_ isOn]); - pid_t pid(ExecFork()); - if (pid == 0) { - FILE *dpkg(popen("dpkg --set-selections", "w")); - fwrite(package, strlen(package), 1, dpkg); - - if (on) - fwrite(" hold\n", 6, 1, dpkg); - else - fwrite(" install\n", 9, 1, dpkg); + FILE *dpkg(popen("/usr/libexec/cydia/cydo --set-selections", "w")); + fwrite(package, strlen(package), 1, dpkg); - pclose(dpkg); + if (on) + fwrite(" hold\n", 6, 1, dpkg); + else + fwrite(" install\n", 9, 1, dpkg); - exit(0); - } ReapZombie(pid); + pclose(dpkg); } - (void) onIgnored:(id)control { @@ -7932,7 +7909,7 @@ if (kCFCoreFoundationVersionNumber < 800) { [invocation setTarget:self]; [invocation setSelector:@selector(_updateIgnored)]; - [delegate_ reloadDataWithInvocation:invocation]; + [self.delegate reloadDataWithInvocation:invocation]; } - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { @@ -8020,12 +7997,10 @@ if (kCFCoreFoundationVersionNumber < 800) { /* Installed Controller {{{ */ @interface InstalledController : FilteredPackageListController { - BOOL expert_; + bool sectioned_; } - (id) initWithDatabase:(Database *)database; - -- (void) updateRoleButton; - (void) queueStatusDidChange; @end @@ -8040,49 +8015,113 @@ if (kCFCoreFoundationVersionNumber < 800) { return [NSURL URLWithString:@"cydia://installed"]; } +- (void) useRecent { + sectioned_ = false; + +@synchronized (self) { + [self setFilter:[](Package *package) { + return ![package uninstalled] && package->role_ < 7; + }]; + + [self setSorter:[](NSMutableArray *packages) { + [packages radixSortUsingSelector:@selector(recent)]; + }]; +} } + +- (void) useFilter:(UISegmentedControl *)segmented { + NSInteger selected([segmented selectedSegmentIndex]); + if (selected == 2) + return [self useRecent]; + bool simple(selected == 0); + sectioned_ = true; + +@synchronized (self) { + [self setFilter:[=](Package *package) { + return ![package uninstalled] && package->role_ <= (simple ? 1 : 3); + }]; + + [self setSorter:nullptr]; +} } + +- (NSArray *) sectionsForPackages:(NSMutableArray *)packages { + if (sectioned_) + return [super sectionsForPackages:packages]; + + CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle)); + + NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); + Section *section(nil); + time_t last(0); + + for (size_t offset(0), count([packages count]); offset != count; ++offset) { + Package *package([packages objectAtIndex:offset]); + + time_t upgraded([package upgraded]); + if (upgraded < 1168364520) + upgraded = 0; + else + upgraded -= upgraded % (60 * 60 * 24); + + if (section == nil || upgraded != last) { + last = upgraded; + + NSString *name; + if (upgraded == 0) + continue; // XXX: name = UCLocalize("..."); + else { + name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:upgraded]); + [name autorelease]; + } + + section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; + [sections addObject:section]; + } + + [section addToCount]; + } + + CFRelease(formatter); + return sections; +} + - (id) initWithDatabase:(Database *)database { - if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED") filter:@selector(isInstalledAndUnfiltered:) with:[NSNumber numberWithBool:YES]]) != nil) { - [self updateRoleButton]; + if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED")]) != nil) { + UISegmentedControl *segmented([[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:UCLocalize("USER"), UCLocalize("EXPERT"), UCLocalize("RECENT"), nil]] autorelease]); + [segmented setSelectedSegmentIndex:0]; + [segmented setSegmentedControlStyle:UISegmentedControlStyleBar]; + [[self navigationItem] setTitleView:segmented]; + + [segmented addTarget:self action:@selector(modeChanged:) forEvents:UIControlEventValueChanged]; + [self useFilter:segmented]; + [self queueStatusDidChange]; } return self; } #if !AlwaysReload - (void) queueButtonClicked { - [delegate_ queue]; + [self.delegate queue]; } #endif - (void) queueStatusDidChange { #if !AlwaysReload if (Queuing_) { - [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] + [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:UCLocalize("QUEUE") style:UIBarButtonItemStyleDone target:self action:@selector(queueButtonClicked) ] autorelease]]; } else { - [[self navigationItem] setLeftBarButtonItem:nil]; + [[self navigationItem] setRightBarButtonItem:nil]; } #endif } -- (void) updateRoleButton { - [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] - initWithTitle:(expert_ ? UCLocalize("EXPERT") : UCLocalize("SIMPLE")) - style:(expert_ ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) - target:self - action:@selector(roleButtonClicked) - ] autorelease]]; -} - -- (void) roleButtonClicked { - [self setObject:[NSNumber numberWithBool:expert_]]; +- (void) modeChanged:(UISegmentedControl *)segmented { + [self useFilter:segmented]; [self reloadData]; - expert_ = !expert_; - - [self updateRoleButton]; } @end @@ -8090,15 +8129,19 @@ if (kCFCoreFoundationVersionNumber < 800) { /* Source Cell {{{ */ @interface SourceCell : CyteTableViewCell < - CyteTableViewCellDelegate + CyteTableViewCellDelegate, + SourceDelegate > { + _H source_; _H url_; _H icon_; _H origin_; _H label_; + _H indicator_; } - (void) setSource:(Source *)source; +- (void) setFetch:(NSNumber *)fetch; @end @@ -8107,7 +8150,7 @@ if (kCFCoreFoundationVersionNumber < 800) { - (void) _setImage:(NSArray *)data { if ([url_ isEqual:[data objectAtIndex:0]]) { icon_ = [data objectAtIndex:1]; - [content_ setNeedsDisplay]; + [self.content setNeedsDisplay]; } } @@ -8131,22 +8174,30 @@ if (kCFCoreFoundationVersionNumber < 800) { } - (void) setSource:(Source *)source { - icon_ = [UIImage applicationImageNamed:@"unknown.png"]; + source_ = source; + [source_ setDelegate:self]; + + [self setFetch:[NSNumber numberWithBool:[source_ fetch]]]; + + icon_ = [UIImage imageNamed:@"unknown.png"]; origin_ = [source name]; label_ = [source rooturi]; - [content_ setNeedsDisplay]; + [self.content setNeedsDisplay]; url_ = [source iconURL]; [NSThread detachNewThreadSelector:@selector(_setSource:) toTarget:self withObject:url_]; } - (void) setAllSource { - icon_ = [UIImage applicationImageNamed:@"folder.png"]; + source_ = nil; + [indicator_ stopAnimating]; + + icon_ = [UIImage imageNamed:@"folder.png"]; origin_ = UCLocalize("ALL_SOURCES"); label_ = UCLocalize("ALL_SOURCES_EX"); - [content_ setNeedsDisplay]; + [self.content setNeedsDisplay]; } - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { @@ -8154,24 +8205,43 @@ if (kCFCoreFoundationVersionNumber < 800) { UIView *content([self contentView]); CGRect bounds([content bounds]); - content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; - [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; - [content_ setBackgroundColor:[UIColor whiteColor]]; - [content addSubview:content_]; + self.content = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; + [self.content setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; + [self.content setBackgroundColor:[UIColor whiteColor]]; + [content addSubview:self.content]; + + [self.content setDelegate:self]; + [self.content setOpaque:YES]; - [content_ setDelegate:self]; - [content_ setOpaque:YES]; + indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGraySmall] autorelease]; + [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin];// | UIViewAutoresizingFlexibleBottomMargin]; + [content addSubview:indicator_]; - [[content_ layer] setContentsGravity:kCAGravityTopLeft]; + [[self.content layer] setContentsGravity:kCAGravityTopLeft]; } return self; } +- (void) layoutSubviews { + [super layoutSubviews]; + + UIView *content([self contentView]); + CGRect bounds([content bounds]); + + CGRect frame([indicator_ frame]); + frame.origin.x = bounds.size.width - frame.size.width; + frame.origin.y = Retina((bounds.size.height - frame.size.height) / 2); + + if (kCFCoreFoundationVersionNumber < 800) + frame.origin.x -= 8; + [indicator_ setFrame:frame]; +} + - (NSString *) accessibilityLabel { return origin_; } - (void) drawContentRect:(CGRect)rect { - bool highlighted(highlighted_); + bool highlighted(self.highlighted); float width(rect.size.width); if (icon_ != nil) { @@ -8186,7 +8256,7 @@ if (kCFCoreFoundationVersionNumber < 800) { rect.origin.x = 26 - rect.size.width / 2; rect.origin.y = 26 - rect.size.height / 2; - [icon_ drawInRect:rect]; + [icon_ drawInRect:Retina(rect)]; } if (highlighted && kCFCoreFoundationVersionNumber < 800) @@ -8194,11 +8264,18 @@ if (kCFCoreFoundationVersionNumber < 800) { if (!highlighted) UISetColor(Black_); - [origin_ drawAtPoint:CGPointMake(52, 8) forWidth:(width - 61) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; + [origin_ drawAtPoint:CGPointMake(52, 8) forWidth:(width - 49) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; if (!highlighted) UISetColor(Gray_); - [label_ drawAtPoint:CGPointMake(52, 29) forWidth:(width - 61) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; + [label_ drawAtPoint:CGPointMake(52, 29) forWidth:(width - 49) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; +} + +- (void) setFetch:(NSNumber *)fetch { + if ([fetch boolValue]) + [indicator_ startAnimating]; + else + [indicator_ stopAnimating]; } @end @@ -8219,10 +8296,8 @@ if (kCFCoreFoundationVersionNumber < 800) { _H hud_; _H error_; - //NSURLConnection *installer_; NSURLConnection *trivial_bz2_; NSURLConnection *trivial_gz_; - //NSURLConnection *automatic_; BOOL cydia_; } @@ -8243,10 +8318,8 @@ if (kCFCoreFoundationVersionNumber < 800) { } - (void) dealloc { - //[self _releaseConnection:installer_]; [self _releaseConnection:trivial_gz_]; [self _releaseConnection:trivial_bz2_]; - //[self _releaseConnection:automatic_]; [super dealloc]; } @@ -8312,7 +8385,7 @@ if (kCFCoreFoundationVersionNumber < 800) { source:[self sourceAtIndexPath:indexPath] ] autorelease]); - [controller setDelegate:delegate_]; + [controller setDelegate:self.delegate]; [[self navigationController] pushViewController:controller animated:YES]; } @@ -8330,18 +8403,20 @@ if (kCFCoreFoundationVersionNumber < 800) { if (source == nil) return; [Sources_ removeObjectForKey:[source key]]; - Changed_ = true; - [delegate_ _saveConfig]; - [delegate_ reloadDataWithInvocation:nil]; + [self.delegate syncData]; } } +- (void) tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { + [self updateButtonsForEditingStatusAnimated:YES]; +} + - (void) complete { - [delegate_ addTrivialSource:href_]; + [self.delegate addTrivialSource:href_]; href_ = nil; - [delegate_ syncData]; + [self.delegate syncData]; } - (NSString *) getWarning { @@ -8380,9 +8455,9 @@ if (kCFCoreFoundationVersionNumber < 800) { ) { NSString *warning(cydia_ ? [self yieldToSelector:@selector(getWarning)] : nil); - [delegate_ releaseNetworkActivityIndicator]; + [self.delegate releaseNetworkActivityIndicator]; - [delegate_ removeProgressHUD:hud_]; + [self.delegate removeProgressHUD:hud_]; hud_ = nil; if (cydia_) { @@ -8488,24 +8563,20 @@ if (kCFCoreFoundationVersionNumber < 800) { switch (button) { case 1: { NSString *href = [[alert textField] text]; - - //installer_ = [[self _requestHRef:href method:@"GET"] retain]; - - if (![href hasSuffix:@"/"]) - href_ = [href stringByAppendingString:@"/"]; - else - href_ = href; + href = VerifySource(href); + if (href == nil) + break; + href_ = href; trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; - //trivial_bz2_ = [[self _requestHRef:[href stringByAppendingString:@"dists/Release"] method:@"HEAD"] retain]; cydia_ = false; // XXX: this is stupid - hud_ = [delegate_ addProgressHUD]; + hud_ = [self.delegate addProgressHUD]; [hud_ setText:UCLocalize("VERIFYING_URL")]; - [delegate_ retainNetworkActivityIndicator]; + [self.delegate retainNetworkActivityIndicator]; } break; case 0: @@ -8535,6 +8606,39 @@ if (kCFCoreFoundationVersionNumber < 800) { } } +- (void) updateButtonsForEditingStatusAnimated:(BOOL)animated { + BOOL editing([list_ isEditing]); + + if (editing) + [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] + initWithTitle:UCLocalize("ADD") + style:UIBarButtonItemStylePlain + target:self + action:@selector(addButtonClicked) + ] autorelease] animated:animated]; + else if ([self.delegate updating]) + [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] + initWithTitle:UCLocalize("CANCEL") + style:UIBarButtonItemStyleDone + target:self + action:@selector(cancelButtonClicked) + ] autorelease] animated:animated]; + else + [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] + initWithTitle:UCLocalize("REFRESH") + style:UIBarButtonItemStylePlain + target:self + action:@selector(refreshButtonClicked) + ] autorelease] animated:animated]; + + [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] + initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) + style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) + target:self + action:@selector(editButtonClicked) + ] autorelease] animated:animated]; +} + - (void) loadView { list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease]; [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; @@ -8574,6 +8678,7 @@ if (kCFCoreFoundationVersionNumber < 800) { - (void) reloadData { [super reloadData]; + [self updateButtonsForEditingStatusAnimated:YES]; @synchronized (database_) { era_ = [database_ era]; @@ -8611,7 +8716,7 @@ if (kCFCoreFoundationVersionNumber < 800) { [alert setNumberOfRows:1]; [alert addTextFieldWithValue:@"http://" label:@""]; - UITextInputTraits *traits = [[alert textField] textInputTraits]; + NSObject *traits = [[alert textField] textInputTraits]; [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; [traits setKeyboardType:UIKeyboardTypeURL]; @@ -8625,22 +8730,13 @@ if (kCFCoreFoundationVersionNumber < 800) { [self showAddSourcePrompt]; } -- (void) updateButtonsForEditingStatusAnimated:(BOOL)animated { - BOOL editing([list_ isEditing]); - - [[self navigationItem] setLeftBarButtonItem:(editing ? [[[UIBarButtonItem alloc] - initWithTitle:UCLocalize("ADD") - style:UIBarButtonItemStylePlain - target:self - action:@selector(addButtonClicked) - ] autorelease] : [[self navigationItem] backBarButtonItem]) animated:animated]; +- (void) refreshButtonClicked { + if ([self.delegate requestUpdate]) + [self updateButtonsForEditingStatusAnimated:YES]; +} - [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] - initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) - style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) - target:self - action:@selector(editButtonClicked) - ] autorelease] animated:animated]; +- (void) cancelButtonClicked { + [self.delegate cancelUpdate]; } - (void) editButtonClicked { @@ -8671,7 +8767,7 @@ if (kCFCoreFoundationVersionNumber < 800) { spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; CGRect spinrect = [spinner_ frame]; - spinrect.origin.x = ([[self view] frame].size.width / 2) - (spinrect.size.width / 2); + spinrect.origin.x = Retina([[self view] frame].size.width / 2 - spinrect.size.width / 2); spinrect.origin.y = [[self view] frame].size.height - 80.0f; [spinner_ setFrame:spinrect]; [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; @@ -8682,7 +8778,7 @@ if (kCFCoreFoundationVersionNumber < 800) { captrect.size.width = [[self view] frame].size.width; captrect.size.height = 40.0f; captrect.origin.x = 0; - captrect.origin.y = ([[self view] frame].size.height / 2) - (captrect.size.height * 2); + captrect.origin.y = Retina([[self view] frame].size.height / 2 - captrect.size.height * 2); caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; @@ -8690,14 +8786,14 @@ if (kCFCoreFoundationVersionNumber < 800) { [caption_ setTextColor:[UIColor whiteColor]]; [caption_ setBackgroundColor:[UIColor clearColor]]; [caption_ setShadowColor:[UIColor blackColor]]; - [caption_ setTextAlignment:UITextAlignmentCenter]; + [caption_ setTextAlignment:NSTextAlignmentCenter]; [view addSubview:caption_]; CGRect statusrect; statusrect.size.width = [[self view] frame].size.width; statusrect.size.height = 30.0f; statusrect.origin.x = 0; - statusrect.origin.y = ([[self view] frame].size.height / 2) - statusrect.size.height; + statusrect.origin.y = Retina([[self view] frame].size.height / 2 - statusrect.size.height); status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; @@ -8705,78 +8801,30 @@ if (kCFCoreFoundationVersionNumber < 800) { [status_ setTextColor:[UIColor whiteColor]]; [status_ setBackgroundColor:[UIColor clearColor]]; [status_ setShadowColor:[UIColor blackColor]]; - [status_ setTextAlignment:UITextAlignmentCenter]; + [status_ setTextAlignment:NSTextAlignmentCenter]; [view addSubview:status_]; } -- (void) releaseSubviews { - spinner_ = nil; - status_ = nil; - caption_ = nil; - - [super releaseSubviews]; -} - -@end -/* }}} */ - -@interface CYURLCache : SDURLCache { -} - -@end - -@implementation CYURLCache - -- (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request { -#if !ForRelease - if (false); - else if ([event isEqualToString:@"no-cache"]) - event = @"!!!"; - else if ([event isEqualToString:@"store"]) - event = @">>>"; - else if ([event isEqualToString:@"invalid"]) - event = @"???"; - else if ([event isEqualToString:@"memory"]) - event = @"mem"; - else if ([event isEqualToString:@"disk"]) - event = @"ssd"; - else if ([event isEqualToString:@"miss"]) - event = @"---"; - - NSLog(@"%@: %@", event, [[request URL] absoluteString]); -#endif -} - -- (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request { - if (NSURLResponse *response = [cached response]) - if (NSString *mime = [response MIMEType]) - if ([mime isEqualToString:@"text/cache-manifest"]) { - NSURL *url([response URL]); - -#if !ForRelease - NSLog(@"###: %@", [url absoluteString]); -#endif - - @synchronized (HostConfig_) { - [CachedURLs_ addObject:url]; - } - } +- (void) releaseSubviews { + spinner_ = nil; + status_ = nil; + caption_ = nil; - [super storeCachedResponse:cached forRequest:request]; + [super releaseSubviews]; } @end +/* }}} */ -@interface Cydia : UIApplication < +@interface Cydia : CyteApplication < ConfirmationControllerDelegate, DatabaseDelegate, - CydiaDelegate, - UINavigationControllerDelegate, - UITabBarControllerDelegate + CydiaDelegate > { - _H window_; + _H window_; _H tabbar_; - _H emulated_; + _H emulated_; + _H appcache_; _H essential_; _H broken_; @@ -8821,6 +8869,30 @@ if (kCFCoreFoundationVersionNumber < 800) { [tabbar_ beginUpdate]; } +- (void) cancelUpdate { + [tabbar_ cancelUpdate]; +} + +- (bool) requestUpdate { + if (IsReachable("cydia.saurik.com")) { + [self beginUpdate]; + return true; + } else { + UIAlertView *alert = [[[UIAlertView alloc] + initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("REFRESH")] + message:@"Host Unreachable" // XXX: Localize + delegate:self + cancelButtonTitle:UCLocalize("OK") + otherButtonTitles:nil + ] autorelease]; + + [alert setContext:@"norefresh"]; + [alert show]; + + return false; + } +} + - (BOOL) updating { return [tabbar_ updating]; } @@ -8865,30 +8937,17 @@ if (kCFCoreFoundationVersionNumber < 800) { [self _loaded]; } -- (void) _saveConfig { - @synchronized (database_) { - _trace(); - MetaFile_.Sync(); - _trace(); - } - - if (Changed_) { - NSString *error(nil); - - if (NSData *data = [NSPropertyListSerialization dataFromPropertyList:Metadata_ format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error]) { - _trace(); - NSError *error(nil); - if (![data writeToFile:@"/var/lib/cydia/metadata.plist" options:NSAtomicWrite error:&error]) - NSLog(@"failure to save metadata data: %@", error); - _trace(); - - Changed_ = false; - } else { - NSLog(@"failure to serialize metadata: %@", error); - } - } +- (void) reloadSpringBoard { + if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x + system("/usr/libexec/cydia/cydo /bin/launchctl stop com.apple.backboardd"); + else + system("/usr/libexec/cydia/cydo /bin/launchctl stop com.apple.SpringBoard"); + sleep(15); + system("/usr/bin/killall backboardd SpringBoard"); +} - CydiaWriteSources(); +- (void) _saveConfig { + SaveConfig(database_); } // Navigation controller for the queuing badge. @@ -8897,13 +8956,9 @@ if (kCFCoreFoundationVersionNumber < 800) { return [controllers objectAtIndex:3]; } -- (void) unloadData { - [tabbar_ unloadData]; -} - - (void) _updateData { [self _saveConfig]; - [self unloadData]; + [window_ unloadData]; UINavigationController *navigation = [self queueNavigationController]; @@ -8915,13 +8970,15 @@ if (kCFCoreFoundationVersionNumber < 800) { [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; } -- (void) _refreshIfPossible:(NSDate *)update { +- (void) _refreshIfPossible { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSDate *update([[NSDictionary dictionaryWithContentsOfFile:@ CacheState_] objectForKey:@"LastUpdate"]); + bool recently = false; if (update != nil) { NSTimeInterval interval([update timeIntervalSinceNow]); - if (interval <= 0 && interval > -(15*60)) + if (interval > -(15*60)) recently = true; } @@ -8939,17 +8996,18 @@ if (kCFCoreFoundationVersionNumber < 800) { // We are going to load, so remember that. loaded_ = true; - [tabbar_ performSelectorOnMainThread:@selector(setUpdate:) withObject:update waitUntilDone:NO]; + [tabbar_ performSelectorOnMainThread:@selector(beginUpdate) withObject:nil waitUntilDone:NO]; } [pool release]; } - (void) refreshIfPossible { - [NSThread detachNewThreadSelector:@selector(_refreshIfPossible:) toTarget:self withObject:[Metadata_ objectForKey:@"LastUpdate"]]; + [NSThread detachNewThreadSelector:@selector(_refreshIfPossible) toTarget:self withObject:nil]; } - (void) reloadDataWithInvocation:(NSInvocation *)invocation { +_profile(reloadDataWithInvocation) @synchronized (self) { UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); if (hud != nil) @@ -8962,6 +9020,7 @@ if (kCFCoreFoundationVersionNumber < 800) { [essential_ removeAllObjects]; [broken_ removeAllObjects]; + _profile(reloadDataWithInvocation$Essential) NSArray *packages([database_ packages]); for (Package *package in packages) { if ([package half]) @@ -8972,6 +9031,7 @@ if (kCFCoreFoundationVersionNumber < 800) { ++changes; } } + _end UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; if (changes != 0) { @@ -8987,11 +9047,16 @@ if (kCFCoreFoundationVersionNumber < 800) { [self setApplicationIconBadgeNumber:0]; } + Queuing_ = false; [self _updateData]; if (hud != nil) [self removeProgressHUD:hud]; -} } +} +_end + + PrintTimes(); +} - (void) updateData { [self _updateData]; @@ -9012,18 +9077,14 @@ if (kCFCoreFoundationVersionNumber < 800) { if (emulated_ == nil) return; - [window_ addSubview:[tabbar_ view]]; - if ([window_ respondsToSelector:@selector(setRootViewController:)]) - [window_ setRootViewController:tabbar_]; - [[emulated_ view] removeFromSuperview]; + [window_ setRootViewController:tabbar_]; emulated_ = nil; + [window_ setUserInteractionEnabled:YES]; } - (void) presentModalViewController:(UIViewController *)controller force:(BOOL)force { UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:controller] autorelease]); - if (IsWildcat_) - [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; UIViewController *parent; if (emulated_ == nil) @@ -9035,6 +9096,8 @@ if (kCFCoreFoundationVersionNumber < 800) { parent = tabbar_; } + if (IsWildcat_) + [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; [parent presentModalViewController:navigation animated:YES]; } @@ -9083,12 +9146,10 @@ if (kCFCoreFoundationVersionNumber < 800) { CydiaAddSource(href, distribution, sections); } -- (void) addTrivialSource:(NSString *)href { +// XXX: this method should not return anything +- (BOOL) addTrivialSource:(NSString *)href { CydiaAddSource(href, @"./"); -} - -- (void) updateValues { - Changed_ = true; + return YES; } - (void) resolve { @@ -9170,7 +9231,7 @@ if (kCFCoreFoundationVersionNumber < 800) { - (void) _uicache { _trace(); - system("su -c /usr/bin/uicache mobile"); + system("/usr/bin/uicache"); _trace(); } @@ -9243,12 +9304,14 @@ if (kCFCoreFoundationVersionNumber < 800) { @synchronized (self) { for (Package *broken in (id) broken_) { [broken remove]; - - NSString *id = [broken id]; - unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]); - unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]); - unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]); - unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]); + NSString *id(ShellEscape([broken id])); + system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/rm -f" + " /var/lib/dpkg/info/%@.prerm" + " /var/lib/dpkg/info/%@.postrm" + " /var/lib/dpkg/info/%@.preinst" + " /var/lib/dpkg/info/%@.postinst" + " /var/lib/dpkg/info/%@.extrainst_" + "", id, id, id, id, id] UTF8String]); } [self resolve]; @@ -9302,6 +9365,9 @@ if (kCFCoreFoundationVersionNumber < 800) { return false; } + if ([tabbar_ modalViewController] != nil) + return false; + // Use external process status API internally. // This is probably a really bad idea. // XXX: what is the point of this? does this solve anything at all? @@ -9325,7 +9391,22 @@ if (kCFCoreFoundationVersionNumber < 800) { return true; } -- (void) applicationSuspend:(__GSEvent *)event { +- (void) suspendReturningToLastApp:(BOOL)returning { + if ([self isSafeToSuspend]) + [super suspendReturningToLastApp:returning]; +} + +- (void) suspend { + if ([self isSafeToSuspend]) + [super suspend]; +} + +- (void) applicationSuspend { + if ([self isSafeToSuspend]) + [super applicationSuspend]; +} + +- (void) applicationSuspend:(GSEventRef)event { if ([self isSafeToSuspend]) [super applicationSuspend:event]; } @@ -9433,7 +9514,7 @@ if (kCFCoreFoundationVersionNumber < 800) { controller = [[[SectionController alloc] initWithDatabase:database_ source:nil section:argument] autorelease]; } - if (!external && [base isEqualToString:@"sources"]) { + if ([base isEqualToString:@"sources"]) { if ([argument isEqualToString:@"add"]) { controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; [(SourcesController *)controller showAddSourcePrompt]; @@ -9501,10 +9582,11 @@ if (kCFCoreFoundationVersionNumber < 800) { } - (void) saveState { - [Metadata_ setObject:[tabbar_ navigationURLCollection] forKey:@"InterfaceState"]; - [Metadata_ setObject:[NSDate date] forKey:@"LastClosed"]; - [Metadata_ setObject:[NSNumber numberWithInt:[tabbar_ selectedIndex]] forKey:@"InterfaceIndex"]; - Changed_ = true; + [[NSDictionary dictionaryWithObjectsAndKeys: + @"InterfaceState", [tabbar_ navigationURLCollection], + @"LastClosed", [NSDate date], + @"InterfaceIndex", [NSNumber numberWithInt:[tabbar_ selectedIndex]], + nil] writeToFile:@ SavedState_ atomically:YES]; [self _saveConfig]; } @@ -9513,8 +9595,37 @@ if (kCFCoreFoundationVersionNumber < 800) { [self saveState]; } +- (void) applicationDidEnterBackground:(UIApplication *)application { + if (kCFCoreFoundationVersionNumber < 1000 && [self isSafeToSuspend]) + return [self terminateWithSuccess]; + Backgrounded_ = [NSDate date]; + [self saveState]; +} + +- (void) applicationWillEnterForeground:(UIApplication *)application { + if (Backgrounded_ == nil) + return; + + NSTimeInterval interval([Backgrounded_ timeIntervalSinceNow]); + + if (interval <= -(30*60)) { + [tabbar_ setSelectedIndex:0]; + [[[tabbar_ viewControllers] objectAtIndex:0] popToRootViewControllerAnimated:NO]; + } + + if (interval <= -(15*60)) { + if (IsReachable("cydia.saurik.com")) { + [tabbar_ beginUpdate]; + [appcache_ reloadURLWithCache:YES]; + } + } + + if ([database_ delocked]) + [self reloadData]; +} + - (void) setConfigurationData:(NSString *)data { - static Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$"); + static RegEx conffile_r("'(.*)' '(.*)' ([01]) ([01])"); if (!conffile_r(data)) { lprintf("E:invalid conffile\n"); @@ -9555,90 +9666,21 @@ if (kCFCoreFoundationVersionNumber < 800) { - (void) stash { [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; UpdateExternalStatus(1); - [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/free.sh"]; + [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/free.sh"]; UpdateExternalStatus(0); [self removeStashController]; - - pid_t pid(ExecFork()); - if (pid == 0) { - execlp("launchctl", "launchctl", "stop", "com.apple.SpringBoard", NULL); - perror("launchctl stop"); - - exit(0); - } ReapZombie(pid); -} - -- (void) setupViewControllers { - tabbar_ = [[[CydiaTabBarController alloc] initWithDatabase:database_] autorelease]; - - NSMutableArray *items; - if (kCFCoreFoundationVersionNumber < 800) { - items = [NSMutableArray arrayWithObjects: - [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage applicationImageNamed:@"home.png"] tag:0] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage applicationImageNamed:@"install.png"] tag:0] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage applicationImageNamed:@"changes.png"] tag:0] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage applicationImageNamed:@"manage.png"] tag:0] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage applicationImageNamed:@"search.png"] tag:0] autorelease], - nil]; - } else { - items = [NSMutableArray arrayWithObjects: - [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage applicationImageNamed:@"home7.png"] selectedImage:[UIImage applicationImageNamed:@"home7s.png"]] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage applicationImageNamed:@"install7.png"] selectedImage:[UIImage applicationImageNamed:@"install7s.png"]] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage applicationImageNamed:@"changes7.png"] selectedImage:[UIImage applicationImageNamed:@"changes7s.png"]] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage applicationImageNamed:@"manage7.png"] selectedImage:[UIImage applicationImageNamed:@"manage7s.png"]] autorelease], - [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage applicationImageNamed:@"search7.png"] selectedImage:[UIImage applicationImageNamed:@"search7s.png"]] autorelease], - nil]; - } - - NSMutableArray *controllers([NSMutableArray array]); - for (UITabBarItem *item in items) { - UINavigationController *controller([[[UINavigationController alloc] init] autorelease]); - [controller setTabBarItem:item]; - [controllers addObject:controller]; - } - [tabbar_ setViewControllers:controllers]; - - [tabbar_ setUpdateDelegate:self]; -} - -- (void) _sendMemoryWarningNotification { - if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: maybe 4_0? - [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification" object:[UIApplication sharedApplication]]; - else - [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidReceiveMemoryWarningNotification" object:[UIApplication sharedApplication]]; -} - -- (void) _sendMemoryWarningNotifications { - while (true) { - [self performSelectorOnMainThread:@selector(_sendMemoryWarningNotification) withObject:nil waitUntilDone:NO]; - sleep(2); - //usleep(2000000); - } -} - -- (void) applicationDidReceiveMemoryWarning:(UIApplication *)application { - NSLog(@"--"); - [[NSURLCache sharedURLCache] removeAllCachedResponses]; + [self reloadSpringBoard]; } - (void) applicationDidFinishLaunching:(id)unused { - //[NSThread detachNewThreadSelector:@selector(_sendMemoryWarningNotifications) toTarget:self withObject:nil]; - + [super applicationDidFinishLaunching:unused]; _trace(); - if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) - [self setApplicationSupportsShakeToEdit:NO]; @synchronized (HostConfig_) { [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; } - [NSURLCache setSharedURLCache:[[[CYURLCache alloc] - initWithMemoryCapacity:524288 - diskCapacity:10485760 - diskPath:[NSString stringWithFormat:@"%@/SDURLCache", Cache_] - ] autorelease]]; - [CydiaWebViewController _initialize]; [NSURLProtocol registerClass:[CydiaURLProtocol class]]; @@ -9657,13 +9699,17 @@ _trace(); broken_ = [NSMutableArray arrayWithCapacity:4]; // XXX: I really need this thing... like, seriously... I'm sorry - [[[AppCacheController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] reloadData]; + appcache_ = [[[AppCacheController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] autorelease]; + [appcache_ reloadData]; - window_ = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; + window_ = [[[CyteWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; [window_ orderFront:self]; [window_ makeKey:self]; [window_ setHidden:NO]; + if (access("/.cydia_no_stash", F_OK) == 0); + else { + if (false) stash: { [self addStashController]; // XXX: this would be much cleaner as a yieldToSelector: @@ -9694,20 +9740,40 @@ _trace(); Stash_("/Library/Wallpaper"); //Stash_("/usr/bin"); Stash_("/usr/include"); - Stash_("/usr/lib/pam"); Stash_("/usr/share"); //Stash_("/var/lib"); + } + database_ = [Database sharedInstance]; [database_ setDelegate:self]; [window_ setUserInteractionEnabled:NO]; - [self setupViewControllers]; - emulated_ = [[[CydiaLoadingViewController alloc] init] autorelease]; - [window_ addSubview:[emulated_ view]]; - if ([window_ respondsToSelector:@selector(setRootViewController:)]) - [window_ setRootViewController:emulated_]; + tabbar_ = [[[CydiaTabBarController alloc] initWithDatabase:database_] autorelease]; + + [tabbar_ addViewControllers:nil, + @"Cydia", @"home.png", @"home7.png", @"home7s.png", + UCLocalize("SOURCES"), @"install.png", @"install7.png", @"install7s.png", + UCLocalize("CHANGES"), @"changes.png", @"changes7.png", @"changes7s.png", + UCLocalize("INSTALLED"), @"manage.png", @"manage7.png", @"manage7s.png", + UCLocalize("SEARCH"), @"search.png", @"search7.png", @"search7s.png", + nil]; + + [tabbar_ setUpdateDelegate:self]; + + CydiaLoadingViewController *loading([[[CydiaLoadingViewController alloc] init] autorelease]); + UINavigationController *navigation([[[UINavigationController alloc] init] autorelease]); + [navigation setViewControllers:[NSArray arrayWithObject:loading]]; + + emulated_ = [[[CyteTabBarController alloc] init] autorelease]; + [emulated_ setViewControllers:[NSArray arrayWithObject:navigation]]; + [emulated_ setSelectedIndex:0]; + + if ([emulated_ respondsToSelector:@selector(concealTabBarSelection)]) + [emulated_ concealTabBarSelection]; + + [window_ setRootViewController:emulated_]; [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; _trace(); @@ -9731,12 +9797,12 @@ _trace(); [self reloadDataWithInvocation:nil]; [self refreshIfPossible]; - PrintTimes(); - [self disemulate]; - int savedIndex = [[Metadata_ objectForKey:@"InterfaceIndex"] intValue]; - NSArray *saved = [[[Metadata_ objectForKey:@"InterfaceState"] mutableCopy] autorelease]; + NSDictionary *state([NSDictionary dictionaryWithContentsOfFile:@ SavedState_]); + + int savedIndex = [[state objectForKey:@"InterfaceIndex"] intValue]; + NSArray *saved = [[[state objectForKey:@"InterfaceState"] mutableCopy] autorelease]; int standardIndex = 0; NSArray *standard = [self defaultStartPages]; @@ -9745,10 +9811,9 @@ _trace(); if (saved == nil) valid = NO; - NSDate *closed = [Metadata_ objectForKey:@"LastClosed"]; + NSDate *closed = [state objectForKey:@"LastClosed"]; if (valid && closed != nil) { NSTimeInterval interval([closed timeIntervalSinceNow]); - // XXX: Is 30 minutes the optimal time here? if (interval <= -(30*60)) valid = NO; } @@ -9795,12 +9860,17 @@ _trace(); // (Try to) show the startup URL. if (starturl_ != nil) { - [self openCydiaURL:starturl_ forExternal:NO]; + [self openCydiaURL:starturl_ forExternal:YES]; starturl_ = nil; } } - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { + if (!IsWildcat_) { + [sheet addButtonWithTitle:UCLocalize("CANCEL")]; + [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; + } + if (item != nil && IsWildcat_) { [sheet showFromBarButtonItem:item animated:YES]; } else { @@ -9840,124 +9910,46 @@ id Dealloc_(id self, SEL selector) { return object; }*/ -static NSSet *MobilizedFiles_; - -static NSURL *MobilizeURL(NSURL *url) { - NSString *path([url path]); - if ([path hasPrefix:@"/var/root/"]) { - NSString *file([path substringFromIndex:10]); - if ([MobilizedFiles_ containsObject:file]) - url = [NSURL fileURLWithPath:[@"/var/mobile/" stringByAppendingString:file] isDirectory:NO]; - } - - return url; -} - -Class $CFXPreferencesPropertyListSource; -@class CFXPreferencesPropertyListSource; - -MSHook(BOOL, CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync, CFXPreferencesPropertyListSource *self, SEL _cmd) { - NSURL *&url(MSHookIvar(self, "_url")), *old(url); - NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); - url = MobilizeURL(url); - BOOL value(_CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync(self, _cmd)); - //NSLog(@"%@ %s", [url absoluteString], value ? "YES" : "NO"); - url = old; - [pool release]; - return value; -} - -MSHook(void *, CFXPreferencesPropertyListSource$createPlistFromDisk, CFXPreferencesPropertyListSource *self, SEL _cmd) { - NSURL *&url(MSHookIvar(self, "_url")), *old(url); - NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); - url = MobilizeURL(url); - void *value(_CFXPreferencesPropertyListSource$createPlistFromDisk(self, _cmd)); - //NSLog(@"%@ %@", [url absoluteString], value); - url = old; - [pool release]; - return value; +static NSMutableDictionary *AutoreleaseDeepMutableCopyOfDictionary(CFTypeRef type) { + if (type == NULL) + return nil; + if (CFGetTypeID(type) != CFDictionaryGetTypeID()) + return nil; + CFTypeRef copy(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, type, kCFPropertyListMutableContainers)); + CFRelease(type); + return [(NSMutableDictionary *) copy autorelease]; } -Class $NSURLConnection; - -MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) { - NSMutableURLRequest *copy([[request mutableCopy] autorelease]); - - NSURL *url([copy URL]); - - NSString *host([url host]); - NSString *scheme([[url scheme] lowercaseString]); - - NSString *compound([NSString stringWithFormat:@"%@:%@", scheme, host]); +int main_store(int, char *argv[]); - @synchronized (HostConfig_) { - if ([copy respondsToSelector:@selector(setHTTPShouldUsePipelining:)]) - if ([PipelinedHosts_ containsObject:host] || [PipelinedHosts_ containsObject:compound]) - [copy setHTTPShouldUsePipelining:YES]; - - if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"]) - if ([control isEqualToString:@"max-age=0"]) - if ([CachedURLs_ containsObject:url]) { -#if !ForRelease - NSLog(@"~~~: %@", url); +int main(int argc, char *argv[]) { +#ifdef __arm64__ + const char *argv0(argv[0]); + if (const char *slash = strrchr(argv0, '/')) + argv0 = slash + 1; + if (false); + else if (!strcmp(argv0, "store")) + return main_store(argc, argv); #endif - [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; - - [copy setValue:nil forHTTPHeaderField:@"Cache-Control"]; - [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"]; - [copy setValue:nil forHTTPHeaderField:@"If-None-Match"]; - } - } - - if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) { - } return self; -} - -Class $WAKWindow; - -static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) { - CGSize size([[UIScreen mainScreen] bounds].size); - /*if ([$WAKWindow respondsToSelector:@selector(hasLandscapeOrientation)]) - if ([$WAKWindow hasLandscapeOrientation]) - std::swap(size.width, size.height);*/ - return size; -} - -Class $NSUserDefaults; - -MSHook(id, NSUserDefaults$objectForKey$, NSUserDefaults *self, SEL _cmd, NSString *key) { - if ([key respondsToSelector:@selector(isEqualToString:)] && [key isEqualToString:@"WebKitLocalStorageDatabasePathPreferenceKey"]) - return [NSString stringWithFormat:@"%@/LocalStorage", Cache_]; - return _NSUserDefaults$objectForKey$(self, _cmd, key); -} + int fd(open("/tmp/cydia.log", O_WRONLY | O_APPEND | O_CREAT, 0644)); + dup2(fd, 2); + close(fd); -int main(int argc, char *argv[]) { NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); _trace(); UpdateExternalStatus(0); - UIScreen *screen([UIScreen mainScreen]); - if ([screen respondsToSelector:@selector(scale)]) - ScreenScale_ = [screen scale]; - else - ScreenScale_ = 1; - - UIDevice *device([UIDevice currentDevice]); - if ([device respondsToSelector:@selector(userInterfaceIdiom)]) { - UIUserInterfaceIdiom idiom([device userInterfaceIdiom]); - if (idiom == UIUserInterfaceIdiomPad) - IsWildcat_ = true; - } - Idiom_ = IsWildcat_ ? @"ipad" : @"iphone"; - Pcre pattern("^([0-9]+\\.[0-9]+)"); + RegEx pattern("([0-9]+\\.[0-9]+).*"); + UIDevice *device([UIDevice currentDevice]); if (pattern([device systemVersion])) Firmware_ = pattern[1]; + if (pattern(Cydia_)) Major_ = pattern[1]; @@ -9966,10 +9958,7 @@ int main(int argc, char *argv[]) { HostConfig_ = [[[NSObject alloc] init] autorelease]; @synchronized (HostConfig_) { BridgedHosts_ = [NSMutableSet setWithCapacity:4]; - TokenHosts_ = [NSMutableSet setWithCapacity:4]; InsecureHosts_ = [NSMutableSet setWithCapacity:4]; - PipelinedHosts_ = [NSMutableSet setWithCapacity:4]; - CachedURLs_ = [NSMutableSet setWithCapacity:32]; } NSString *ui(@"ui/ios"); @@ -9980,112 +9969,94 @@ int main(int argc, char *argv[]) { PackageName = reinterpret_cast(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); - MobilizedFiles_ = [NSMutableSet setWithObjects: - @"Library/Preferences/com.apple.Accessibility.plist", - @"Library/Preferences/com.apple.preferences.sounds.plist", - nil]; - - /* Library Hacks {{{ */ - class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); - - $WAKWindow = objc_getClass("WAKWindow"); - if ($WAKWindow != NULL) - if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize))) - method_setImplementation(method, (IMP) &$WAKWindow$screenSize); - - $CFXPreferencesPropertyListSource = objc_getClass("CFXPreferencesPropertyListSource"); - - Method CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync(class_getInstanceMethod($CFXPreferencesPropertyListSource, @selector(_backingPlistChangedSinceLastSync))); - if (CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync != NULL) { - _CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync = reinterpret_cast(method_getImplementation(CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync)); - method_setImplementation(CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync, reinterpret_cast(&$CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync)); - } - - Method CFXPreferencesPropertyListSource$createPlistFromDisk(class_getInstanceMethod($CFXPreferencesPropertyListSource, @selector(createPlistFromDisk))); - if (CFXPreferencesPropertyListSource$createPlistFromDisk != NULL) { - _CFXPreferencesPropertyListSource$createPlistFromDisk = reinterpret_cast(method_getImplementation(CFXPreferencesPropertyListSource$createPlistFromDisk)); - method_setImplementation(CFXPreferencesPropertyListSource$createPlistFromDisk, reinterpret_cast(&$CFXPreferencesPropertyListSource$createPlistFromDisk)); - } - - $NSURLConnection = objc_getClass("NSURLConnection"); - Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:))); - if (NSURLConnection$init$ != NULL) { - _NSURLConnection$init$ = reinterpret_cast(method_getImplementation(NSURLConnection$init$)); - method_setImplementation(NSURLConnection$init$, reinterpret_cast(&$NSURLConnection$init$)); - } - - $NSUserDefaults = objc_getClass("NSUserDefaults"); - Method NSUserDefaults$objectForKey$(class_getInstanceMethod($NSUserDefaults, @selector(objectForKey:))); - if (NSUserDefaults$objectForKey$ != NULL) { - _NSUserDefaults$objectForKey$ = reinterpret_cast(method_getImplementation(NSUserDefaults$objectForKey$)); - method_setImplementation(NSUserDefaults$objectForKey$, reinterpret_cast(&$NSUserDefaults$objectForKey$)); - } - /* }}} */ /* Set Locale {{{ */ Locale_ = CFLocaleCopyCurrent(); Languages_ = [NSLocale preferredLanguages]; - //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); - //NSLog(@"%@", [Languages_ description]); + std::string languages; + const char *translation(NULL); - const char *lang; + // XXX: this isn't really a language, but this is compatible with older Cydia builds if (Locale_ != NULL) - lang = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]; - else if (Languages_ != nil && [Languages_ count] != 0) - lang = [[Languages_ objectAtIndex:0] UTF8String]; - else - // XXX: consider just setting to C and then falling through? - lang = NULL; - - if (lang != NULL) { - Pcre pattern("^([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?$"); - lang = !pattern(lang) ? NULL : [pattern->*@"%1$@%2$@" UTF8String]; - } + if (const char *language = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]) { + RegEx pattern("([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?"); + if (pattern(language)) { + translation = strdup([pattern->*@"%1$@%2$@" UTF8String]); + languages += translation; + languages += ","; + } + } - NSLog(@"Setting Language: %s", lang); + if (Languages_ != nil) + for (NSString *locale : Languages_) { + auto components([NSLocale componentsFromLocaleIdentifier:locale]); + NSString *language([components objectForKey:(id)kCFLocaleLanguageCode]); + if (NSString *script = [components objectForKey:(id)kCFLocaleScriptCode]) + language = [NSString stringWithFormat:@"%@-%@", language, script]; + languages += [language UTF8String]; + languages += ","; + } - if (lang != NULL) { - setenv("LANG", lang, true); - std::setlocale(LC_ALL, lang); - } + languages += "en"; + NSLog(@"Setting Language: [%s] %s", translation, languages.c_str()); /* }}} */ + /* Index Collation {{{ */ + if (Class $UILocalizedIndexedCollation = objc_getClass("UILocalizedIndexedCollation")) { @try { + NSBundle *bundle([NSBundle bundleForClass:$UILocalizedIndexedCollation]); + NSString *path([bundle pathForResource:@"UITableViewLocalizedSectionIndex" ofType:@"plist"]); + //path = @"/System/Library/Frameworks/UIKit.framework/.lproj/UITableViewLocalizedSectionIndex.plist"; + NSDictionary *dictionary([NSDictionary dictionaryWithContentsOfFile:path]); + _H collation([[[$UILocalizedIndexedCollation alloc] initWithDictionary:dictionary] autorelease]); + + CollationLocale_ = MSHookIvar(collation, "_locale"); + + if (kCFCoreFoundationVersionNumber >= 800 && [[CollationLocale_ localeIdentifier] isEqualToString:@"zh@collation=stroke"]) { + CollationThumbs_ = [NSArray arrayWithObjects:@"1",@"•",@"4",@"•",@"7",@"•",@"10",@"•",@"13",@"•",@"16",@"•",@"19",@"A",@"•",@"E",@"•",@"I",@"•",@"M",@"•",@"R",@"•",@"V",@"•",@"Z",@"#",nil]; + for (NSInteger offset : (NSInteger[]) {0,1,3,4,6,7,9,10,12,13,15,16,18,25,26,29,30,33,34,37,38,42,43,46,47,50,51}) + CollationOffset_.push_back(offset); + CollationTitles_ = [NSArray arrayWithObjects:@"1 畫",@"2 畫",@"3 畫",@"4 畫",@"5 畫",@"6 畫",@"7 畫",@"8 畫",@"9 畫",@"10 畫",@"11 畫",@"12 畫",@"13 畫",@"14 畫",@"15 畫",@"16 畫",@"17 畫",@"18 畫",@"19 畫",@"20 畫",@"21 畫",@"22 畫",@"23 畫",@"24 畫",@"25 畫以上",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; + CollationStarts_ = [NSArray arrayWithObjects:@"一",@"丁",@"丈",@"不",@"且",@"丞",@"串",@"並",@"亭",@"乘",@"乾",@"傀",@"亂",@"僎",@"僵",@"儐",@"償",@"叢",@"儳",@"嚴",@"儷",@"儻",@"囌",@"囑",@"廳",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"ʒ",nil]; + } else { - apr_app_initialize(&argc, const_cast(&argv), NULL); - - /* Parse Arguments {{{ */ - bool substrate(false); + CollationThumbs_ = [collation sectionIndexTitles]; + for (size_t index(0), end([CollationThumbs_ count]); index != end; ++index) + CollationOffset_.push_back([collation sectionForSectionIndexTitleAtIndex:index]); + + CollationTitles_ = [collation sectionTitles]; + CollationStarts_ = MSHookIvar(collation, "_sectionStartStrings"); + + NSString *&transform(MSHookIvar(collation, "_transform")); + if (&transform != NULL && transform != nil) { + /*if ([collation respondsToSelector:@selector(transformedCollationStringForString:)]) + CollationModify_ = [=](NSString *value) { return [collation transformedCollationStringForString:value]; };*/ + const UChar *uid(reinterpret_cast([transform cStringUsingEncoding:NSUnicodeStringEncoding])); + UErrorCode code(U_ZERO_ERROR); + CollationTransl_ = utrans_openU(uid, -1, UTRANS_FORWARD, NULL, 0, NULL, &code); + if (!U_SUCCESS(code)) + NSLog(@"%s", u_errorName(code)); + } - if (argc != 0) { - char **args(argv); - int arge(1); + } + } @catch (NSException *e) { + NSLog(@"%@", e); + goto hard; + } } else hard: { + CollationLocale_ = [[[NSLocale alloc] initWithLocaleIdentifier:@"en@collation=dictionary"] autorelease]; - for (int argi(1); argi != argc; ++argi) - if (strcmp(argv[argi], "--") == 0) { - arge = argi; - argv[argi] = argv[0]; - argv += argi; - argc -= argi; - break; - } + CollationThumbs_ = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; + for (NSInteger offset(0); offset != 28; ++offset) + CollationOffset_.push_back(offset); - for (int argi(1); argi != arge; ++argi) - if (strcmp(args[argi], "--substrate") == 0) - substrate = true; - else - fprintf(stderr, "unknown argument: %s\n", args[argi]); + CollationTitles_ = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; + CollationStarts_ = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"ʒ",nil]; } /* }}} */ App_ = [[NSBundle mainBundle] bundlePath]; Advanced_ = YES; - setuid(0); - setgid(0); - - if (access("/var/mobile/Library/Keyboard/UserDictionary.sqlite", F_OK) == 0) - system("mkdir -p /var/root/Library/Keyboard; cp -af /var/mobile/Library/Keyboard/UserDictionary.sqlite /var/root/Library/Keyboard/"); - - Cache_ = [[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia", @"/var/root"] retain]; + Cache_ = [[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia", @"/var/mobile"] retain]; + mkdir([Cache_ UTF8String], 0755); /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); alloc_ = alloc->method_imp; @@ -10125,6 +10096,11 @@ int main(int argc, char *argv[]) { else Machine_ = machine; + int64_t usermem(0); + size = sizeof(usermem); + if (sysctlbyname("hw.usermem", &usermem, &size, NULL, 0) == -1) + usermem = 0; + SerialNumber_ = (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber"); ChipID_ = [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString]; BBSNum_ = CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false); @@ -10138,55 +10114,58 @@ int main(int argc, char *argv[]) { NSString *agent([NSString stringWithFormat:@"Cydia/%@ CyF/%.2f", Cydia_, kCFCoreFoundationVersionNumber]); - if (Pcre match = Pcre("^[0-9]+(\\.[0-9]+)+", Safari_)) - agent = [NSString stringWithFormat:@"Safari/%@ %@", match[0], agent]; - if (Pcre match = Pcre("^[0-9]+[A-Z][0-9]+[a-z]?", System_)) - agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[0], agent]; - if (Pcre match = Pcre("^[0-9]+(\\.[0-9]+)+", Product_)) - agent = [NSString stringWithFormat:@"Version/%@ %@", match[0], agent]; + if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Safari_)) + agent = [NSString stringWithFormat:@"Safari/%@ %@", match[1], agent]; + if (RegEx match = RegEx("([0-9]+[A-Z][0-9]+[a-z]?).*", System_)) + agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[1], agent]; + if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Product_)) + agent = [NSString stringWithFormat:@"Version/%@ %@", match[1], agent]; UserAgent_ = agent; /* }}} */ /* Load Database {{{ */ - _trace(); - Metadata_ = [[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]; - _trace(); SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; - if (Metadata_ == NULL) - Metadata_ = [NSMutableDictionary dictionaryWithCapacity:2]; - else { - Settings_ = [Metadata_ objectForKey:@"Settings"]; - - Packages_ = [Metadata_ objectForKey:@"Packages"]; - - Values_ = [Metadata_ objectForKey:@"Values"]; - Sections_ = [Metadata_ objectForKey:@"Sections"]; - Sources_ = [Metadata_ objectForKey:@"Sources"]; + _trace(); + mkdir("/var/mobile/Library/Cydia", 0755); + MetaFile_.Open("/var/mobile/Library/Cydia/metadata.cb0"); + _trace(); - Token_ = [Metadata_ objectForKey:@"Token"]; + Values_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaValues"), CFSTR("com.saurik.Cydia"))); + Sections_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSections"), CFSTR("com.saurik.Cydia"))); + Sources_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSources"), CFSTR("com.saurik.Cydia"))); + Version_ = [(NSNumber *) CFPreferencesCopyAppValue(CFSTR("CydiaVersion"), CFSTR("com.saurik.Cydia")) autorelease]; - Version_ = [Metadata_ objectForKey:@"Version"]; - } + _trace(); + NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]); - if (Values_ == nil) { + if (Values_ == nil) + Values_ = [metadata objectForKey:@"Values"]; + if (Values_ == nil) Values_ = [[[NSMutableDictionary alloc] initWithCapacity:4] autorelease]; - [Metadata_ setObject:Values_ forKey:@"Values"]; - } - if (Sections_ == nil) { + if (Sections_ == nil) + Sections_ = [metadata objectForKey:@"Sections"]; + if (Sections_ == nil) Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; - [Metadata_ setObject:Sections_ forKey:@"Sections"]; - } - if (Sources_ == nil) { + if (Sources_ == nil) + Sources_ = [metadata objectForKey:@"Sources"]; + if (Sources_ == nil) Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; - [Metadata_ setObject:Sources_ forKey:@"Sources"]; - } - if (Version_ == nil) { + // XXX: this wrong, but in a way that doesn't matter :/ + if (Version_ == nil) + Version_ = [metadata objectForKey:@"Version"]; + if (Version_ == nil) Version_ = [NSNumber numberWithUnsignedInt:0]; - [Metadata_ setObject:Version_ forKey:@"Version"]; + + if (NSDictionary *packages = [metadata objectForKey:@"Packages"]) { + bool fail(false); + CFDictionaryApplyFunction((CFDictionaryRef) packages, &PackageImport, &fail); + _trace(); + if (fail) + NSLog(@"unable to import package preferences... from 2010? oh well :/"); } if ([Version_ unsignedIntValue] == 0) { @@ -10196,82 +10175,84 @@ int main(int argc, char *argv[]) { CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./"); Version_ = [NSNumber numberWithUnsignedInt:1]; - [Metadata_ setObject:Version_ forKey:@"Version"]; - - [Metadata_ removeObjectForKey:@"LastUpdate"]; - - Changed_ = true; - } - /* }}} */ - - CydiaWriteSources(); - - _trace(); - MetaFile_.Open("/var/lib/cydia/metadata.cb0"); - _trace(); - - if (Packages_ != nil) { - bool fail(false); - CFDictionaryApplyFunction((CFDictionaryRef) Packages_, &PackageImport, &fail); - _trace(); - if (!fail) { - [Metadata_ removeObjectForKey:@"Packages"]; - Packages_ = nil; - Changed_ = true; + if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:@ CacheState_]) { + [cache removeObjectForKey:@"LastUpdate"]; + [cache writeToFile:@ CacheState_ atomically:YES]; } } - Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; + _H broken([NSMutableArray array]); + for (NSString *key in (id) Sources_) + if ([key rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"# "]].location != NSNotFound || ![([[Sources_ objectForKey:key] objectForKey:@"URI"] ?: @"/") hasSuffix:@"/"]) + [broken addObject:key]; + if ([broken count] != 0) + for (NSString *key in (id) broken) + [Sources_ removeObjectForKey:key]; + broken = nil; -#define MobileSubstrate_(name) \ - if (substrate && access("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", F_OK) == 0) { \ - void *handle(dlopen("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", RTLD_LAZY | RTLD_GLOBAL)); \ - if (handle == NULL) \ - NSLog(@"%s", dlerror()); \ - } + SaveConfig(nil); + system("/usr/libexec/cydia/cydo /bin/rm -f /var/lib/cydia/metadata.plist"); + /* }}} */ - MobileSubstrate_(Activator) - MobileSubstrate_(libstatusbar) - MobileSubstrate_(SimulatedKeyEvents) - MobileSubstrate_(WinterBoard) + Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; - /*if (substrate && access("/Library/MobileSubstrate/MobileSubstrate.dylib", F_OK) == 0) - dlopen("/Library/MobileSubstrate/MobileSubstrate.dylib", RTLD_LAZY | RTLD_GLOBAL);*/ + if (kCFCoreFoundationVersionNumber > 1000) + system("/usr/libexec/cydia/cydo /usr/libexec/cydia/setnsfpn /var/lib"); int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); if (access("/User", F_OK) != 0 || version != 6) { _trace(); - system("/usr/libexec/cydia/firmware.sh"); + system("/usr/libexec/cydia/cydo /usr/libexec/cydia/firmware.sh"); _trace(); } - _assert([[NSFileManager defaultManager] - createDirectoryAtPath:@"/var/cache/apt/archives/partial" - withIntermediateDirectories:YES - attributes:nil - error:NULL - ]); - if (access("/tmp/cydia.chk", F_OK) == 0) { - if (unlink("/var/cache/apt/pkgcache.bin") == -1) + if (unlink([Cache("pkgcache.bin") UTF8String]) == -1) _assert(errno == ENOENT); - if (unlink("/var/cache/apt/srcpkgcache.bin") == -1) + if (unlink([Cache("srcpkgcache.bin") UTF8String]) == -1) _assert(errno == ENOENT); } + system("/usr/libexec/cydia/cydo /bin/ln -sf /var/mobile/Library/Caches/com.saurik.Cydia/sources.list /etc/apt/sources.list.d/cydia.list"); + /* APT Initialization {{{ */ _assert(pkgInitConfig(*_config)); _assert(pkgInitSystem(*_config, _system)); - if (lang != NULL) - _config->Set("APT::Acquire::Translation", lang); + _config->Set("Acquire::AllowInsecureRepositories", true); + _config->Set("Acquire::Check-Valid-Until", false); + _config->Set("Dir::Bin::Methods::store", "/Applications/Cydia.app/store"); + + _config->Set("pkgCacheGen::ForceEssential", ""); + + if (translation != NULL) + _config->Set("APT::Acquire::Translation", translation); + _config->Set("Acquire::Languages", languages); // XXX: this timeout might be important :( //_config->Set("Acquire::http::Timeout", 15); - _config->Set("Acquire::http::MaxParallel", 3); + _config->Set("Acquire::http::MaxParallel", usermem >= 384 * 1024 * 1024 ? 16 : 3); + + mkdir([Cache("archives") UTF8String], 0755); + mkdir([Cache("archives/partial") UTF8String], 0755); + _config->Set("Dir::Cache", [Cache_ UTF8String]); + + symlink("/var/lib/apt/extended_states", [Cache("extended_states") UTF8String]); + _config->Set("Dir::State", [Cache_ UTF8String]); + + mkdir([Cache("lists") UTF8String], 0755); + mkdir([Cache("lists/partial") UTF8String], 0755); + mkdir([Cache("periodic") UTF8String], 0755); + _config->Set("Dir::State::Lists", [Cache("lists") UTF8String]); + + std::string logs("/var/mobile/Library/Logs/Cydia"); + mkdir(logs.c_str(), 0755); + _config->Set("Dir::Log", logs); + + _config->Set("Dir::Bin::dpkg", "/usr/libexec/cydia/cydo"); /* }}} */ /* Color Choices {{{ */ space_ = CGColorSpaceCreateDeviceRGB(); @@ -10296,6 +10277,7 @@ int main(int argc, char *argv[]) { /* }}} */ $SBSSetInterceptsMenuButtonForever = reinterpret_cast(dlsym(RTLD_DEFAULT, "SBSSetInterceptsMenuButtonForever")); + $SBSCopyIconImagePNGDataForDisplayIdentifier = reinterpret_cast(dlsym(RTLD_DEFAULT, "SBSCopyIconImagePNGDataForDisplayIdentifier")); const char *symbol(kCFCoreFoundationVersionNumber >= 800 ? "MGGetBoolAnswer" : "GSSystemHasCapability"); BOOL (*GSSystemHasCapability)(CFStringRef) = reinterpret_cast(dlsym(RTLD_DEFAULT, symbol));