]> git.saurik.com Git - cydia.git/blob - Preferences.mm
URL Authentication.
[cydia.git] / Preferences.mm
1 /* Source {{{ */
2 @interface Source : NSObject {
3 NSString *description_;
4 NSString *label_;
5 NSString *origin_;
6
7 NSString *uri_;
8 NSString *distribution_;
9 NSString *type_;
10
11 BOOL trusted_;
12 }
13
14 - (void) dealloc;
15
16 - (Source *) initWithMetaIndex:(metaIndex *)index;
17
18 - (BOOL) trusted;
19
20 - (NSString *) uri;
21 - (NSString *) distribution;
22 - (NSString *) type;
23
24 - (NSString *) description;
25 - (NSString *) label;
26 - (NSString *) origin;
27 @end
28
29 @implementation Source
30
31 - (void) dealloc {
32 [uri_ release];
33 [distribution_ release];
34 [type_ release];
35
36 if (description_ != nil)
37 [description_ release];
38 if (label_ != nil)
39 [label_ release];
40 if (origin_ != nil)
41 [origin_ release];
42
43 [super dealloc];
44 }
45
46 - (Source *) initWithMetaIndex:(metaIndex *)index {
47 if ((self = [super init]) != nil) {
48 trusted_ = index->IsTrusted();
49
50 uri_ = [[NSString stringWithCString:index->GetURI().c_str()] retain];
51 distribution_ = [[NSString stringWithCString:index->GetDist().c_str()] retain];
52 type_ = [[NSString stringWithCString:index->GetType()] retain];
53
54 description_ = nil;
55 label_ = nil;
56 origin_ = nil;
57
58 debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index));
59 if (dindex != NULL) {
60 std::ifstream release(dindex->MetaIndexFile("Release").c_str());
61 std::string line;
62 while (std::getline(release, line)) {
63 std::string::size_type colon(line.find(':'));
64 if (colon == std::string::npos)
65 continue;
66
67 std::string name(line.substr(0, colon));
68 std::string value(line.substr(colon + 1));
69 while (!value.empty() && value[0] == ' ')
70 value = value.substr(1);
71
72 if (name == "Description")
73 description_ = [[NSString stringWithCString:value.c_str()] retain];
74 else if (name == "Label")
75 label_ = [[NSString stringWithCString:value.c_str()] retain];
76 else if (name == "Origin")
77 origin_ = [[NSString stringWithCString:value.c_str()] retain];
78 }
79 }
80 } return self;
81 }
82
83 - (BOOL) trusted {
84 return trusted_;
85 }
86
87 - (NSString *) uri {
88 return uri_;
89 }
90
91 - (NSString *) distribution {
92 return distribution_;
93 }
94
95 - (NSString *) type {
96 return type_;
97 }
98
99 - (NSString *) description {
100 return description_;
101 }
102
103 - (NSString *) label {
104 return label_;
105 }
106
107 - (NSString *) origin {
108 return origin_;
109 }
110
111 @end
112 /* }}} */
113 /* Source Cell {{{ */
114 @interface SourceCell : UITableCell {
115 UITextLabel *description_;
116 UIRightTextLabel *label_;
117 UITextLabel *origin_;
118 }
119
120 - (void) dealloc;
121
122 - (SourceCell *) initWithSource:(Source *)source;
123
124 - (void) _setSelected:(float)fraction;
125 - (void) setSelected:(BOOL)selected;
126 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade;
127 - (void) _setSelectionFadeFraction:(float)fraction;
128
129 @end
130
131 @implementation SourceCell
132
133 - (void) dealloc {
134 [description_ release];
135 [label_ release];
136 [origin_ release];
137 [super dealloc];
138 }
139
140 - (SourceCell *) initWithSource:(Source *)source {
141 if ((self = [super init]) != nil) {
142 GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 20);
143 GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 14);
144
145 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
146 float clear[] = {0, 0, 0, 0};
147
148 NSString *description = [source description];
149 if (description == nil)
150 description = [source uri];
151
152 description_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 7, 270, 25)];
153 [description_ setBackgroundColor:CGColorCreate(space, clear)];
154 [description_ setFont:bold];
155 [description_ setText:description];
156
157 NSString *label = [source label];
158 if (label == nil)
159 label = [source type];
160
161 label_ = [[UIRightTextLabel alloc] initWithFrame:CGRectMake(290, 32, 90, 25)];
162 [label_ setBackgroundColor:CGColorCreate(space, clear)];
163 [label_ setFont:small];
164 [label_ setText:label];
165
166 NSString *origin = [source origin];
167 if (origin == nil)
168 origin = [source distribution];
169
170 origin_ = [[UITextLabel alloc] initWithFrame:CGRectMake(13, 35, 315, 20)];
171 [origin_ setBackgroundColor:CGColorCreate(space, clear)];
172 [origin_ setFont:small];
173 [origin_ setText:origin];
174
175 [self addSubview:description_];
176 [self addSubview:label_];
177 [self addSubview:origin_];
178
179 CFRelease(small);
180 CFRelease(bold);
181 } return self;
182 }
183
184 - (void) _setSelected:(float)fraction {
185 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
186
187 float black[] = {
188 interpolate(0.0, 1.0, fraction),
189 interpolate(0.0, 1.0, fraction),
190 interpolate(0.0, 1.0, fraction),
191 1.0};
192
193 float blue[] = {
194 interpolate(0.2, 1.0, fraction),
195 interpolate(0.2, 1.0, fraction),
196 interpolate(1.0, 1.0, fraction),
197 1.0};
198
199 float gray[] = {
200 interpolate(0.4, 1.0, fraction),
201 interpolate(0.4, 1.0, fraction),
202 interpolate(0.4, 1.0, fraction),
203 1.0};
204
205 [description_ setColor:CGColorCreate(space, black)];
206 [label_ setColor:CGColorCreate(space, blue)];
207 [origin_ setColor:CGColorCreate(space, gray)];
208 }
209
210 - (void) setSelected:(BOOL)selected {
211 [self _setSelected:(selected ? 1.0 : 0.0)];
212 [super setSelected:selected];
213 }
214
215 - (void) setSelected:(BOOL)selected withFade:(BOOL)fade {
216 if (!fade)
217 [self _setSelected:(selected ? 1.0 : 0.0)];
218 [super setSelected:selected withFade:fade];
219 }
220
221 - (void) _setSelectionFadeFraction:(float)fraction {
222 [self _setSelected:fraction];
223 [super _setSelectionFadeFraction:fraction];
224 }
225
226 @end
227 /* }}} */
228
229 /* Sources View {{{ */
230 @interface SourcesView : UIView {
231 UISectionList *list_;
232 Database *database_;
233 id delegate_;
234 NSMutableArray *sources_;
235 UIActionSheet *alert_;
236 }
237
238 - (int) numberOfSectionsInSectionList:(UISectionList *)list;
239 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section;
240 - (int) sectionList:(UISectionList *)list rowForSection:(int)section;
241
242 - (int) numberOfRowsInTable:(UITable *)table;
243 - (float) table:(UITable *)table heightForRow:(int)row;
244 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col;
245 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row;
246 - (void) tableRowSelected:(NSNotification*)notification;
247
248 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button;
249
250 - (void) dealloc;
251 - (id) initWithFrame:(CGRect)frame database:(Database *)database;
252 - (void) setDelegate:(id)delegate;
253 - (void) reloadData;
254 - (NSString *) leftTitle;
255 - (NSString *) rightTitle;
256 @end
257
258 @implementation SourcesView
259
260 - (int) numberOfSectionsInSectionList:(UISectionList *)list {
261 return 1;
262 }
263
264 - (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
265 return @"sources";
266 }
267
268 - (int) sectionList:(UISectionList *)list rowForSection:(int)section {
269 return 0;
270 }
271
272 - (int) numberOfRowsInTable:(UITable *)table {
273 return [sources_ count];
274 }
275
276 - (float) table:(UITable *)table heightForRow:(int)row {
277 return 64;
278 }
279
280 - (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col {
281 return [[[SourceCell alloc] initWithSource:[sources_ objectAtIndex:row]] autorelease];
282 }
283
284 - (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
285 return NO;
286 }
287
288 - (void) tableRowSelected:(NSNotification*)notification {
289 UITable *table([list_ table]);
290 int row([table selectedRow]);
291 if (row == INT_MAX)
292 return;
293
294 [table selectRow:-1 byExtendingSelection:NO withFade:YES];
295 }
296
297 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
298 [alert_ dismiss];
299 [alert_ release];
300 alert_ = nil;
301 }
302
303 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
304 switch (button) {
305 case 0:
306 alert_ = [[UIActionSheet alloc]
307 initWithTitle:@"Unimplemented"
308 buttons:[NSArray arrayWithObjects:@"Okay", nil]
309 defaultButtonIndex:0
310 delegate:self
311 context:self
312 ];
313
314 [alert_ setBodyText:@"This feature will be implemented soon. In the mean time, you may add sources by adding .list files to '/etc/apt/sources.list.d'. If you'd like to be in the default list, please contact the author of Packager."];
315 [alert_ popupAlertAnimated:YES];
316 break;
317
318 case 1:
319 [delegate_ update];
320 break;
321 }
322 }
323
324 - (void) dealloc {
325 if (sources_ != nil)
326 [sources_ release];
327 [list_ release];
328 [super dealloc];
329 }
330
331 - (id) initWithFrame:(CGRect)frame database:(Database *)database {
332 if ((self = [super initWithFrame:frame]) != nil) {
333 database_ = database;
334 sources_ = nil;
335
336 CGSize navsize = [UINavigationBar defaultSize];
337 CGRect navrect = {{0, 0}, navsize};
338 CGRect bounds = [self bounds];
339
340 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
341 [self addSubview:navbar_];
342
343 [navbar_ setBarStyle:1];
344 [navbar_ setDelegate:self];
345
346 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Sources"] autorelease];
347 [navbar_ pushNavigationItem:navitem];
348
349 list_ = [[UISectionList alloc] initWithFrame:CGRectMake(
350 0, navsize.height, bounds.size.width, bounds.size.height - navsize.height
351 )];
352
353 [self addSubview:list_];
354
355 [list_ setDataSource:self];
356 [list_ setShouldHideHeaderInShortLists:NO];
357
358 UITableColumn *column = [[UITableColumn alloc]
359 initWithTitle:@"Name"
360 identifier:@"name"
361 width:frame.size.width
362 ];
363
364 UITable *table = [list_ table];
365 [table setSeparatorStyle:1];
366 [table addTableColumn:column];
367 [table setDelegate:self];
368 } return self;
369 }
370
371 - (void) setDelegate:(id)delegate {
372 delegate_ = delegate;
373 }
374
375 - (void) reloadData {
376 pkgSourceList list;
377 _assert(list.ReadMainList());
378
379 if (sources_ != nil)
380 [sources_ release];
381
382 sources_ = [[NSMutableArray arrayWithCapacity:16] retain];
383 for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source)
384 [sources_ addObject:[[[Source alloc] initWithMetaIndex:*source] autorelease]];
385
386 [list_ reloadData];
387 }
388
389 - (NSString *) leftTitle {
390 return @"Refresh All";
391 }
392
393 - (NSString *) rightTitle {
394 return @"Edit";
395 }
396
397 @end
398 /* }}} */
399 /* Settings View {{{ */
400 @interface SettingsView : ResetView {
401 }
402
403 - (void) dealloc;
404 - (void) reloadData;
405 @end
406
407 @implementation SettingsView
408
409 - (void) dealloc {
410 [super dealloc];
411 }
412
413 - (id) initWithFrame:(CGRect)frame database:(Database *)database {
414 if ((self = [super initWithFrame:frame]) != nil) {
415 database_ = database;
416 sources_ = nil;
417
418 CGSize navsize = [UINavigationBar defaultSize];
419 CGRect navrect = {{0, 0}, navsize};
420 CGRect bounds = [self bounds];
421
422 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
423 [self addSubview:navbar_];
424
425 [navbar_ setBarStyle:1];
426 [navbar_ setDelegate:self];
427
428 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Settings"] autorelease];
429 [navbar_ pushNavigationItem:navitem];
430 } return self;
431 }
432
433 - (void) reloadData {
434 [self resetView];
435 }
436
437 @end
438 /* }}} */