📄 cydia.mm
字号:
static _transient NSString *Role_;
static _transient NSMutableDictionary *Packages_;
static _transient NSMutableDictionary *Sections_;
static _transient NSMutableDictionary *Sources_;
static _transient NSMutableArray *Documents_;
static bool Changed_;
static NSDate *now_;
NSString *GetLastUpdate() {
NSDate *update = [Metadata_ objectForKey:@"LastUpdate"];
if (update == nil)
return @"Never or Unknown";
CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle);
CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update);
CFRelease(formatter);
return [(NSString *) formatted autorelease];
}
/* }}} */
/* Display Helpers {{{ */
inline float Interpolate(float begin, float end, float fraction) {
return (end - begin) * fraction + begin;
}
NSString *SizeString(double size) {
unsigned power = 0;
while (size > 1024) {
size /= 1024;
++power;
}
static const char *powers_[] = {"B", "kB", "MB", "GB"};
return [NSString stringWithFormat:@"%.1f%s", size, powers_[power]];
}
NSString *StripVersion(NSString *version) {
NSRange colon = [version rangeOfString:@":"];
if (colon.location != NSNotFound)
version = [version substringFromIndex:(colon.location + 1)];
return version;
}
static const float TextViewOffset_ = 22;
UITextView *GetTextView(NSString *value, float left, bool html) {
UITextView *text([[[UITextView alloc] initWithFrame:CGRectMake(left, 3, 310 - left, 1000)] autorelease]);
[text setEditable:NO];
[text setTextSize:16];
/*if (html)
[text setHTML:value];
else*/
[text setText:value];
[text setEnabled:NO];
[text setBackgroundColor:[UIColor clearColor]];
CGRect frame = [text frame];
[text setFrame:frame];
CGRect rect = [text visibleTextRect];
frame.size.height = rect.size.height;
[text setFrame:frame];
return text;
}
NSString *Simplify(NSString *title) {
const char *data = [title UTF8String];
size_t size = [title length];
static Pcre square_r("^\\[(.*)\\]$");
if (square_r(data, size))
return Simplify(square_r[1]);
static Pcre paren_r("^\\((.*)\\)$");
if (paren_r(data, size))
return Simplify(paren_r[1]);
static Pcre title_r("^(.*?) \\(.*\\)$");
if (title_r(data, size))
return Simplify(title_r[1]);
return title;
}
/* }}} */
bool isSectionVisible(NSString *section) {
NSDictionary *metadata = [Sections_ objectForKey:section];
NSNumber *hidden = metadata == nil ? nil : [metadata objectForKey:@"Hidden"];
return hidden == nil || ![hidden boolValue];
}
/* Delegate Prototypes {{{ */
@class Package;
@class Source;
@interface NSObject (ProgressDelegate)
@end
@implementation NSObject(ProgressDelegate)
- (void) _setProgressError:(NSArray *)args {
[self performSelector:@selector(setProgressError:forPackage:)
withObject:[args objectAtIndex:0]
withObject:([args count] == 1 ? nil : [args objectAtIndex:1])
];
}
@end
@protocol ProgressDelegate
- (void) setProgressError:(NSString *)error forPackage:(NSString *)id;
- (void) setProgressTitle:(NSString *)title;
- (void) setProgressPercent:(float)percent;
- (void) startProgress;
- (void) addProgressOutput:(NSString *)output;
- (bool) isCancelling:(size_t)received;
@end
@protocol ConfigurationDelegate
- (void) repairWithSelector:(SEL)selector;
- (void) setConfigurationData:(NSString *)data;
@end
@protocol CydiaDelegate
- (void) installPackage:(Package *)package;
- (void) removePackage:(Package *)package;
- (void) slideUp:(UIActionSheet *)alert;
- (void) distUpgrade;
- (void) updateData;
- (void) syncData;
- (void) askForSettings;
- (UIProgressHUD *) addProgressHUD;
- (RVPage *) pageForURL:(NSURL *)url hasTag:(int *)tag;
- (RVPage *) pageForPackage:(NSString *)name;
- (void) openMailToURL:(NSURL *)url;
@end
/* }}} */
/* Status Delegation {{{ */
class Status :
public pkgAcquireStatus
{
private:
_transient NSObject<ProgressDelegate> *delegate_;
public:
Status() :
delegate_(nil)
{
}
void setDelegate(id delegate) {
delegate_ = delegate;
}
virtual bool MediaChange(std::string media, std::string drive) {
return false;
}
virtual void IMSHit(pkgAcquire::ItemDesc &item) {
}
virtual void Fetch(pkgAcquire::ItemDesc &item) {
[delegate_ setProgressTitle:[NSString stringWithUTF8String:("Downloading " + item.ShortDesc).c_str()]];
}
virtual void Done(pkgAcquire::ItemDesc &item) {
}
virtual void Fail(pkgAcquire::ItemDesc &item) {
if (
item.Owner->Status == pkgAcquire::Item::StatIdle ||
item.Owner->Status == pkgAcquire::Item::StatDone
)
return;
[delegate_ performSelectorOnMainThread:@selector(_setProgressError:)
withObject:[NSArray arrayWithObjects:[NSString stringWithUTF8String:item.Owner->ErrorText.c_str()], nil]
waitUntilDone:YES
];
}
virtual bool Pulse(pkgAcquire *Owner) {
bool value = pkgAcquireStatus::Pulse(Owner);
float percent(
double(CurrentBytes + CurrentItems) /
double(TotalBytes + TotalItems)
);
[delegate_ setProgressPercent:percent];
return [delegate_ isCancelling:CurrentBytes] ? false : value;
}
virtual void Start() {
[delegate_ startProgress];
}
virtual void Stop() {
}
};
/* }}} */
/* Progress Delegation {{{ */
class Progress :
public OpProgress
{
private:
_transient id<ProgressDelegate> delegate_;
protected:
virtual void Update() {
[delegate_ setProgressTitle:[NSString stringWithUTF8String:Op.c_str()]];
[delegate_ setProgressPercent:(Percent / 100)];
}
public:
Progress() :
delegate_(nil)
{
}
void setDelegate(id delegate) {
delegate_ = delegate;
}
virtual void Done() {
[delegate_ setProgressPercent:1];
}
};
/* }}} */
/* Database Interface {{{ */
@interface Database : NSObject {
pkgCacheFile cache_;
pkgDepCache::Policy *policy_;
pkgRecords *records_;
pkgProblemResolver *resolver_;
pkgAcquire *fetcher_;
FileFd *lock_;
SPtr<pkgPackageManager> manager_;
pkgSourceList *list_;
NSMutableDictionary *sources_;
NSMutableArray *packages_;
_transient NSObject<ConfigurationDelegate, ProgressDelegate> *delegate_;
Status status_;
Progress progress_;
int cydiafd_;
int statusfd_;
FILE *input_;
}
- (void) _readCydia:(NSNumber *)fd;
- (void) _readStatus:(NSNumber *)fd;
- (void) _readOutput:(NSNumber *)fd;
- (FILE *) input;
- (Package *) packageWithName:(NSString *)name;
- (Database *) init;
- (pkgCacheFile &) cache;
- (pkgDepCache::Policy *) policy;
- (pkgRecords *) records;
- (pkgProblemResolver *) resolver;
- (pkgAcquire &) fetcher;
- (NSArray *) packages;
- (NSArray *) sources;
- (void) reloadData;
- (void) configure;
- (void) prepare;
- (void) perform;
- (void) upgrade;
- (void) update;
- (void) updateWithStatus:(Status &)status;
- (void) setDelegate:(id)delegate;
- (Source *) getSource:(const pkgCache::PkgFileIterator &)file;
@end
/* }}} */
/* Source Class {{{ */
@interface Source : NSObject {
NSString *description_;
NSString *label_;
NSString *origin_;
NSString *uri_;
NSString *distribution_;
NSString *type_;
NSString *version_;
NSString *defaultIcon_;
NSDictionary *record_;
BOOL trusted_;
}
- (Source *) initWithMetaIndex:(metaIndex *)index;
- (NSComparisonResult) compareByNameAndType:(Source *)source;
- (NSDictionary *) record;
- (BOOL) trusted;
- (NSString *) uri;
- (NSString *) distribution;
- (NSString *) type;
- (NSString *) key;
- (NSString *) host;
- (NSString *) name;
- (NSString *) description;
- (NSString *) label;
- (NSString *) origin;
- (NSString *) version;
- (NSString *) defaultIcon;
@end
@implementation Source
- (void) dealloc {
[uri_ release];
[distribution_ release];
[type_ release];
if (description_ != nil)
[description_ release];
if (label_ != nil)
[label_ release];
if (origin_ != nil)
[origin_ release];
if (version_ != nil)
[version_ release];
if (defaultIcon_ != nil)
[defaultIcon_ release];
if (record_ != nil)
[record_ release];
[super dealloc];
}
+ (NSArray *) _attributeKeys {
return [NSArray arrayWithObjects:@"description", @"distribution", @"host", @"key", @"label", @"name", @"origin", @"trusted", @"type", @"uri", @"version", nil];
}
- (NSArray *) attributeKeys {
return [[self class] _attributeKeys];
}
+ (BOOL) isKeyExcludedFromWebScript:(const char *)name {
return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
}
- (Source *) initWithMetaIndex:(metaIndex *)index {
if ((self = [super init]) != nil) {
trusted_ = index->IsTrusted();
uri_ = [[NSString stringWithUTF8String:index->GetURI().c_str()] retain];
distribution_ = [[NSString stringWithUTF8String:index->GetDist().c_str()] retain];
type_ = [[NSString stringWithUTF8String:index->GetType()] retain];
debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index));
if (dindex != NULL) {
std::ifstream release(dindex->MetaIndexFile("Release").c_str());
std::string line;
while (std::getline(release, line)) {
std::string::size_type colon(line.find(':'));
if (colon == std::string::npos)
continue;
std::string name(line.substr(0, colon));
std::string value(line.substr(colon + 1));
while (!value.empty() && value[0] == ' ')
value = value.substr(1);
if (name == "Default-Icon")
defaultIcon_ = [[NSString stringWithUTF8String:value.c_str()] retain];
else if (name == "Description")
description_ = [[NSString stringWithUTF8String:value.c_str()] retain];
else if (name == "Label")
label_ = [[NSString stringWithUTF8String:value.c_str()] retain];
else if (name == "Origin")
origin_ = [[NSString stringWithUTF8String:value.c_str()] retain];
else if (name == "Version")
version_ = [[NSString stringWithUTF8String:value.c_str()] retain];
}
}
record_ = [Sources_ objectForKey:[self key]];
if (record_ != nil)
record_ = [record_ retain];
} return self;
}
- (NSComparisonResult) compareByNameAndType:(Source *)source {
NSDictionary *lhr = [self record];
NSDictionary *rhr = [source record];
if (lhr != rhr)
return lhr == nil ? NSOrderedDescending : NSOrderedAscending;
NSString *lhs = [self name];
NSString *rhs = [source name];
if ([lhs length] != 0 && [rhs length] != 0) {
unichar lhc = [lhs characterAtIndex:0];
unichar rhc = [rhs characterAtIndex:0];
if (isalpha(lhc) && !isalpha(rhc))
return NSOrderedAscending;
else if (!isalpha(lhc) && isalpha(rhc))
return NSOrderedDescending;
}
return [lhs compare:rhs options:CompareOptions_];
}
- (NSDictionary *) record {
return record_;
}
- (BOOL) trusted {
return trusted_;
}
- (NSString *) uri {
return uri_;
}
- (NSString *) distribution {
return distribution_;
}
- (NSString *) type {
return type_;
}
- (NSString *) key {
return [NSString stringWithFormat:@"%@:%@:%@", type_, uri_, distribution_];
}
- (NSString *) host {
return [[[NSURL URLWithString:[self uri]] host] lowercaseString];
}
- (NSString *) name {
return origin_ == nil ? [self host] : origin_;
}
- (NSString *) description {
return description_;
}
- (NSString *) label {
return label_ == nil ? [self host] : label_;
}
- (NSString *) origin {
return origin_;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -