#include <errno.h>
#include <pcre.h>
+#include <Cytore.hpp>
+
#include "UICaboodle/BrowserView.h"
#include "substrate.h"
while (false); \
[_pool release];
+// Hash Functions/Structures {{{
+extern "C" uint32_t hashlittle(const void *key, size_t length, uint32_t initval = 0);
+
+union SplitHash {
+ uint32_t u32;
+ uint16_t u16[2];
+};
+// }}}
+
static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
void NSLogPoint(const char *fix, const CGPoint &point) {
_finline operator id() {
return (NSString *) static_cast<CFStringRef>(*this);
}
+
+ _finline operator const char *() {
+ return reinterpret_cast<const char *>(data_);
+ }
};
/* }}} */
/* C++ NSString Algorithm Adapters {{{ */
private:
CGColorRef color_;
+ static CGColorRef Create_(CGColorSpaceRef space, float red, float green, float blue, float alpha) {
+ CGFloat color[] = {red, green, blue, alpha};
+ return CGColorCreate(space, color);
+ }
+
public:
CYColor() :
color_(NULL)
}
CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) :
- color_(NULL)
+ color_(Create_(space, red, green, blue, alpha))
{
Set(space, red, green, blue, alpha);
}
void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) {
Clear();
- float color[] = {red, green, blue, alpha};
- color_ = CGColorCreate(space, (CGFloat *) color);
+ color_ = Create_(space, red, green, blue, alpha);
}
operator CGColorRef() {
static _transient NSMutableDictionary *Sections_;
static _transient NSMutableDictionary *Sources_;
static bool Changed_;
-static NSDate *now_;
+static time_t now_;
static bool IsWildcat_;
/* }}} */
@end
/* }}} */
+// Cytore Definitions {{{
+struct PackageValue :
+ Cytore::Block
+{
+ Cytore::Offset<void> reserved_;
+ Cytore::Offset<PackageValue> next_;
+
+ uint32_t index_ : 23;
+ uint32_t subscribed_ : 1;
+ uint32_t : 8;
+
+ int32_t first_;
+ int32_t last_;
+
+ uint16_t vhash_;
+ uint16_t nhash_;
+
+ char version_[8];
+ char name_[];
+};
+
+struct MetaValue :
+ Cytore::Block
+{
+ Cytore::Offset<void> reserved_;
+ Cytore::Offset<PackageValue> packages_[1 << 16];
+};
+
+static Cytore::File<MetaValue> MetaFile_;
+// }}}
+// Cytore Helper Functions {{{
+static PackageValue *PackageFind(const char *name, size_t length, Cytore::Offset<PackageValue> *cache = NULL) {
+ SplitHash nhash = { hashlittle(name, length) };
+
+ PackageValue *metadata;
+
+ Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]);
+ offset: if (offset->IsNull()) {
+ *offset = MetaFile_.New<PackageValue>(length + 1);
+ metadata = &MetaFile_.Get(*offset);
+
+ memcpy(metadata->name_, name, length + 1);
+ 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 (cache != NULL)
+ *cache = *offset;
+
+ return metadata;
+}
+
+static void PackageImport(const void *key, const void *value, void *context) {
+ char buffer[1024];
+ if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) {
+ NSLog(@"failed to import package %@", key);
+ return;
+ }
+
+ PackageValue *metadata(PackageFind(buffer, strlen(buffer)));
+ NSDictionary *package((NSDictionary *) value);
+
+ if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"])
+ if ([subscribed boolValue])
+ metadata->subscribed_ = true;
+
+ if (NSDate *date = [package objectForKey:@"FirstSeen"]) {
+ time_t time([date timeIntervalSince1970]);
+ if (metadata->first_ > time || metadata->first_ == 0)
+ metadata->first_ = time;
+ }
+
+ if (NSDate *date = [package objectForKey:@"LastSeen"]) {
+ time_t time([date timeIntervalSince1970]);
+ if (metadata->last_ < time || metadata->last_ == 0) {
+ metadata->last_ = time;
+ goto last;
+ }
+ } else if (metadata->last_ == 0) last: {
+ NSString *version([package objectForKey:@"LastVersion"]);
+ if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) {
+ size_t length(strlen(buffer));
+ uint16_t vhash(hashlittle(buffer, length));
+
+ size_t capped(std::min<size_t>(8, length));
+ char *latest(buffer + length - capped);
+
+ strncpy(metadata->version_, latest, sizeof(metadata->version_));
+ metadata->vhash_ = vhash;
+ }
+ }
+}
+// }}}
+
/* Source Class {{{ */
@interface Source : NSObject {
CYString depiction_;
bool essential_;
bool obsolete_;
- NSString *latest_;
+ CYString latest_;
CYString installed_;
CYString id_;
NSMutableArray *tags_;
NSString *role_;
- NSMutableDictionary *metadata_;
- _transient NSDate *firstSeen_;
- _transient NSDate *lastSeen_;
- bool subscribed_;
+ Cytore::Offset<PackageValue> metadata_;
+
+ bool ignored_;
}
- (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database;
- (NSString *) shortDescription;
- (unichar) index;
-- (NSMutableDictionary *) metadata;
-- (NSDate *) seen;
-- (BOOL) subscribed;
+- (PackageValue *) metadata;
+- (time_t) seen;
+
+- (bool) subscribed;
+- (bool) setSubscribed:(bool)subscribed;
+
- (BOOL) ignored;
- (NSString *) latest;
- (uint32_t) compareBySection:(NSArray *)sections;
-- (uint32_t) compareForChanges;
-
- (void) install;
- (void) remove;
value.bits.ignored = [self ignored] ? 0 : 1;
value.bits.upgradable = 1;
} else {
- value.bits.timestamp = static_cast<uint32_t>([[self seen] timeIntervalSince1970]) >> 2;
+ value.bits.timestamp = static_cast<uint32_t>([self seen]) >> 2;
value.bits.ignored = 0;
value.bits.upgradable = 0;
}
if (source_ != nil)
[source_ release];
- if (latest_ != nil)
- [latest_ release];
-
if (sponsor$_ != nil)
[sponsor$_ release];
if (author$_ != nil)
if (role_ != nil)
[role_ release];
- if (metadata_ != nil)
- [metadata_ release];
-
[super dealloc];
}
database_ = database;
_profile(Package$initWithVersion$Latest)
- const char *latest(StripVersion_(version_.VerStr()));
- latest_ = (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(latest), strlen(latest), kCFStringEncodingASCII, NO);
+ latest_.set(pool_, StripVersion_(version_.VerStr()));
_end
pkgCache::VerIterator current;
}
_end
- bool changed(false);
-
_profile(Package$initWithVersion$Metadata)
- metadata_ = [Packages_ objectForKey:id_];
-
- if (metadata_ == nil) {
- firstSeen_ = now_;
-
- metadata_ = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
- firstSeen_, @"FirstSeen",
- latest_, @"LastVersion",
- nil] mutableCopy];
-
- changed = true;
- } else {
- firstSeen_ = [metadata_ objectForKey:@"FirstSeen"];
- lastSeen_ = [metadata_ objectForKey:@"LastSeen"];
+ PackageValue *metadata(PackageFind(id_.data(), id_.size(), &metadata_));
- if (NSNumber *subscribed = [metadata_ objectForKey:@"IsSubscribed"])
- subscribed_ = [subscribed boolValue];
+ const char *latest(version_.VerStr());
+ size_t length(strlen(latest));
- NSString *version([metadata_ objectForKey:@"LastVersion"]);
+ uint16_t vhash(hashlittle(latest, length));
- if (firstSeen_ == nil) {
- firstSeen_ = lastSeen_ == nil ? now_ : lastSeen_;
- [metadata_ setObject:firstSeen_ forKey:@"FirstSeen"];
- changed = true;
- }
-
- if (version == nil) {
- [metadata_ setObject:latest_ forKey:@"LastVersion"];
- changed = true;
- } else if (![version isEqualToString:latest_]) {
- [metadata_ setObject:latest_ forKey:@"LastVersion"];
- lastSeen_ = now_;
- [metadata_ setObject:lastSeen_ forKey:@"LastSeen"];
- changed = true;
- }
- }
+ size_t capped(std::min<size_t>(8, length));
+ latest = latest + length - capped;
- metadata_ = [metadata_ retain];
+ if (metadata->first_ == 0)
+ metadata->first_ = now_;
- if (changed) {
- [Packages_ setObject:metadata_ forKey:id_];
- Changed_ = true;
- }
+ if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) {
+ metadata->last_ = now_;
+ strncpy(metadata->version_, latest, sizeof(metadata->version_));
+ metadata->vhash_ = vhash;
+ } else if (metadata->last_ == 0)
+ metadata->last_ = metadata->first_;
_end
_profile(Package$initWithVersion$Section)
obsolete_ = [self hasTag:@"cydia::obsolete"];
essential_ = ((iterator_->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES) || [self hasTag:@"cydia::essential"];
_end
+
+ ignored_ = iterator_->SelectedState == pkgCache::State::Hold;
_end } return self;
}
}
- (Address *) maintainer {
- if (file_.end())
+@synchronized (database_) {
+ if ([database_ era] != era_ || file_.end())
return nil;
+
pkgRecords::Parser *parser = &[database_ records]->Lookup(file_);
const std::string &maintainer(parser->Maintainer());
return maintainer.empty() ? nil : [Address addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]];
-}
+} }
- (size_t) size {
- return version_.end() ? 0 : version_->InstalledSize;
-}
+@synchronized (database_) {
+ if ([database_ era] != era_ || version_.end())
+ return 0;
+
+ return version_->InstalledSize;
+} }
- (NSString *) longDescription {
@synchronized (database_) {
_end
}
-- (NSMutableDictionary *) metadata {
- return metadata_;
+- (PackageValue *) metadata {
+ return &MetaFile_.Get(metadata_);
}
-- (NSDate *) seen {
- if (subscribed_ && lastSeen_ != nil)
- return lastSeen_;
- return firstSeen_;
+- (time_t) seen {
+ PackageValue *metadata([self metadata]);
+ return metadata->subscribed_ ? metadata->last_ : metadata->first_;
}
-- (BOOL) subscribed {
- return subscribed_;
+- (bool) subscribed {
+ return [self metadata]->subscribed_;
}
-- (BOOL) ignored {
- NSDictionary *metadata([self metadata]);
- if (NSNumber *ignored = [metadata objectForKey:@"IsIgnored"])
- return [ignored boolValue];
- else
+- (bool) setSubscribed:(bool)subscribed {
+ PackageValue *metadata([self metadata]);
+ if (metadata->subscribed_ == subscribed)
return false;
+ metadata->subscribed_ = subscribed;
+ return true;
+}
+
+- (BOOL) ignored {
+ return ignored_;
}
- (NSString *) latest {
icon = [UIImage imageAtPath:[static_cast<id>(icon_) substringFromIndex:7]];
if (icon == nil) if (section != nil)
icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]];
- if (icon == nil) if (source_ != nil) if (NSString *dicon = [source_ defaultIcon])
+ if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon])
if ([dicon hasPrefix:@"file:///"])
// XXX: correct escaping
icon = [UIImage imageAtPath:[dicon substringFromIndex:7]];
return _not(uint32_t);
}
-- (uint32_t) compareForChanges {
- union {
- uint32_t key;
-
- struct {
- uint32_t timestamp : 30;
- uint32_t ignored : 1;
- uint32_t upgradable : 1;
- } bits;
- } value;
-
- bool upgradable([self upgradableAndEssential:YES]);
- value.bits.upgradable = upgradable ? 1 : 0;
-
- if (upgradable) {
- value.bits.timestamp = 0;
- value.bits.ignored = [self ignored] ? 0 : 1;
- value.bits.upgradable = 1;
- } else {
- value.bits.timestamp = static_cast<uint32_t>([[self seen] timeIntervalSince1970]) >> 2;
- value.bits.ignored = 0;
- value.bits.upgradable = 0;
- }
-
- return _not(uint32_t) - value.key;
-}
-
- (void) clear {
@synchronized (database_) {
pkgProblemResolver *resolver = [database_ resolver];
return era_;
}
+- (void) releasePackages {
+ CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL);
+ CFArrayRemoveAllValues(packages_);
+}
+
- (void) dealloc {
// XXX: actually implement this thing
_assert(false);
- NSRecycleZone(zone_);
- // XXX: malloc_destroy_zone(zone_);
+ [self releasePackages];
apr_pool_destroy(pool_);
+ NSRecycleZone(zone_);
[super dealloc];
}
@synchronized (self) {
++era_;
- CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL);
- CFArrayRemoveAllValues(packages_);
-
+ [self releasePackages];
sources_.clear();
_error->Discard();
delete policy_;
policy_ = NULL;
- if (now_ != nil) {
- [now_ release];
- now_ = nil;
- }
-
cache_.Close();
apr_pool_clear(pool_);
unlink("/tmp/cydia.chk");
- now_ = [[NSDate date] retain];
+ now_ = [[NSDate date] timeIntervalSince1970];
policy_ = new pkgDepCache::Policy();
records_ = new pkgRecords(cache_);
- (NSArray *) getInstalledPackages {
NSArray *packages([[Database sharedInstance] packages]);
- NSMutableArray *installed([NSMutableArray arrayWithCapacity:[packages count]]);
+ NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]);
for (Package *package in packages)
if ([package installed] != nil)
[installed addObject:package];
NSString *source_;
UIImage *badge_;
Package *package_;
- UIColor *color_;
- BOOL faded_;
- float fade_;
UIImage *placard_;
}
- (void) dealloc {
[self clearPackage];
- [color_ release];
[super dealloc];
}
-- (float) fade {
- return faded_ ? [self selectionPercent] : fade_;
-}
-
- (PackageCell *) init {
CGRect frame(CGRectMake(0, 0, 320, 74));
if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) {
[content_ setDelegate:self];
[content_ setOpaque:YES];
- if ([self respondsToSelector:@selector(selectionPercent)])
- faded_ = YES;
} return self;
}
#endif
Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease];
- Section *ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") localize:NO] autorelease];
+ Section *ignored = nil;
Section *section = nil;
- NSDate *last = nil;
+ time_t last = 0;
upgrades_ = 0;
bool unseens = false;
if (!uae) {
unseens = true;
- NSDate *seen;
+ time_t seen([package seen]);
- _profile(ChangesController$reloadData$Remember)
- seen = [package seen];
- _end
-
- if (section == nil || last != seen && (seen == nil || [seen compare:last] != NSOrderedSame)) {
+ if (section == nil || last != seen) {
last = seen;
NSString *name;
- if (seen == nil)
- name = UCLocalize("UNKNOWN");
- else {
- name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) seen);
- [name autorelease];
- }
+ name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]);
+ [name autorelease];
_profile(ChangesController$reloadData$Allocate)
name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name];
}
[section addToCount];
- } else if ([package ignored])
+ } else if ([package ignored]) {
+ if (ignored == nil) {
+ ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease];
+ }
[ignored addToCount];
- else {
+ } else {
++upgrades_;
[upgradable addToCount];
}
if (package_ == nil)
return 0;
- return 1;
+ return 2;
}
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return UCLocalize("SHOW_ALL_CHANGES_EX");
}
-- (void) onSomething:(BOOL)value withKey:(NSString *)key {
+- (void) onSubscribed:(id)control {
+ bool value([control isOn]);
if (package_ == nil)
return;
-
- NSMutableDictionary *metadata([package_ metadata]);
-
- BOOL before;
- if (NSNumber *number = [metadata objectForKey:key])
- before = [number boolValue];
- else
- before = NO;
-
- if (value != before) {
- [metadata setObject:[NSNumber numberWithBool:value] forKey:key];
- Changed_ = true;
+ if ([package_ setSubscribed:value])
[delegate_ updateData];
- }
-}
-
-- (void) onSubscribed:(id)control {
- [self onSomething:(int) [control isOn] withKey:@"IsSubscribed"];
}
- (void) onIgnored:(id)control {
- [self onSomething:(int) [control isOn] withKey:@"IsIgnored"];
+ // TODO: set Held state - possibly call out to dpkg, etc.
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Database *database_;
int tag_;
-
- UIKeyboard *keyboard_;
- int huds_;
+ int hudcount_;
+ NSURL *starturl_;
SectionsController *sections_;
ChangesController *changes_;
}
- (BOOL) hudIsShowing {
- return (huds_ > 0);
+ return (hudcount_ > 0);
}
- (void) applicationSuspend:(__GSEvent *)event {
while ([target modalViewController] != nil) target = [target modalViewController];
[[target view] addSubview:hud];
- huds_++;
+ hudcount_++;
return hud;
}
[hud show:NO];
[hud removeFromSuperview];
[window_ setUserInteractionEnabled:YES];
- huds_--;
+ hudcount_--;
}
- (CYViewController *) pageForPackage:(NSString *)name {
return nil;
}
-- (void) applicationOpenURL:(NSURL *)url {
- [super applicationOpenURL:url];
- int tag;
- if (CYViewController *page = [self pageForURL:url hasTag:&tag]) {
+- (BOOL) openCydiaURL:(NSURL *)url {
+ CYViewController *page = nil;
+ int tag = 0;
+
+ NSLog(@"open url: %@", url);
+
+ if ((page = [self pageForURL:url hasTag:&tag])) {
[self setPage:page];
tag_ = tag;
[tabbar_ setSelectedViewController:(tag_ == -1 ? nil : [[tabbar_ viewControllers] objectAtIndex:tag_])];
}
+
+ return !!page;
+}
+
+- (void) applicationOpenURL:(NSURL *)url {
+ [super applicationOpenURL:url];
+ NSLog(@"first: %@", url);
+ if (!loaded_) starturl_ = [url retain];
+ else [self openCydiaURL:url];
}
- (void) applicationWillResignActive:(UIApplication *)application {
[self reloadData];
PrintTimes();
- // Show the home page
- [tabbar_ setSelectedIndex:0];
- _setHomePage(self);
+ // Show the initial page
+ if (starturl_ == nil || ![self openCydiaURL:starturl_]) {
+ [tabbar_ setSelectedIndex:0];
+ _setHomePage(self);
+ }
+
+ [starturl_ release];
+ starturl_ = nil;
+
[window_ setUserInteractionEnabled:YES];
// XXX: does this actually slow anything down?
if (Settings_ != nil)
Role_ = [Settings_ objectForKey:@"Role"];
- if (Packages_ == nil) {
- Packages_ = [[[NSMutableDictionary alloc] initWithCapacity:128] autorelease];
- [Metadata_ setObject:Packages_ forKey:@"Packages"];
- }
-
if (Sections_ == nil) {
Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease];
[Metadata_ setObject:Sections_ forKey:@"Sections"];
}
/* }}} */
+ _trace();
+ MetaFile_.Open("/var/lib/cydia/metadata.cb0");
+ _trace();
+
+ if (Packages_ != nil) {
+ CFDictionaryApplyFunction((CFDictionaryRef) Packages_, &PackageImport, NULL);
+ _trace();
+ [Metadata_ removeObjectForKey:@"Packages"];
+ Packages_ = nil;
+ Changed_ = true;
+ }
+
Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil];
if (substrate && access("/Library/MobileSubstrate/DynamicLibraries/SimulatedKeyEvents.dylib", F_OK) == 0)