]> git.saurik.com Git - cydia.git/blob - Cydia.mm
aa90bd0f53fc9525105eefd5bb4a7f84832495b0
[cydia.git] / Cydia.mm
1 /* #include Directives {{{ */
2 #include <Foundation/NSURL.h>
3 #include <UIKit/UIKit.h>
4 #import <GraphicsServices/GraphicsServices.h>
5
6 #include <sstream>
7 #include <ext/stdio_filebuf.h>
8
9 #include <apt-pkg/acquire.h>
10 #include <apt-pkg/acquire-item.h>
11 #include <apt-pkg/algorithms.h>
12 #include <apt-pkg/cachefile.h>
13 #include <apt-pkg/configuration.h>
14 #include <apt-pkg/debmetaindex.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/init.h>
17 #include <apt-pkg/pkgrecords.h>
18 #include <apt-pkg/sourcelist.h>
19 #include <apt-pkg/sptr.h>
20
21 #include <sys/sysctl.h>
22
23 #include <errno.h>
24 #include <pcre.h>
25 #include <string.h>
26 /* }}} */
27 /* Extension Keywords {{{ */
28 #define _trace() fprintf(stderr, "_trace()@%s:%u[%s]\n", __FILE__, __LINE__, __FUNCTION__)
29
30 #define _assert(test) do \
31 if (!(test)) { \
32 fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
33 exit(-1); \
34 } \
35 while (false)
36 /* }}} */
37
38 @interface WebView
39 - (void) setApplicationNameForUserAgent:(NSString *)applicationName;
40 @end
41
42 static const int PulseInterval_ = 50000;
43 const char *Machine_ = NULL;
44 const char *SerialNumber_ = NULL;
45
46 @interface NSString (CydiaBypass)
47 - (NSString *) stringByAddingPercentEscapes;
48 @end
49
50 @protocol ProgressDelegate
51 - (void) setError:(NSString *)error;
52 - (void) setTitle:(NSString *)title;
53 - (void) setPercent:(float)percent;
54 - (void) addOutput:(NSString *)output;
55 @end
56
57 NSString *SizeString(double size) {
58 unsigned power = 0;
59 while (size > 1024) {
60 size /= 1024;
61 ++power;
62 }
63
64 static const char *powers_[] = {"B", "kB", "MB", "GB"};
65
66 return [NSString stringWithFormat:@"%.1f%s", size, powers_[power]];
67 }
68
69 /* Status Delegation {{{ */
70 class Status :
71 public pkgAcquireStatus
72 {
73 private:
74 id delegate_;
75
76 public:
77 Status() :
78 delegate_(nil)
79 {
80 }
81
82 void setDelegate(id delegate) {
83 delegate_ = delegate;
84 }
85
86 virtual bool MediaChange(std::string media, std::string drive) {
87 return false;
88 }
89
90 virtual void IMSHit(pkgAcquire::ItemDesc &item) {
91 }
92
93 virtual void Fetch(pkgAcquire::ItemDesc &item) {
94 [delegate_ setTitle:[NSString stringWithCString:("Downloading " + item.ShortDesc).c_str()]];
95 }
96
97 virtual void Done(pkgAcquire::ItemDesc &item) {
98 }
99
100 virtual void Fail(pkgAcquire::ItemDesc &item) {
101 [delegate_ performSelectorOnMainThread:@selector(setStatusFail) withObject:nil waitUntilDone:YES];
102 }
103
104 virtual bool Pulse(pkgAcquire *Owner) {
105 bool value = pkgAcquireStatus::Pulse(Owner);
106
107 float percent(
108 double(CurrentBytes + CurrentItems) /
109 double(TotalBytes + TotalItems)
110 );
111
112 [delegate_ setPercent:percent];
113 return value;
114 }
115
116 virtual void Start() {
117 }
118
119 virtual void Stop() {
120 }
121 };
122 /* }}} */
123 /* Progress Delegation {{{ */
124 class Progress :
125 public OpProgress
126 {
127 private:
128 id delegate_;
129
130 protected:
131 virtual void Update() {
132 [delegate_ setTitle:[NSString stringWithCString:Op.c_str()]];
133 [delegate_ setPercent:(Percent / 100)];
134 }
135
136 public:
137 Progress() :
138 delegate_(nil)
139 {
140 }
141
142 void setDelegate(id delegate) {
143 delegate_ = delegate;
144 }
145
146 virtual void Done() {
147 [delegate_ setPercent:1];
148 }
149 };
150 /* }}} */
151
152 /* External Constants {{{ */
153 extern NSString *kUIButtonBarButtonAction;
154 extern NSString *kUIButtonBarButtonInfo;
155 extern NSString *kUIButtonBarButtonInfoOffset;
156 extern NSString *kUIButtonBarButtonSelectedInfo;
157 extern NSString *kUIButtonBarButtonStyle;
158 extern NSString *kUIButtonBarButtonTag;
159 extern NSString *kUIButtonBarButtonTarget;
160 extern NSString *kUIButtonBarButtonTitle;
161 extern NSString *kUIButtonBarButtonTitleVerticalHeight;
162 extern NSString *kUIButtonBarButtonTitleWidth;
163 extern NSString *kUIButtonBarButtonType;
164 /* }}} */
165 /* Mime Addresses {{{ */
166 @interface Address : NSObject {
167 NSString *name_;
168 NSString *email_;
169 }
170
171 - (void) dealloc;
172
173 - (NSString *) name;
174 - (NSString *) email;
175
176 + (Address *) addressWithString:(NSString *)string;
177 - (Address *) initWithString:(NSString *)string;
178 @end
179
180 @implementation Address
181
182 - (void) dealloc {
183 [name_ release];
184 if (email_ != nil)
185 [email_ release];
186 [super dealloc];
187 }
188
189 - (NSString *) name {
190 return name_;
191 }
192
193 - (NSString *) email {
194 return email_;
195 }
196
197 + (Address *) addressWithString:(NSString *)string {
198 return [[[Address alloc] initWithString:string] autorelease];
199 }
200
201 - (Address *) initWithString:(NSString *)string {
202 if ((self = [super init]) != nil) {
203 const char *error;
204 int offset;
205 pcre *code = pcre_compile("^\"?(.*)\"? <([^>]*)>$", 0, &error, &offset, NULL);
206
207 if (code == NULL) {
208 fprintf(stderr, "%d:%s\n", offset, error);
209 _assert(false);
210 }
211
212 pcre_extra *study = NULL;
213 int capture;
214 pcre_fullinfo(code, study, PCRE_INFO_CAPTURECOUNT, &capture);
215 int matches[(capture + 1) * 3];
216
217 size_t size = [string length];
218 const char *data = [string UTF8String];
219
220 if (pcre_exec(code, study, data, size, 0, 0, matches, sizeof(matches) / sizeof(matches[0])) >= 0) {
221 name_ = [[NSString stringWithCString:(data + matches[2]) length:(matches[3] - matches[2])] retain];
222 email_ = [[NSString stringWithCString:(data + matches[4]) length:(matches[5] - matches[4])] retain];
223 } else {
224 name_ = [[NSString stringWithCString:data length:size] retain];
225 email_ = nil;
226 }
227 } return self;
228 }
229
230 @end
231 /* }}} */
232
233 /* Right Alignment {{{ */
234 @interface UIRightTextLabel : UITextLabel {
235 float _savedRightEdgeX;
236 BOOL _sizedtofit_flag;
237 }
238
239 - (void) setFrame:(CGRect)frame;
240 - (void) setText:(NSString *)text;
241 - (void) realignText;
242 @end
243
244 @implementation UIRightTextLabel
245
246 - (void) setFrame:(CGRect)frame {
247 [super setFrame:frame];
248 if (_sizedtofit_flag == NO) {
249 _savedRightEdgeX = frame.origin.x;
250 [self realignText];
251 }
252 }
253
254 - (void) setText:(NSString *)text {
255 [super setText:text];
256 [self realignText];
257 }
258
259 - (void) realignText {
260 CGRect oldFrame = [self frame];
261
262 _sizedtofit_flag = YES;
263 [self sizeToFit]; // shrink down size so I can right align it
264
265 CGRect newFrame = [self frame];
266
267 oldFrame.origin.x = _savedRightEdgeX - newFrame.size.width;
268 oldFrame.size.width = newFrame.size.width;
269 [super setFrame:oldFrame];
270 _sizedtofit_flag = NO;
271 }
272
273 @end
274 /* }}} */
275 /* Linear Algebra {{{ */
276 inline float interpolate(float begin, float end, float fraction) {
277 return (end - begin) * fraction + begin;
278 }
279 /* }}} */
280
281 /* Reset View {{{ */
282 @interface ResetView : UIView {
283 UINavigationBar *navbar_;
284 bool resetting_;
285 }
286
287 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item;
288
289 - (void) dealloc;
290 - (void) resetView;
291 - (void) _resetView;
292 - (NSString *) leftTitle;
293 - (NSString *) rightTitle;
294 @end
295
296 @implementation ResetView
297
298 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
299 if ([[navbar_ navigationItems] count] == 1)
300 [self _resetView];
301 }
302
303 - (void) dealloc {
304 [navbar_ release];
305 [super dealloc];
306 }
307
308 - (void) resetView {
309 resetting_ = true;
310 if ([[navbar_ navigationItems] count] == 1)
311 [self _resetView];
312 else while ([[navbar_ navigationItems] count] != 1)
313 [navbar_ popNavigationItem];
314 resetting_ = false;
315 }
316
317 - (void) _resetView {
318 [navbar_ showButtonsWithLeftTitle:[self leftTitle] rightTitle:[self rightTitle]];
319 }
320
321 - (NSString *) leftTitle {
322 return nil;
323 }
324
325 - (NSString *) rightTitle {
326 return nil;
327 }
328
329 @end
330 /* }}} */
331
332 @class Package;
333
334 @interface Database : NSObject {
335 pkgCacheFile cache_;
336 pkgRecords *records_;
337 pkgProblemResolver *resolver_;
338 pkgAcquire *fetcher_;
339 FileFd *lock_;
340 SPtr<pkgPackageManager> manager_;
341
342 id delegate_;
343 Status status_;
344 Progress progress_;
345 int statusfd_;
346 }
347
348 - (void) _readStatus:(NSNumber *)fd;
349 - (void) _readOutput:(NSNumber *)fd;
350
351 - (Package *) packageWithName:(NSString *)name;
352
353 - (Database *) init;
354 - (pkgCacheFile &) cache;
355 - (pkgRecords *) records;
356 - (pkgProblemResolver *) resolver;
357 - (pkgAcquire &) fetcher;
358 - (void) reloadData;
359
360 - (void) prepare;
361 - (void) perform;
362 - (void) update;
363 - (void) upgrade;
364
365 - (void) setDelegate:(id)delegate;
366 @end
367
368 /* Package Class {{{ */
369 @interface Package : NSObject {
370 pkgCache::PkgIterator iterator_;
371 Database *database_;
372 pkgRecords::Parser *parser_;
373 pkgCache::VerIterator version_;
374 pkgCache::VerFileIterator file_;
375 }
376
377 - (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database version:(pkgCache::VerIterator)version file:(pkgCache::VerFileIterator)file;
378 + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database;
379
380 - (NSString *) name;
381 - (NSString *) section;
382 - (NSString *) latest;
383 - (NSString *) installed;
384 - (Address *) maintainer;
385 - (size_t) size;
386 - (NSString *) tagline;
387 - (NSString *) description;
388 - (NSComparisonResult) compareBySectionAndName:(Package *)package;
389
390 - (void) install;
391 - (void) remove;
392 @end
393
394 @implementation Package
395
396 - (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database version:(pkgCache::VerIterator)version file:(pkgCache::VerFileIterator)file {
397 if ((self = [super init]) != nil) {
398 iterator_ = iterator;
399 database_ = database;
400
401 version_ = version;
402 file_ = file;
403 parser_ = &[database_ records]->Lookup(file);
404 } return self;
405 }
406
407 + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database {
408 for (pkgCache::VerIterator version = iterator.VersionList(); !version.end(); ++version)
409 for (pkgCache::VerFileIterator file = version.FileList(); !file.end(); ++file)
410 return [[[Package alloc]
411 initWithIterator:iterator
412 database:database
413 version:version
414 file:file]
415 autorelease];
416 return nil;
417 }
418
419 - (NSString *) name {
420 return [[NSString stringWithCString:iterator_.Name()] lowercaseString];
421 }
422
423 - (NSString *) section {
424 return [NSString stringWithCString:iterator_.Section()];
425 }
426
427 - (NSString *) latest {
428 return [NSString stringWithCString:version_.VerStr()];
429 }
430
431 - (NSString *) installed {
432 return iterator_.CurrentVer().end() ? nil : [NSString stringWithCString:iterator_.CurrentVer().VerStr()];
433 }
434
435 - (Address *) maintainer {
436 return [Address addressWithString:[NSString stringWithCString:parser_->Maintainer().c_str()]];
437 }
438
439 - (size_t) size {
440 return version_->InstalledSize;
441 }
442
443 - (NSString *) tagline {
444 return [NSString stringWithCString:parser_->ShortDesc().c_str()];
445 }
446
447 - (NSString *) description {
448 return [NSString stringWithCString:parser_->LongDesc().c_str()];
449 }
450
451 - (NSComparisonResult) compareBySectionAndName:(Package *)package {
452 NSComparisonResult result = [[self section] compare:[package section]];
453 if (result != NSOrderedSame)
454 return result;
455 return [[self name] compare:[package name]];
456 }
457
458 - (void) install {
459 pkgProblemResolver *resolver = [database_ resolver];
460 resolver->Clear(iterator_);
461 resolver->Protect(iterator_);
462 [database_ cache]->MarkInstall(iterator_, false);
463 }
464
465 - (void) remove {
466 pkgProblemResolver *resolver = [database_ resolver];
467 resolver->Clear(iterator_);
468 resolver->Protect(iterator_);
469 resolver->Remove(iterator_);
470 [database_ cache]->MarkDelete(iterator_, true);
471 }
472
473 @end
474 /* }}} */
475 /* Section Class {{{ */
476 @interface Section : NSObject {
477 NSString *name_;
478 size_t row_;
479 NSMutableArray *packages_;
480 }
481
482 - (void) dealloc;
483
484 - (Section *) initWithName:(NSString *)name row:(size_t)row;
485 - (NSString *) name;
486 - (size_t) row;
487 - (void) addPackage:(Package *)package;
488 @end
489
490 @implementation Section
491
492 - (void) dealloc {
493 [name_ release];
494 [packages_ release];
495 [super dealloc];
496 }
497
498 - (Section *) initWithName:(NSString *)name row:(size_t)row {
499 if ((self = [super init]) != nil) {
500 name_ = [name retain];
501 row_ = row;
502 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
503 } return self;
504 }
505
506 - (NSString *) name {
507 return name_;
508 }
509
510 - (size_t) row {
511 return row_;
512 }
513
514 - (void) addPackage:(Package *)package {
515 [packages_ addObject:package];
516 }
517
518 @end
519 /* }}} */
520
521 /* Confirmation View {{{ */
522 void AddTextView(NSMutableDictionary *fields, NSMutableArray *packages, NSString *key) {
523 if ([packages count] == 0)
524 return;
525
526 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
527 float clear[] = {0, 0, 0, 0};
528 float blue[] = {0, 0, 0.4, 1};
529
530 UITextView *text([[[UITextView alloc] initWithFrame: CGRectMake(110, 3, 200, 60)] autorelease]);
531 [text setEditable:NO];
532 [text setTextSize:16];
533 [text setBackgroundColor:CGColorCreate(space, clear)];
534 [text setTextColor:CGColorCreate(space, blue)];
535 [text setText:([packages count] == 0 ? @"n/a" : [packages componentsJoinedByString:@", "])];
536 [text setEnabled:NO];
537
538 CGRect frame([text frame]);
539 CGSize size([text contentSize]);
540 frame.size.height = size.height;
541 [text setFrame:frame];
542
543 [fields setObject:text forKey:key];
544 }
545
546 @protocol ConfirmationViewDelegate
547 - (void) cancel;
548 - (void) confirm;
549 @end
550
551 @interface ConfirmationView : UIView {
552 Database *database_;
553 id delegate_;
554 UITransitionView *transition_;
555 UIView *overlay_;
556 UINavigationBar *navbar_;
557 UIPreferencesTable *table_;
558 NSMutableDictionary *fields_;
559 UIAlertSheet *essential_;
560 }
561
562 - (void) dealloc;
563 - (void) cancel;
564
565 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to;
566 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button;
567 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button;
568
569 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table;
570 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group;
571 - (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed;
572 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group;
573 - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group;
574
575 - (id) initWithView:(UIView *)view database:(Database *)database delegate:(id)delegate;
576
577 @end
578
579 @implementation ConfirmationView
580
581 - (void) dealloc {
582 [transition_ release];
583 [overlay_ release];
584 [navbar_ release];
585 [table_ release];
586 [fields_ release];
587 if (essential_ != nil)
588 [essential_ release];
589 [super dealloc];
590 }
591
592 - (void) cancel {
593 [transition_ transition:7 toView:nil];
594 [delegate_ cancel];
595 }
596
597 - (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to {
598 if (from != nil && to == nil)
599 [self removeFromSuperview];
600 }
601
602 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
603 switch (button) {
604 case 0:
605 if (essential_ != nil)
606 [essential_ popupAlertAnimated:YES];
607 else
608 [delegate_ confirm];
609 break;
610
611 case 1:
612 [self cancel];
613 break;
614 }
615 }
616
617 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
618 [essential_ dismiss];
619 [self cancel];
620 }
621
622 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
623 return 2;
624 }
625
626 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group {
627 switch (group) {
628 case 0: return @"Statistics";
629 case 1: return @"Modifications";
630
631 default: _assert(false);
632 }
633 }
634
635 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
636 switch (group) {
637 case 0: return 3;
638 case 1: return [fields_ count];
639
640 default: _assert(false);
641 }
642 }
643
644 - (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed {
645 if (group != 1 || row == -1)
646 return proposed;
647 else {
648 _assert(size_t(row) < [fields_ count]);
649 return [[[fields_ allValues] objectAtIndex:row] contentSize].height;
650 }
651 }
652
653 - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group {
654 UIPreferencesTableCell *cell = [[[UIPreferencesTableCell alloc] init] autorelease];
655 [cell setShowSelection:NO];
656
657 switch (group) {
658 case 0: switch (row) {
659 case 0: {
660 [cell setTitle:@"Downloading"];
661 [cell setValue:SizeString([database_ fetcher].FetchNeeded())];
662 } break;
663
664 case 1: {
665 [cell setTitle:@"Resuming At"];
666 [cell setValue:SizeString([database_ fetcher].PartialPresent())];
667 } break;
668
669 case 2: {
670 double size([database_ cache]->UsrSize());
671
672 if (size < 0) {
673 [cell setTitle:@"Disk Freeing"];
674 [cell setValue:SizeString(-size)];
675 } else {
676 [cell setTitle:@"Disk Using"];
677 [cell setValue:SizeString(size)];
678 }
679 } break;
680
681 default: _assert(false);
682 } break;
683
684 case 1:
685 _assert(size_t(row) < [fields_ count]);
686 [cell setTitle:[[fields_ allKeys] objectAtIndex:row]];
687 [cell addSubview:[[fields_ allValues] objectAtIndex:row]];
688 break;
689
690 default: _assert(false);
691 }
692
693 return cell;
694 }
695
696 - (id) initWithView:(UIView *)view database:(Database *)database delegate:(id)delegate {
697 if ((self = [super initWithFrame:[view bounds]]) != nil) {
698 database_ = database;
699 delegate_ = delegate;
700
701 transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]];
702 [self addSubview:transition_];
703
704 overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]];
705
706 CGSize navsize = [UINavigationBar defaultSize];
707 CGRect navrect = {{0, 0}, navsize};
708 CGRect bounds = [overlay_ bounds];
709
710 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
711 [navbar_ setBarStyle:1];
712 [navbar_ setDelegate:self];
713
714 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Confirm"] autorelease];
715 [navbar_ pushNavigationItem:navitem];
716 [navbar_ showButtonsWithLeftTitle:@"Cancel" rightTitle:@"Confirm"];
717
718 fields_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain];
719
720 NSMutableArray *installing = [NSMutableArray arrayWithCapacity:16];
721 NSMutableArray *upgrading = [NSMutableArray arrayWithCapacity:16];
722 NSMutableArray *removing = [NSMutableArray arrayWithCapacity:16];
723
724 bool essential(false);
725
726 pkgCacheFile &cache([database_ cache]);
727 for (pkgCache::PkgIterator iterator = cache->PkgBegin(); !iterator.end(); ++iterator) {
728 NSString *name([NSString stringWithCString:iterator.Name()]);
729 if (cache[iterator].NewInstall())
730 [installing addObject:name];
731 else if (cache[iterator].Upgrade())
732 [upgrading addObject:name];
733 else if (cache[iterator].Delete()) {
734 [removing addObject:name];
735 if ((iterator->Flags & pkgCache::Flag::Essential) != 0)
736 essential = true;
737 }
738 }
739
740 if (!essential)
741 essential_ = nil;
742 else {
743 essential_ = [[UIAlertSheet alloc]
744 initWithTitle:@"Unable to Comply"
745 buttons:[NSArray arrayWithObjects:@"Okay", nil]
746 defaultButtonIndex:0
747 delegate:self
748 context:self
749 ];
750
751 [essential_ setBodyText:@"One or more of the packages you are about to remove are marked 'Essential' and cannot be removed by Cydia. Please use apt-get."];
752 }
753
754 AddTextView(fields_, installing, @"Installing");
755 AddTextView(fields_, upgrading, @"Upgrading");
756 AddTextView(fields_, removing, @"Removing");
757
758 table_ = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(
759 0, navsize.height, bounds.size.width, bounds.size.height - navsize.height
760 )];
761
762 [table_ setReusesTableCells:YES];
763 [table_ setDataSource:self];
764 [table_ reloadData];
765
766 [overlay_ addSubview:navbar_];
767 [overlay_ addSubview:table_];
768
769 [view addSubview:self];
770
771 [transition_ setDelegate:self];
772
773 UIView *blank = [[[UIView alloc] initWithFrame:[transition_ bounds]] autorelease];
774 [transition_ transition:0 toView:blank];
775 [transition_ transition:3 toView:overlay_];
776 } return self;
777 }
778
779 @end
780 /* }}} */
781 /* Package View {{{ */
782 @interface PackageView : UIView {
783 UIPreferencesTable *table_;
784 Package *package_;
785 Database *database_;
786 NSMutableArray *cells_;
787 id delegate_;
788 }
789
790 - (void) dealloc;
791
792 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table;
793 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group;
794 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group;
795 - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group;
796
797 - (BOOL) canSelectRow:(int)row;
798 - (void) tableRowSelected:(NSNotification *)notification;
799
800 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database;
801 - (void) setPackage:(Package *)package;
802 - (void) setDelegate:(id)delegate;
803 @end
804
805 @implementation PackageView
806
807 - (void) dealloc {
808 if (package_ != nil)
809 [package_ release];
810 [table_ release];
811 [database_ release];
812 [cells_ release];
813 [super dealloc];
814 }
815
816 - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
817 return 2;
818 }
819
820 - (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group {
821 switch (group) {
822 case 0: return @"Specifics";
823 case 1: return @"Description";
824
825 default: _assert(false);
826 }
827 }
828
829 - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
830 switch (group) {
831 case 0: return 6;
832 case 1: return 1;
833
834 default: _assert(false);
835 }
836 }
837
838 - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group {
839 UIPreferencesTableCell *cell;
840
841 switch (group) {
842 case 0: switch (row) {
843 case 0:
844 cell = [cells_ objectAtIndex:0];
845 [cell setTitle:@"Name"];
846 [cell setValue:[package_ name]];
847 break;
848
849 case 1: {
850 cell = [cells_ objectAtIndex:1];
851 [cell setTitle:@"Installed"];
852 NSString *installed([package_ installed]);
853 [cell setValue:(installed == nil ? @"n/a" : installed)];
854 } break;
855
856 case 2:
857 cell = [cells_ objectAtIndex:2];
858 [cell setTitle:@"Latest"];
859 [cell setValue:[package_ latest]];
860 break;
861
862 case 3:
863 cell = [cells_ objectAtIndex:3];
864 [cell setTitle:@"Section"];
865 [cell setValue:[package_ section]];
866 break;
867
868 case 4:
869 cell = [cells_ objectAtIndex:4];
870 [cell setTitle:@"Size"];
871 [cell setValue:SizeString([package_ size])];
872 break;
873
874 case 5:
875 cell = [cells_ objectAtIndex:5];
876 [cell setTitle:@"Maintainer"];
877 [cell setValue:[[package_ maintainer] name]];
878 [cell setShowDisclosure:YES];
879 [cell setShowSelection:YES];
880 break;
881
882 default: _assert(false);
883 } break;
884
885 case 1: switch (row) {
886 case 0:
887 cell = [cells_ objectAtIndex:6];
888 [cell setTitle:nil];
889 [cell setValue:[package_ tagline]];
890 break;
891
892 case 1:
893 cell = [cells_ objectAtIndex:7];
894 [cell setTitle:@"Description"];
895 [cell setValue:[package_ description]];
896 break;
897 } break;
898
899 default: _assert(false);
900 }
901
902 return cell;
903 }
904
905 - (BOOL) canSelectRow:(int)row {
906 return YES;
907 }
908
909 - (void) tableRowSelected:(NSNotification *)notification {
910 switch ([table_ selectedRow]) {
911 case 5:
912 [delegate_ openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?subject=%@",
913 [[package_ maintainer] email],
914 [[NSString stringWithFormat:@"regarding apt package \"%@\"", [package_ name]] stringByAddingPercentEscapes]
915 ]]];
916 break;
917 }
918 }
919
920 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database {
921 if ((self = [super initWithFrame:frame]) != nil) {
922 database_ = [database retain];
923
924 table_ = [[UIPreferencesTable alloc] initWithFrame:[self bounds]];
925 [self addSubview:table_];
926
927 [table_ setDataSource:self];
928 [table_ setDelegate:self];
929
930 cells_ = [[NSMutableArray arrayWithCapacity:16] retain];
931
932 for (unsigned i = 0; i != 8; ++i) {
933 UIPreferencesTableCell *cell = [[[UIPreferencesTableCell alloc] init] autorelease];
934 [cell setShowSelection:NO];
935 [cells_ addObject:cell];
936 }
937 } return self;
938 }
939
940 - (void) setPackage:(Package *)package {
941 package_ = [package retain];
942 [table_ reloadData];
943 }
944
945 - (void) setDelegate:(id)delegate {
946 delegate_ = delegate;
947 }
948
949 @end
950 /* }}} */
951 /* Package Cell {{{ */
952 @protocol PackageCellDelegate
953 - (NSString *) versionWithPackage:(Package *)package;
954 @end
955
956 @interface PackageCell : UITableCell {
957 UITextLabel *name_;
958 UIRightTextLabel *version_;
959 UITextLabel *description_;
960 }
961
962 - (void) dealloc;
963
964 - (PackageCell *) initWithPackage:(Package *)package delegate:(id)delegate;
965
966 - (void) _setSelected:(float)fraction;
967 - (void) setSelected:(BOOL)selected;
968 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade;
969 - (void) _setSelectionFadeFraction:(float)fraction;
970
971 @end
972
973 @implementation PackageCell
974
975 - (void) dealloc {
976 [name_ release];
977 [version_ release];
978 [description_ release];
979 [super dealloc];
980 }
981
982 - (PackageCell *) initWithPackage:(Package *)package delegate:(id)delegate {
983 if ((self = [super init]) != nil) {
984 GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22);
985 GSFontRef large = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 16);
986 GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 14);
987
988 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
989 float clear[] = {0, 0, 0, 0};
990
991 name_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 7, 250, 25)];
992 [name_ setText:[package name]];
993 [name_ setBackgroundColor:CGColorCreate(space, clear)];
994 [name_ setFont:bold];
995
996 version_ = [[UIRightTextLabel alloc] initWithFrame:CGRectMake(290, 7, 70, 25)];
997 [version_ setText:[delegate versionWithPackage:package]];
998 [version_ setBackgroundColor:CGColorCreate(space, clear)];
999 [version_ setFont:large];
1000
1001 description_ = [[UITextLabel alloc] initWithFrame:CGRectMake(13, 35, 315, 20)];
1002 [description_ setText:[package tagline]];
1003 [description_ setBackgroundColor:CGColorCreate(space, clear)];
1004 [description_ setFont:small];
1005
1006 [self addSubview:name_];
1007 [self addSubview:version_];
1008 [self addSubview:description_];
1009
1010 CFRelease(small);
1011 CFRelease(large);
1012 CFRelease(bold);
1013 } return self;
1014 }
1015
1016 - (void) _setSelected:(float)fraction {
1017 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
1018
1019 float black[] = {
1020 interpolate(0.0, 1.0, fraction),
1021 interpolate(0.0, 1.0, fraction),
1022 interpolate(0.0, 1.0, fraction),
1023 1.0};
1024
1025 float blue[] = {
1026 interpolate(0.2, 1.0, fraction),
1027 interpolate(0.2, 1.0, fraction),
1028 interpolate(1.0, 1.0, fraction),
1029 1.0};
1030
1031 float gray[] = {
1032 interpolate(0.4, 1.0, fraction),
1033 interpolate(0.4, 1.0, fraction),
1034 interpolate(0.4, 1.0, fraction),
1035 1.0};
1036
1037 [name_ setColor:CGColorCreate(space, black)];
1038 [version_ setColor:CGColorCreate(space, blue)];
1039 [description_ setColor:CGColorCreate(space, gray)];
1040 }
1041
1042 - (void) setSelected:(BOOL)selected {
1043 [self _setSelected:(selected ? 1.0 : 0.0)];
1044 [super setSelected:selected];
1045 }
1046
1047 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade {
1048 if (!fade)
1049 [self _setSelected:(selected ? 1.0 : 0.0)];
1050 [super setSelected:selected withFade:fade];
1051 }
1052
1053 - (void) _setSelectionFadeFraction:(float)fraction {
1054 [self _setSelected:fraction];
1055 [super _setSelectionFadeFraction:fraction];
1056 }
1057
1058 @end
1059 /* }}} */
1060 /* Sources View {{{ */
1061 @interface SourcesView : ResetView {
1062 UISectionList *list_;
1063 Database *database_;
1064 id delegate_;
1065 NSMutableArray *sources_;
1066 }
1067
1068 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button;
1069 - (void) dealloc;
1070 - (id) initWithFrame:(CGRect)frame database:(Database *)database;
1071 - (void) setDelegate:(id)delegate;
1072 - (void) reloadData;
1073 - (NSString *) leftTitle;
1074 - (NSString *) rightTitle;
1075 @end
1076
1077 @implementation SourcesView
1078
1079 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
1080 switch (button) {
1081 case 0:
1082 break;
1083
1084 case 1:
1085 [delegate_ update];
1086 break;
1087 }
1088 }
1089
1090 - (void) dealloc {
1091 if (sources_ != nil)
1092 [sources_ release];
1093 [list_ release];
1094 [super dealloc];
1095 }
1096
1097 - (id) initWithFrame:(CGRect)frame database:(Database *)database {
1098 if ((self = [super initWithFrame:frame]) != nil) {
1099 database_ = database;
1100 sources_ = nil;
1101
1102 CGSize navsize = [UINavigationBar defaultSize];
1103 CGRect navrect = {{0, 0}, navsize};
1104 CGRect bounds = [self bounds];
1105
1106 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
1107 [self addSubview:navbar_];
1108
1109 [navbar_ setBarStyle:1];
1110 [navbar_ setDelegate:self];
1111
1112 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Sources"] autorelease];
1113 [navbar_ pushNavigationItem:navitem];
1114
1115 list_ = [[UISectionList alloc] initWithFrame:CGRectMake(
1116 0, navsize.height, bounds.size.width, bounds.size.height - navsize.height
1117 )];
1118
1119 [list_ setDataSource:self];
1120 //[list_ setShouldHideHeaderInShortLists:NO];
1121 } return self;
1122 }
1123
1124 - (void) setDelegate:(id)delegate {
1125 delegate_ = delegate;
1126 }
1127
1128 - (void) reloadData {
1129 pkgSourceList list;
1130 _assert(list.ReadMainList());
1131
1132 sources_ = [[NSMutableArray arrayWithCapacity:16] retain];
1133
1134 for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) {
1135 metaIndex *index(*source);
1136 fprintf(stderr, "\"%s\" \"%s\" \"%s\" \"%s\"\n", index->GetURI().c_str(), index->GetDist().c_str(), index->GetType(), index->IsTrusted() ? "true" : "false");
1137
1138 debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index));
1139 if (dindex == NULL)
1140 continue;
1141
1142 fprintf(stderr, " \"%s\"\n", dindex->MetaIndexFile("Release").c_str());
1143
1144 std::ifstream release(dindex->MetaIndexFile("Release").c_str());
1145 std::string line;
1146 while (std::getline(release, line)) {
1147 std::string::size_type colon(line.find(':'));
1148 if (colon == std::string::npos)
1149 continue;
1150 std::string name(line.substr(0, colon));
1151 std::string value(line.substr(colon + 1));
1152 while (!value.empty() && value[0] == ' ')
1153 value = value.substr(1);
1154 std::cerr << "[" << name << "|" << value << "]" << std::endl;
1155 }
1156 }
1157
1158 [self resetView];
1159 }
1160
1161 - (NSString *) leftTitle {
1162 return @"Refresh All";
1163 }
1164
1165 - (NSString *) rightTitle {
1166 return @"Edit";
1167 }
1168
1169 @end
1170 /* }}} */
1171
1172 @implementation Database
1173
1174 - (void) _readStatus:(NSNumber *)fd {
1175 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1176
1177 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
1178 std::istream is(&ib);
1179 std::string line;
1180
1181 const char *error;
1182 int offset;
1183 pcre *code = pcre_compile("^([^:]*):([^:]*):([^:]*):(.*)$", 0, &error, &offset, NULL);
1184
1185 pcre_extra *study = NULL;
1186 int capture;
1187 pcre_fullinfo(code, study, PCRE_INFO_CAPTURECOUNT, &capture);
1188 int matches[(capture + 1) * 3];
1189
1190 while (std::getline(is, line)) {
1191 const char *data(line.c_str());
1192
1193 _assert(pcre_exec(code, study, data, line.size(), 0, 0, matches, sizeof(matches) / sizeof(matches[0])) >= 0);
1194
1195 std::istringstream buffer(line.substr(matches[6], matches[7] - matches[6]));
1196 float percent;
1197 buffer >> percent;
1198 [delegate_ setPercent:(percent / 100)];
1199
1200 NSString *string = [NSString stringWithCString:(data + matches[8]) length:(matches[9] - matches[8])];
1201 std::string type(line.substr(matches[2], matches[3] - matches[2]));
1202
1203 if (type == "pmerror")
1204 [delegate_ setError:string];
1205 else if (type == "pmstatus")
1206 [delegate_ setTitle:string];
1207 else if (type == "pmconffile")
1208 ;
1209 else _assert(false);
1210 }
1211
1212 [pool release];
1213 }
1214
1215 - (void) _readOutput:(NSNumber *)fd {
1216 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1217
1218 __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in);
1219 std::istream is(&ib);
1220 std::string line;
1221
1222 while (std::getline(is, line))
1223 [delegate_ addOutput:[NSString stringWithCString:line.c_str()]];
1224
1225 [pool release];
1226 }
1227
1228 - (Package *) packageWithName:(NSString *)name {
1229 pkgCache::PkgIterator iterator(cache_->FindPkg([name cString]));
1230 return iterator.end() ? nil : [Package packageWithIterator:iterator database:self];
1231 }
1232
1233 - (Database *) init {
1234 if ((self = [super init]) != nil) {
1235 records_ = NULL;
1236 resolver_ = NULL;
1237 fetcher_ = NULL;
1238 lock_ = NULL;
1239
1240 int fds[2];
1241
1242 _assert(pipe(fds) != -1);
1243 statusfd_ = fds[1];
1244
1245 [NSThread
1246 detachNewThreadSelector:@selector(_readStatus:)
1247 toTarget:self
1248 withObject:[[NSNumber numberWithInt:fds[0]] retain]
1249 ];
1250
1251 _assert(pipe(fds) != -1);
1252 _assert(dup2(fds[1], 1) != -1);
1253 _assert(close(fds[1]) != -1);
1254
1255 [NSThread
1256 detachNewThreadSelector:@selector(_readOutput:)
1257 toTarget:self
1258 withObject:[[NSNumber numberWithInt:fds[0]] retain]
1259 ];
1260 } return self;
1261 }
1262
1263 - (pkgCacheFile &) cache {
1264 return cache_;
1265 }
1266
1267 - (pkgRecords *) records {
1268 return records_;
1269 }
1270
1271 - (pkgProblemResolver *) resolver {
1272 return resolver_;
1273 }
1274
1275 - (pkgAcquire &) fetcher {
1276 return *fetcher_;
1277 }
1278
1279 - (void) reloadData {
1280 _error->Discard();
1281 manager_ = NULL;
1282 delete lock_;
1283 delete fetcher_;
1284 delete resolver_;
1285 delete records_;
1286 cache_.Close();
1287 cache_.Open(progress_, true);
1288 records_ = new pkgRecords(cache_);
1289 resolver_ = new pkgProblemResolver(cache_);
1290 fetcher_ = new pkgAcquire(&status_);
1291 lock_ = NULL;
1292 }
1293
1294 - (void) prepare {
1295 pkgRecords records(cache_);
1296
1297 lock_ = new FileFd();
1298 lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock"));
1299 _assert(!_error->PendingError());
1300
1301 pkgSourceList list;
1302 _assert(list.ReadMainList());
1303
1304 manager_ = (_system->CreatePM(cache_));
1305 _assert(manager_->GetArchives(fetcher_, &list, &records));
1306 _assert(!_error->PendingError());
1307 }
1308
1309 - (void) perform {
1310 if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue)
1311 return;
1312
1313 _system->UnLock();
1314 pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_);
1315
1316 if (result == pkgPackageManager::Failed)
1317 return;
1318 if (_error->PendingError())
1319 return;
1320 if (result != pkgPackageManager::Completed)
1321 return;
1322 }
1323
1324 - (void) update {
1325 pkgSourceList list;
1326 _assert(list.ReadMainList());
1327
1328 FileFd lock;
1329 lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock"));
1330 _assert(!_error->PendingError());
1331
1332 pkgAcquire fetcher(&status_);
1333 _assert(list.GetIndexes(&fetcher));
1334 _assert(fetcher.Run(PulseInterval_) != pkgAcquire::Failed);
1335
1336 bool failed = false;
1337 for (pkgAcquire::ItemIterator item = fetcher.ItemsBegin(); item != fetcher.ItemsEnd(); item++)
1338 if ((*item)->Status != pkgAcquire::Item::StatDone) {
1339 (*item)->Finished();
1340 failed = true;
1341 }
1342
1343 if (!failed && _config->FindB("APT::Get::List-Cleanup", true) == true) {
1344 _assert(fetcher.Clean(_config->FindDir("Dir::State::lists")));
1345 _assert(fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/"));
1346 }
1347 }
1348
1349 - (void) upgrade {
1350 _assert(cache_->DelCount() == 0 && cache_->InstCount() == 0);
1351 _assert(pkgApplyStatus(cache_));
1352
1353 if (cache_->BrokenCount() != 0) {
1354 _assert(pkgFixBroken(cache_));
1355 _assert(cache_->BrokenCount() == 0);
1356 _assert(pkgMinimizeUpgrade(cache_));
1357 }
1358
1359 _assert(pkgDistUpgrade(cache_));
1360 }
1361
1362 - (void) setDelegate:(id)delegate {
1363 delegate_ = delegate;
1364 status_.setDelegate(delegate);
1365 progress_.setDelegate(delegate);
1366 }
1367
1368 @end
1369
1370 /* Progress Data {{{ */
1371 @interface ProgressData : NSObject {
1372 SEL selector_;
1373 id target_;
1374 id object_;
1375 }
1376
1377 - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object;
1378
1379 - (SEL) selector;
1380 - (id) target;
1381 - (id) object;
1382 @end
1383
1384 @implementation ProgressData
1385
1386 - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object {
1387 if ((self = [super init]) != nil) {
1388 selector_ = selector;
1389 target_ = target;
1390 object_ = object;
1391 } return self;
1392 }
1393
1394 - (SEL) selector {
1395 return selector_;
1396 }
1397
1398 - (id) target {
1399 return target_;
1400 }
1401
1402 - (id) object {
1403 return object_;
1404 }
1405
1406 @end
1407 /* }}} */
1408 /* Progress View {{{ */
1409 @interface ProgressView : UIView <
1410 ProgressDelegate
1411 > {
1412 UIView *view_;
1413 UIView *background_;
1414 UITransitionView *transition_;
1415 UIView *overlay_;
1416 UINavigationBar *navbar_;
1417 UIProgressBar *progress_;
1418 UITextView *output_;
1419 UITextLabel *status_;
1420 id delegate_;
1421 UIAlertSheet *alert_;
1422 }
1423
1424 - (void) dealloc;
1425
1426 - (ProgressView *) initWithFrame:(struct CGRect)frame delegate:(id)delegate;
1427 - (void) setContentView:(UIView *)view;
1428 - (void) resetView;
1429
1430 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button;
1431
1432 - (void) _retachThread;
1433 - (void) _detachNewThreadData:(ProgressData *)data;
1434 - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object;
1435
1436 - (void) setError:(NSString *)error;
1437 - (void) _setError:(NSString *)error;
1438
1439 - (void) setTitle:(NSString *)title;
1440 - (void) _setTitle:(NSString *)title;
1441
1442 - (void) setPercent:(float)percent;
1443 - (void) _setPercent:(NSNumber *)percent;
1444
1445 - (void) addOutput:(NSString *)output;
1446 - (void) _addOutput:(NSString *)output;
1447
1448 - (void) setStatusFail;
1449 @end
1450
1451 @protocol ProgressViewDelegate
1452 - (void) progressViewIsComplete:(ProgressView *)sender;
1453 @end
1454
1455 @implementation ProgressView
1456
1457 - (void) dealloc {
1458 [view_ release];
1459 [background_ release];
1460 [transition_ release];
1461 [overlay_ release];
1462 [navbar_ release];
1463 [progress_ release];
1464 [output_ release];
1465 [status_ release];
1466 [super dealloc];
1467 }
1468
1469 - (ProgressView *) initWithFrame:(struct CGRect)frame delegate:(id)delegate {
1470 if ((self = [super initWithFrame:frame]) != nil) {
1471 delegate_ = delegate;
1472 alert_ = nil;
1473
1474 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
1475 float black[] = {0.0, 0.0, 0.0, 1.0};
1476 float white[] = {1.0, 1.0, 1.0, 1.0};
1477 float clear[] = {0.0, 0.0, 0.0, 0.0};
1478
1479 background_ = [[UIView alloc] initWithFrame:[self bounds]];
1480 [background_ setBackgroundColor:CGColorCreate(space, black)];
1481 [self addSubview:background_];
1482
1483 transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]];
1484 [self addSubview:transition_];
1485
1486 overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]];
1487
1488 CGSize navsize = [UINavigationBar defaultSize];
1489 CGRect navrect = {{0, 0}, navsize};
1490
1491 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
1492 [overlay_ addSubview:navbar_];
1493
1494 [navbar_ setBarStyle:1];
1495 [navbar_ setDelegate:self];
1496
1497 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Running..."] autorelease];
1498 [navbar_ pushNavigationItem:navitem];
1499
1500 CGRect bounds = [overlay_ bounds];
1501 CGSize prgsize = [UIProgressBar defaultSize];
1502
1503 CGRect prgrect = {{
1504 (bounds.size.width - prgsize.width) / 2,
1505 bounds.size.height - prgsize.height - 20
1506 }, prgsize};
1507
1508 progress_ = [[UIProgressBar alloc] initWithFrame:prgrect];
1509 [overlay_ addSubview:progress_];
1510
1511 status_ = [[UITextLabel alloc] initWithFrame:CGRectMake(
1512 10,
1513 bounds.size.height - prgsize.height - 50,
1514 bounds.size.width - 20,
1515 24
1516 )];
1517
1518 [status_ setColor:CGColorCreate(space, white)];
1519 [status_ setBackgroundColor:CGColorCreate(space, clear)];
1520
1521 [status_ setCentersHorizontally:YES];
1522 //[status_ setFont:font];
1523
1524 output_ = [[UITextView alloc] initWithFrame:CGRectMake(
1525 10,
1526 navrect.size.height + 20,
1527 bounds.size.width - 20,
1528 bounds.size.height - navsize.height - 62 - navrect.size.height
1529 )];
1530
1531 //[output_ setTextFont:@"Courier New"];
1532 [output_ setTextSize:12];
1533
1534 [output_ setTextColor:CGColorCreate(space, white)];
1535 [output_ setBackgroundColor:CGColorCreate(space, clear)];
1536
1537 [output_ setMarginTop:0];
1538 [output_ setAllowsRubberBanding:YES];
1539
1540 [overlay_ addSubview:output_];
1541 [overlay_ addSubview:status_];
1542
1543 [progress_ setStyle:0];
1544 } return self;
1545 }
1546
1547 - (void) setContentView:(UIView *)view {
1548 view_ = view;
1549 }
1550
1551 - (void) resetView {
1552 [transition_ transition:6 toView:view_];
1553 }
1554
1555 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
1556 [alert_ dismiss];
1557 [alert_ release];
1558 alert_ = nil;
1559 }
1560
1561 - (void) _retachThread {
1562 [delegate_ progressViewIsComplete:self];
1563 [self resetView];
1564 }
1565
1566 - (void) _detachNewThreadData:(ProgressData *)data {
1567 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1568
1569 [[data target] performSelector:[data selector] withObject:[data object]];
1570 [self performSelectorOnMainThread:@selector(_retachThread) withObject:nil waitUntilDone:YES];
1571
1572 [data release];
1573 [pool release];
1574 }
1575
1576 - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object {
1577 [status_ setText:nil];
1578 [output_ setText:@""];
1579 [progress_ setProgress:0];
1580
1581 [transition_ transition:6 toView:overlay_];
1582
1583 [NSThread
1584 detachNewThreadSelector:@selector(_detachNewThreadData:)
1585 toTarget:self
1586 withObject:[[ProgressData alloc]
1587 initWithSelector:selector
1588 target:target
1589 object:object
1590 ]
1591 ];
1592 }
1593
1594 - (void) setStatusFail {
1595 }
1596
1597 - (void) setError:(NSString *)error {
1598 [self
1599 performSelectorOnMainThread:@selector(_setError:)
1600 withObject:error
1601 waitUntilDone:YES
1602 ];
1603 }
1604
1605 - (void) _setError:(NSString *)error {
1606 _assert(alert_ == nil);
1607
1608 alert_ = [[UIAlertSheet alloc]
1609 initWithTitle:@"Package Error"
1610 buttons:[NSArray arrayWithObjects:@"Okay", nil]
1611 defaultButtonIndex:0
1612 delegate:self
1613 context:self
1614 ];
1615
1616 [alert_ setBodyText:error];
1617 [alert_ popupAlertAnimated:YES];
1618 }
1619
1620 - (void) setTitle:(NSString *)title {
1621 [self
1622 performSelectorOnMainThread:@selector(_setTitle:)
1623 withObject:title
1624 waitUntilDone:YES
1625 ];
1626 }
1627
1628 - (void) _setTitle:(NSString *)title {
1629 [status_ setText:[title stringByAppendingString:@"..."]];
1630 }
1631
1632 - (void) setPercent:(float)percent {
1633 [self
1634 performSelectorOnMainThread:@selector(_setPercent:)
1635 withObject:[NSNumber numberWithFloat:percent]
1636 waitUntilDone:YES
1637 ];
1638 }
1639
1640 - (void) _setPercent:(NSNumber *)percent {
1641 [progress_ setProgress:[percent floatValue]];
1642 }
1643
1644 - (void) addOutput:(NSString *)output {
1645 [self
1646 performSelectorOnMainThread:@selector(_addOutput:)
1647 withObject:output
1648 waitUntilDone:YES
1649 ];
1650 }
1651
1652 - (void) _addOutput:(NSString *)output {
1653 [output_ setText:[NSString stringWithFormat:@"%@\n%@", [output_ text], output]];
1654 CGSize size = [output_ contentSize];
1655 CGRect rect = {{0, size.height}, {size.width, 0}};
1656 [output_ scrollRectToVisible:rect animated:YES];
1657 }
1658
1659 @end
1660 /* }}} */
1661
1662 @protocol PackagesViewDelegate
1663 - (void) perform;
1664 - (void) update;
1665 - (void) openURL:(NSString *)url;
1666 @end
1667
1668 /* PackagesView {{{ */
1669 @interface PackagesView : ResetView <
1670 PackageCellDelegate
1671 > {
1672 Database *database_;
1673 NSMutableArray *packages_;
1674 NSMutableArray *sections_;
1675 id delegate_;
1676 UISectionList *list_;
1677 UITransitionView *transition_;
1678 Package *package_;
1679 NSString *pkgname_;
1680 PackageView *pkgview_;
1681 }
1682
1683 - (int) numberOfSectionsInSectionList:(UISectionList *)list;
1684 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section;
1685 - (int) sectionList:(UISectionList *)list rowForSection:(int)section;
1686
1687 - (int) numberOfRowsInTable:(UITable *)table;
1688 - (float) table:(UITable *)table heightForRow:(int)row;
1689 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col;
1690 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row;
1691 - (void) tableRowSelected:(NSNotification*)notification;
1692
1693 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button;
1694 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item;
1695
1696 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database;
1697 - (void) setDelegate:(id)delegate;
1698 - (void) deselect;
1699 - (void) reloadData:(BOOL)reset;
1700
1701 - (NSString *) title;
1702 - (void) perform:(Package *)package;
1703 - (void) addPackage:(Package *)package;
1704 - (NSString *) versionWithPackage:(Package *)package;
1705 @end
1706
1707 @implementation PackagesView
1708
1709 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
1710 return [sections_ count];
1711 }
1712
1713 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
1714 return [[sections_ objectAtIndex:section] name];
1715 }
1716
1717 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
1718 return [[sections_ objectAtIndex:section] row];
1719 }
1720
1721 - (int) numberOfRowsInTable:(UITable *)table {
1722 return [packages_ count];
1723 }
1724
1725 - (float) table:(UITable *)table heightForRow:(int)row {
1726 return 64;
1727 }
1728
1729 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col {
1730 Package *package = [packages_ objectAtIndex:row];
1731 PackageCell *cell = [[[PackageCell alloc] initWithPackage:package delegate:self] autorelease];
1732 return cell;
1733 }
1734
1735 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
1736 return YES;
1737 }
1738
1739 - (void) tableRowSelected:(NSNotification*)notification {
1740 int row = [[list_ table] selectedRow];
1741 if (row == INT_MAX)
1742 return;
1743
1744 package_ = [packages_ objectAtIndex:row];
1745 pkgname_ = [[package_ name] retain];
1746
1747 UINavigationItem *navitem = [[UINavigationItem alloc] initWithTitle:[package_ name]];
1748 [navbar_ pushNavigationItem:navitem];
1749
1750 [navbar_ showButtonsWithLeftTitle:nil rightTitle:[self title]];
1751
1752 [pkgview_ setPackage:package_];
1753 [transition_ transition:1 toView:pkgview_];
1754 }
1755
1756 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
1757 if (button == 0) {
1758 [self perform:package_];
1759
1760 pkgProblemResolver *resolver = [database_ resolver];
1761
1762 resolver->InstallProtect();
1763 if (!resolver->Resolve(true))
1764 _error->Discard();
1765
1766 [delegate_ perform];
1767 }
1768 }
1769
1770 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
1771 [self deselect];
1772 [super navigationBar:navbar poppedItem:item];
1773 }
1774
1775 - (id) initWithFrame:(struct CGRect)frame database:(Database *)database {
1776 if ((self = [super initWithFrame:frame]) != nil) {
1777 database_ = [database retain];
1778
1779 struct CGRect bounds = [self bounds];
1780 CGSize navsize = [UINavigationBar defaultSize];
1781 CGRect navrect = {{0, 0}, navsize};
1782
1783 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
1784 [self addSubview:navbar_];
1785
1786 [navbar_ setBarStyle:1];
1787 [navbar_ setDelegate:self];
1788
1789 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:[self title]] autorelease];
1790 [navbar_ pushNavigationItem:navitem];
1791 [navitem setBackButtonTitle:@"Packages"];
1792
1793 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
1794 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
1795 )];
1796
1797 [self addSubview:transition_];
1798
1799 list_ = [[UISectionList alloc] initWithFrame:[transition_ bounds] showSectionIndex:NO];
1800 [list_ setDataSource:self];
1801 [list_ setShouldHideHeaderInShortLists:NO];
1802
1803 [transition_ transition:0 toView:list_];
1804
1805 UITableColumn *column = [[UITableColumn alloc]
1806 initWithTitle:@"Name"
1807 identifier:@"name"
1808 width:frame.size.width
1809 ];
1810
1811 UITable *table = [list_ table];
1812 [table setSeparatorStyle:1];
1813 [table addTableColumn:column];
1814 [table setDelegate:self];
1815
1816 pkgview_ = [[PackageView alloc] initWithFrame:[transition_ bounds] database:database_];
1817 } return self;
1818 }
1819
1820 - (void) setDelegate:(id)delegate {
1821 delegate_ = delegate;
1822 [pkgview_ setDelegate:delegate];
1823 }
1824
1825 - (void) deselect {
1826 [transition_ transition:(resetting_ ? 0 : 2) toView:list_];
1827 UITable *table = [list_ table];
1828 [table selectRow:-1 byExtendingSelection:NO withFade:(resetting_ ? NO : YES)];
1829 package_ = nil;
1830 }
1831
1832 - (void) reloadData:(BOOL)reset {
1833 packages_ = [[NSMutableArray arrayWithCapacity:16] retain];
1834
1835 if (sections_ != nil) {
1836 [sections_ release];
1837 sections_ = nil;
1838 }
1839
1840 for (pkgCache::PkgIterator iterator = [database_ cache]->PkgBegin(); !iterator.end(); ++iterator)
1841 if (Package *package = [Package packageWithIterator:iterator database:database_])
1842 [self addPackage:package];
1843
1844 [packages_ sortUsingSelector:@selector(compareBySectionAndName:)];
1845 sections_ = [[NSMutableArray arrayWithCapacity:16] retain];
1846
1847 Section *section = nil;
1848 for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) {
1849 Package *package = [packages_ objectAtIndex:offset];
1850 NSString *name = [package section];
1851
1852 if (section == nil || ![[section name] isEqual:name]) {
1853 section = [[Section alloc] initWithName:name row:offset];
1854 [sections_ addObject:section];
1855 }
1856
1857 [section addPackage:package];
1858 }
1859
1860 [list_ reloadData];
1861 if (reset)
1862 [self resetView];
1863 else if (package_ != nil) {
1864 package_ = [database_ packageWithName:pkgname_];
1865 [pkgview_ setPackage:package_];
1866 }
1867 }
1868
1869 - (NSString *) title {
1870 return nil;
1871 }
1872
1873 - (void) perform:(Package *)package {
1874 }
1875
1876 - (void) addPackage:(Package *)package {
1877 [packages_ addObject:package];
1878 }
1879
1880 - (NSString *) versionWithPackage:(Package *)package {
1881 return nil;
1882 }
1883
1884 @end
1885 /* }}} */
1886
1887 /* InstallView {{{ */
1888 @interface InstallView : PackagesView {
1889 }
1890
1891 - (NSString *) title;
1892 - (void) addPackage:(Package *)package;
1893 - (void) perform:(Package *)package;
1894 - (NSString *) versionWithPackage:(Package *)package;
1895 @end
1896
1897 @implementation InstallView
1898
1899 - (NSString *) title {
1900 return @"Install";
1901 }
1902
1903 - (void) addPackage:(Package *)package {
1904 if ([package installed] == nil)
1905 [super addPackage:package];
1906 }
1907
1908 - (void) perform:(Package *)package {
1909 [package install];
1910 }
1911
1912 - (NSString *) versionWithPackage:(Package *)package {
1913 return [package latest];
1914 }
1915
1916 @end
1917 /* }}} */
1918 /* UpgradeView {{{ */
1919 @interface UpgradeView : PackagesView {
1920 }
1921
1922 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button;
1923
1924 - (NSString *) title;
1925 - (NSString *) leftTitle;
1926 - (void) addPackage:(Package *)package;
1927 - (void) perform:(Package *)package;
1928 - (NSString *) versionWithPackage:(Package *)package;
1929 @end
1930
1931 @implementation UpgradeView
1932
1933 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
1934 if (button != 1)
1935 [super navigationBar:navbar buttonClicked:button];
1936 else {
1937 [database_ upgrade];
1938 [delegate_ perform];
1939 }
1940 }
1941
1942 - (NSString *) title {
1943 return @"Upgrade";
1944 }
1945
1946 - (NSString *) leftTitle {
1947 return @"Upgrade All";
1948 }
1949
1950 - (void) addPackage:(Package *)package {
1951 NSString *installed = [package installed];
1952 if (installed != nil && [[package latest] compare:installed] != NSOrderedSame)
1953 [super addPackage:package];
1954 }
1955
1956 - (void) perform:(Package *)package {
1957 [package install];
1958 }
1959
1960 - (NSString *) versionWithPackage:(Package *)package {
1961 return [package latest];
1962 }
1963
1964 @end
1965 /* }}} */
1966 /* UninstallView {{{ */
1967 @interface UninstallView : PackagesView {
1968 }
1969
1970 - (NSString *) title;
1971 - (void) addPackage:(Package *)package;
1972 - (void) perform:(Package *)package;
1973 - (NSString *) versionWithPackage:(Package *)package;
1974 @end
1975
1976 @implementation UninstallView
1977
1978 - (NSString *) title {
1979 return @"Uninstall";
1980 }
1981
1982 - (void) addPackage:(Package *)package {
1983 if ([package installed] != nil)
1984 [super addPackage:package];
1985 }
1986
1987 - (void) perform:(Package *)package {
1988 [package remove];
1989 }
1990
1991 - (NSString *) versionWithPackage:(Package *)package {
1992 return [package installed];
1993 }
1994
1995 @end
1996 /* }}} */
1997
1998 @interface Cydia : UIApplication <
1999 ConfirmationViewDelegate,
2000 PackagesViewDelegate,
2001 ProgressViewDelegate
2002 > {
2003 UIWindow *window_;
2004 UIView *underlay_;
2005 UIView *overlay_;
2006 UITransitionView *transition_;
2007 UIButtonBar *buttonbar_;
2008
2009 UIAlertSheet *alert_;
2010 ConfirmationView *confirm_;
2011
2012 Database *database_;
2013 ProgressView *progress_;
2014
2015 UIView *featured_;
2016 UINavigationBar *navbar_;
2017 UIScroller *scroller_;
2018 UIWebView *webview_;
2019 NSURL *url_;
2020
2021 InstallView *install_;
2022 UpgradeView *upgrade_;
2023 UninstallView *uninstall_;
2024 SourcesView *sources_;
2025 }
2026
2027 - (void) loadNews;
2028 - (void) reloadData:(BOOL)reset;
2029 - (void) perform;
2030 - (void) cancel;
2031 - (void) confirm;
2032 - (void) update;
2033
2034 - (void) progressViewIsComplete:(ProgressView *)progress;
2035
2036 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button;
2037 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button;
2038 - (void) buttonBarItemTapped:(id)sender;
2039
2040 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame;
2041 - (void) view:(UIView *)view didDrawInRect:(CGRect)rect duration:(float)duration;
2042
2043 - (void) applicationDidFinishLaunching:(id)unused;
2044 @end
2045
2046 #include <objc/objc-class.h>
2047
2048 @implementation Cydia
2049
2050 - (void) loadNews {
2051 NSMutableURLRequest *request = [NSMutableURLRequest
2052 requestWithURL:url_
2053 cachePolicy:NSURLRequestReloadIgnoringCacheData
2054 timeoutInterval:30.0
2055 ];
2056
2057 [request addValue:[NSString stringWithCString:Machine_] forHTTPHeaderField:@"X-Machine"];
2058 [request addValue:[NSString stringWithCString:SerialNumber_] forHTTPHeaderField:@"X-Serial-Number"];
2059
2060 [webview_ loadRequest:request];
2061 }
2062
2063 - (void) reloadData:(BOOL)reset {
2064 [database_ reloadData];
2065 [install_ reloadData:reset];
2066 [upgrade_ reloadData:reset];
2067 [uninstall_ reloadData:reset];
2068 [sources_ reloadData];
2069 }
2070
2071 - (void) perform {
2072 [database_ prepare];
2073 confirm_ = [[ConfirmationView alloc] initWithView:underlay_ database:database_ delegate:self];
2074 }
2075
2076 - (void) cancel {
2077 [self reloadData:NO];
2078 [confirm_ release];
2079 confirm_ = nil;
2080 }
2081
2082 - (void) confirm {
2083 [overlay_ removeFromSuperview];
2084
2085 [progress_
2086 detachNewThreadSelector:@selector(perform)
2087 toTarget:database_
2088 withObject:nil
2089 ];
2090 }
2091
2092 - (void) update {
2093 [progress_
2094 detachNewThreadSelector:@selector(update)
2095 toTarget:database_
2096 withObject:nil
2097 ];
2098 }
2099
2100 - (void) progressViewIsComplete:(ProgressView *)progress {
2101 [self reloadData:YES];
2102
2103 if (confirm_ != nil) {
2104 [underlay_ addSubview:overlay_];
2105 [confirm_ removeFromSuperview];
2106 [confirm_ release];
2107 confirm_ = nil;
2108 }
2109 }
2110
2111 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
2112 switch (button) {
2113 case 0:
2114 [self loadNews];
2115 break;
2116
2117 case 1:
2118 _assert(alert_ == nil);
2119
2120 alert_ = [[UIAlertSheet alloc]
2121 initWithTitle:@"About Cydia Packager"
2122 buttons:[NSArray arrayWithObjects:@"Close", nil]
2123 defaultButtonIndex:0
2124 delegate:self
2125 context:self
2126 ];
2127
2128 [alert_ setBodyText:
2129 @"Copyright (C) 2007\n"
2130 "Jay Freeman (saurik)\n"
2131 "saurik@saurik.com\n"
2132 "http://www.saurik.com/\n"
2133 "\n"
2134 "The Okori Group\n"
2135 "http://www.theokorigroup.com/\n"
2136 "\n"
2137 "College of Creative Studies,\n"
2138 "University of California,\n"
2139 "Santa Barbara\n"
2140 "http://www.ccs.ucsb.edu/\n"
2141 "\n"
2142 "Special Thanks:\n"
2143 "bad_, BHSPitMonkey, Cobra, core,\n"
2144 "Corona, cromas, Darken, dtzWill,\n"
2145 "francis, Godores, jerry, Kingstone,\n"
2146 "lounger, rockabilly, tman, Wbiggs"
2147 ];
2148
2149 [alert_ presentSheetFromButtonBar:buttonbar_];
2150 break;
2151 }
2152 }
2153
2154 - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
2155 [alert_ dismiss];
2156 [alert_ release];
2157 alert_ = nil;
2158 }
2159
2160 - (void) buttonBarItemTapped:(id)sender {
2161 UIView *view;
2162
2163 switch ([sender tag]) {
2164 case 1: view = featured_; break;
2165 case 2: view = install_; break;
2166 case 3: view = upgrade_; break;
2167 case 4: view = uninstall_; break;
2168 case 5: view = sources_; break;
2169
2170 default:
2171 _assert(false);
2172 }
2173
2174 if ([view respondsToSelector:@selector(resetView)])
2175 [(id) view resetView];
2176 [transition_ transition:0 toView:view];
2177 }
2178
2179 - (void) view:(UIView *)view didSetFrame:(CGRect)frame {
2180 [scroller_ setContentSize:frame.size];
2181 }
2182
2183 - (void) view:(UIView *)view didDrawInRect:(CGRect)rect duration:(float)duration {
2184 [scroller_ setContentSize:[webview_ bounds].size];
2185 }
2186
2187 - (void) applicationDidFinishLaunching:(id)unused {
2188 _assert(pkgInitConfig(*_config));
2189 _assert(pkgInitSystem(*_config, _system));
2190
2191 alert_ = nil;
2192 confirm_ = nil;
2193
2194 CGRect screenrect = [UIHardware fullScreenApplicationContentRect];
2195 window_ = [[UIWindow alloc] initWithContentRect:screenrect];
2196
2197 [window_ orderFront: self];
2198 [window_ makeKey: self];
2199 [window_ _setHidden: NO];
2200
2201 progress_ = [[ProgressView alloc] initWithFrame:[window_ bounds] delegate:self];
2202 [window_ setContentView:progress_];
2203
2204 underlay_ = [[UIView alloc] initWithFrame:[progress_ bounds]];
2205 [progress_ setContentView:underlay_];
2206
2207 overlay_ = [[UIView alloc] initWithFrame:[underlay_ bounds]];
2208 [underlay_ addSubview:overlay_];
2209
2210 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
2211 0, 0, screenrect.size.width, screenrect.size.height - 48
2212 )];
2213
2214 [overlay_ addSubview:transition_];
2215
2216 featured_ = [[UIView alloc] initWithFrame:[transition_ bounds]];
2217
2218 CGSize navsize = [UINavigationBar defaultSize];
2219 CGRect navrect = {{0, 0}, navsize};
2220
2221 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
2222 [featured_ addSubview:navbar_];
2223
2224 [navbar_ setBarStyle:1];
2225 [navbar_ setDelegate:self];
2226
2227 [navbar_ showButtonsWithLeftTitle:@"About" rightTitle:@"Reload"];
2228
2229 UINavigationItem *navitem = [[UINavigationItem alloc] initWithTitle:@"Featured"];
2230 [navbar_ pushNavigationItem:navitem];
2231
2232 struct CGRect subbounds = [featured_ bounds];
2233 subbounds.origin.y += navsize.height;
2234 subbounds.size.height -= navsize.height;
2235
2236 UIImageView *pinstripe = [[UIImageView alloc] initWithFrame:subbounds];
2237 [pinstripe setImage:[UIImage applicationImageNamed:@"pinstripe.png"]];
2238 [featured_ addSubview:pinstripe];
2239
2240 scroller_ = [[UIScroller alloc] initWithFrame:subbounds];
2241 [featured_ addSubview:scroller_];
2242
2243 [scroller_ setScrollingEnabled:YES];
2244 [scroller_ setAdjustForContentSizeChange:YES];
2245 [scroller_ setClipsSubviews:YES];
2246 [scroller_ setAllowsRubberBanding:YES];
2247 [scroller_ setScrollDecelerationFactor:0.99];
2248 [scroller_ setDelegate:self];
2249
2250 webview_ = [[UIWebView alloc] initWithFrame:[scroller_ bounds]];
2251 [scroller_ addSubview:webview_];
2252
2253 [webview_ setTilingEnabled:YES];
2254 [webview_ setTileSize:CGSizeMake(screenrect.size.width, 500)];
2255 [webview_ setAutoresizes:YES];
2256 [webview_ setDelegate:self];
2257
2258 NSArray *buttonitems = [NSArray arrayWithObjects:
2259 [NSDictionary dictionaryWithObjectsAndKeys:
2260 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
2261 @"featured-up.png", kUIButtonBarButtonInfo,
2262 @"featured-dn.png", kUIButtonBarButtonSelectedInfo,
2263 [NSNumber numberWithInt:1], kUIButtonBarButtonTag,
2264 self, kUIButtonBarButtonTarget,
2265 @"Featured", kUIButtonBarButtonTitle,
2266 @"0", kUIButtonBarButtonType,
2267 nil],
2268
2269 [NSDictionary dictionaryWithObjectsAndKeys:
2270 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
2271 @"install-up.png", kUIButtonBarButtonInfo,
2272 @"install-dn.png", kUIButtonBarButtonSelectedInfo,
2273 [NSNumber numberWithInt:2], kUIButtonBarButtonTag,
2274 self, kUIButtonBarButtonTarget,
2275 @"Install", kUIButtonBarButtonTitle,
2276 @"0", kUIButtonBarButtonType,
2277 nil],
2278
2279 [NSDictionary dictionaryWithObjectsAndKeys:
2280 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
2281 @"upgrade-up.png", kUIButtonBarButtonInfo,
2282 @"upgrade-dn.png", kUIButtonBarButtonSelectedInfo,
2283 [NSNumber numberWithInt:3], kUIButtonBarButtonTag,
2284 self, kUIButtonBarButtonTarget,
2285 @"Upgrade", kUIButtonBarButtonTitle,
2286 @"0", kUIButtonBarButtonType,
2287 nil],
2288
2289 [NSDictionary dictionaryWithObjectsAndKeys:
2290 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
2291 @"uninstall-up.png", kUIButtonBarButtonInfo,
2292 @"uninstall-dn.png", kUIButtonBarButtonSelectedInfo,
2293 [NSNumber numberWithInt:4], kUIButtonBarButtonTag,
2294 self, kUIButtonBarButtonTarget,
2295 @"Uninstall", kUIButtonBarButtonTitle,
2296 @"0", kUIButtonBarButtonType,
2297 nil],
2298
2299 [NSDictionary dictionaryWithObjectsAndKeys:
2300 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
2301 @"sources-up.png", kUIButtonBarButtonInfo,
2302 @"sources-dn.png", kUIButtonBarButtonSelectedInfo,
2303 [NSNumber numberWithInt:5], kUIButtonBarButtonTag,
2304 self, kUIButtonBarButtonTarget,
2305 @"Sources", kUIButtonBarButtonTitle,
2306 @"0", kUIButtonBarButtonType,
2307 nil],
2308 nil];
2309
2310 buttonbar_ = [[UIButtonBar alloc]
2311 initInView:overlay_
2312 withFrame:CGRectMake(
2313 0, screenrect.size.height - 48,
2314 screenrect.size.width, 48
2315 )
2316 withItemList:buttonitems
2317 ];
2318
2319 [buttonbar_ setDelegate:self];
2320 [buttonbar_ setBarStyle:1];
2321 [buttonbar_ setButtonBarTrackingMode:2];
2322
2323 int buttons[5] = {1, 2, 3, 4, 5};
2324 [buttonbar_ registerButtonGroup:0 withButtons:buttons withCount:5];
2325 [buttonbar_ showButtonGroup:0 withDuration:0];
2326
2327 for (int i = 0; i != 5; ++i)
2328 [[buttonbar_ viewWithTag:(i + 1)] setFrame:CGRectMake(
2329 i * 64 + 2, 1, 60, 48
2330 )];
2331
2332 [buttonbar_ showSelectionForButton:1];
2333 [transition_ transition:0 toView:featured_];
2334
2335 [overlay_ addSubview:buttonbar_];
2336
2337 database_ = [[Database alloc] init];
2338 [database_ setDelegate:progress_];
2339
2340 install_ = [[InstallView alloc] initWithFrame:[transition_ bounds] database:database_];
2341 [install_ setDelegate:self];
2342
2343 upgrade_ = [[UpgradeView alloc] initWithFrame:[transition_ bounds] database:database_];
2344 [upgrade_ setDelegate:self];
2345
2346 uninstall_ = [[UninstallView alloc] initWithFrame:[transition_ bounds] database:database_];
2347 [uninstall_ setDelegate:self];
2348
2349 sources_ = [[SourcesView alloc] initWithFrame:[transition_ bounds] database:database_];
2350 [sources_ setDelegate:self];
2351
2352 #if 0
2353
2354 UIAlertSheet *alert = [[UIAlertSheet alloc]
2355 initWithTitle:@"Alert Title"
2356 buttons:[NSArray arrayWithObjects:@"Yes", nil]
2357 defaultButtonIndex:0
2358 delegate:self
2359 context:self
2360 ];
2361
2362 NSLog(@"%p\n", [alert table]);
2363 [[alert table] setDelegate:self];
2364 [[alert table] reloadData];
2365
2366 [alert addTextFieldWithValue:@"Title" label:@"Label"];
2367 [alert setShowsOverSpringBoardAlerts:YES];
2368 [alert setBodyText:@"This is an alert."];
2369 [alert presentSheetFromButtonBar:buttonbar_];
2370 //[alert popupAlertAnimated:YES];
2371
2372 #endif
2373
2374 [self reloadData:NO];
2375 [progress_ resetView];
2376
2377 Package *package([database_ packageWithName:@"cydia"]);
2378 NSString *application = package == nil ? @"Cydia" : [NSString stringWithFormat:@"Cydia/%@", [package installed]];
2379 WebView *webview = [webview_ webView];
2380 [webview setApplicationNameForUserAgent:application];
2381
2382 url_ = [NSURL URLWithString:@"http://cydia.saurik.com/"];
2383 [self loadNews];
2384 }
2385
2386 @end
2387
2388 int main(int argc, char *argv[]) {
2389 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2390
2391 size_t size;
2392 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
2393 char *machine = new char[size];
2394 sysctlbyname("hw.machine", machine, &size, NULL, 0);
2395 Machine_ = machine;
2396
2397 if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice"))
2398 if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) {
2399 if (CFTypeRef serial = IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0)) {
2400 SerialNumber_ = strdup(CFStringGetCStringPtr((CFStringRef) serial, CFStringGetSystemEncoding()));
2401 CFRelease(serial);
2402 }
2403
2404 IOObjectRelease(service);
2405 }
2406
2407 UIApplicationMain(argc, argv, [Cydia class]);
2408 [pool release];
2409 }