summaryrefslogtreecommitdiff
path: root/Preferences.mm
blob: f576193077bfd6563050377b791cb757998a193d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* Source {{{ */
@interface Source : NSObject {
    NSString *description_;
    NSString *label_;
    NSString *origin_;

    NSString *uri_;
    NSString *distribution_;
    NSString *type_;

    BOOL trusted_;
}

- (void) dealloc;

- (Source *) initWithMetaIndex:(metaIndex *)index;

- (BOOL) trusted;

- (NSString *) uri;
- (NSString *) distribution;
- (NSString *) type;

- (NSString *) description;
- (NSString *) label;
- (NSString *) origin;
@end

@implementation Source

- (void) dealloc {
    [uri_ release];
    [distribution_ release];
    [type_ release];

    if (description_ != nil)
        [description_ release];
    if (label_ != nil)
        [label_ release];
    if (origin_ != nil)
        [origin_ release];

    [super dealloc];
}

- (Source *) initWithMetaIndex:(metaIndex *)index {
    if ((self = [super init]) != nil) {
        trusted_ = index->IsTrusted();

        uri_ = [[NSString stringWithCString:index->GetURI().c_str()] retain];
        distribution_ = [[NSString stringWithCString:index->GetDist().c_str()] retain];
        type_ = [[NSString stringWithCString:index->GetType()] retain];

        description_ = nil;
        label_ = nil;
        origin_ = nil;

        debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index));
        if (dindex != NULL) {
            std::ifstream release(dindex->MetaIndexFile("Release").c_str());
            std::string line;
            while (std::getline(release, line)) {
                std::string::size_type colon(line.find(':'));
                if (colon == std::string::npos)
                    continue;

                std::string name(line.substr(0, colon));
                std::string value(line.substr(colon + 1));
                while (!value.empty() && value[0] == ' ')
                    value = value.substr(1);

                if (name == "Description")
                    description_ = [[NSString stringWithCString:value.c_str()] retain];
                else if (name == "Label")
                    label_ = [[NSString stringWithCString:value.c_str()] retain];
                else if (name == "Origin")
                    origin_ = [[NSString stringWithCString:value.c_str()] retain];
            }
        }
    } return self;
}

- (BOOL) trusted {
    return trusted_;
}

- (NSString *) uri {
    return uri_;
}

- (NSString *) distribution {
    return distribution_;
}

- (NSString *) type {
    return type_;
}

- (NSString *) description {
    return description_;
}

- (NSString *) label {
    return label_;
}

- (NSString *) origin {
    return origin_;
}

@end
/* }}} */
/* Source Cell {{{ */
@interface SourceCell : UITableCell {
    UITextLabel *description_;
    UIRightTextLabel *label_;
    UITextLabel *origin_;
}

- (void) dealloc;

- (SourceCell *) initWithSource:(Source *)source;

- (void) _setSelected:(float)fraction;
- (void) setSelected:(BOOL)selected;
- (void) setSelected:(BOOL)selected withFade:(BOOL)fade;
- (void) _setSelectionFadeFraction:(float)fraction;

@end

@implementation SourceCell

- (void) dealloc {
    [description_ release];
    [label_ release];
    [origin_ release];
    [super dealloc];
}

- (SourceCell *) initWithSource:(Source *)source {
    if ((self = [super init]) != nil) {
        GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 20);
        GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 14);

        CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
        float clear[] = {0, 0, 0, 0};

        NSString *description = [source description];
        if (description == nil)
            description = [source uri];

        description_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 7, 270, 25)];
        [description_ setBackgroundColor:CGColorCreate(space, clear)];
        [description_ setFont:bold];
        [description_ setText:description];

        NSString *label = [source label];
        if (label == nil)
            label = [source type];

        label_ = [[UIRightTextLabel alloc] initWithFrame:CGRectMake(290, 32, 90, 25)];
        [label_ setBackgroundColor:CGColorCreate(space, clear)];
        [label_ setFont:small];
        [label_ setText:label];

        NSString *origin = [source origin];
        if (origin == nil)
            origin = [source distribution];

        origin_ = [[UITextLabel alloc] initWithFrame:CGRectMake(13, 35, 315, 20)];
        [origin_ setBackgroundColor:CGColorCreate(space, clear)];
        [origin_ setFont:small];
        [origin_ setText:origin];

        [self addSubview:description_];
        [self addSubview:label_];
        [self addSubview:origin_];

        CFRelease(small);
        CFRelease(bold);
    } return self;
}

- (void) _setSelected:(float)fraction {
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

    float black[] = {
        interpolate(0.0, 1.0, fraction),
        interpolate(0.0, 1.0, fraction),
        interpolate(0.0, 1.0, fraction),
    1.0};

    float blue[] = {
        interpolate(0.2, 1.0, fraction),
        interpolate(0.2, 1.0, fraction),
        interpolate(1.0, 1.0, fraction),
    1.0};

    float gray[] = {
        interpolate(0.4, 1.0, fraction),
        interpolate(0.4, 1.0, fraction),
        interpolate(0.4, 1.0, fraction),
    1.0};

    [description_ setColor:CGColorCreate(space, black)];
    [label_ setColor:CGColorCreate(space, blue)];
    [origin_ setColor:CGColorCreate(space, gray)];
}

- (void) setSelected:(BOOL)selected {
    [self _setSelected:(selected ? 1.0 : 0.0)];
    [super setSelected:selected];
}

- (void) setSelected:(BOOL)selected withFade:(BOOL)fade {
    if (!fade)
        [self _setSelected:(selected ? 1.0 : 0.0)];
    [super setSelected:selected withFade:fade];
}

- (void) _setSelectionFadeFraction:(float)fraction {
    [self _setSelected:fraction];
    [super _setSelectionFadeFraction:fraction];
}

@end
/* }}} */

/* Sources View {{{ */
@interface SourcesView : UIView {
    UISectionList *list_;
    Database *database_;
    id delegate_;
    NSMutableArray *sources_;
    UIActionSheet *alert_;
}

- (int) numberOfSectionsInSectionList:(UISectionList *)list;
- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section;
- (int) sectionList:(UISectionList *)list rowForSection:(int)section;

- (int) numberOfRowsInTable:(UITable *)table;
- (float) table:(UITable *)table heightForRow:(int)row;
- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col;
- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row;
- (void) tableRowSelected:(NSNotification*)notification;

- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button;

- (void) dealloc;
- (id) initWithFrame:(CGRect)frame database:(Database *)database;
- (void) setDelegate:(id)delegate;
- (void) reloadData;
- (NSString *) leftTitle;
- (NSString *) rightTitle;
@end

@implementation SourcesView

- (int) numberOfSectionsInSectionList:(UISectionList *)list {
    return 1;
}

- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
    return @"sources";
}

- (int) sectionList:(UISectionList *)list rowForSection:(int)section {
    return 0;
}

- (int) numberOfRowsInTable:(UITable *)table {
    return [sources_ count];
}

- (float) table:(UITable *)table heightForRow:(int)row {
    return 64;
}

- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col {
    return [[[SourceCell alloc] initWithSource:[sources_ objectAtIndex:row]] autorelease];
}

- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
    return NO;
}

- (void) tableRowSelected:(NSNotification*)notification {
    UITable *table([list_ table]);
    int row([table selectedRow]);
    if (row == INT_MAX)
        return;

    [table selectRow:-1 byExtendingSelection:NO withFade:YES];
}

- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
    [alert_ dismiss];
    [alert_ release];
    alert_ = nil;
}

- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
    switch (button) {
        case 0:
            alert_ = [[UIActionSheet alloc]
                initWithTitle:@"Unimplemented"
                buttons:[NSArray arrayWithObjects:@"Okay", nil]
                defaultButtonIndex:0
                delegate:self
                context:self
            ];

            [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."];
            [alert_ popupAlertAnimated:YES];
        break;

        case 1:
            [delegate_ update];
        break;
    }
}

- (void) dealloc {
    if (sources_ != nil)
        [sources_ release];
    [list_ release];
    [super dealloc];
}

- (id) initWithFrame:(CGRect)frame database:(Database *)database {
    if ((self = [super initWithFrame:frame]) != nil) {
        database_ = database;
        sources_ = nil;

        CGSize navsize = [UINavigationBar defaultSize];
        CGRect navrect = {{0, 0}, navsize};
        CGRect bounds = [self bounds];

        navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
        [self addSubview:navbar_];

        [navbar_ setBarStyle:1];
        [navbar_ setDelegate:self];

        UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Sources"] autorelease];
        [navbar_ pushNavigationItem:navitem];

        list_ = [[UISectionList alloc] initWithFrame:CGRectMake(
            0, navsize.height, bounds.size.width, bounds.size.height - navsize.height
        )];

        [self addSubview:list_];

        [list_ setDataSource:self];
        [list_ setShouldHideHeaderInShortLists:NO];

        UITableColumn *column = [[UITableColumn alloc]
            initWithTitle:@"Name"
            identifier:@"name"
            width:frame.size.width
        ];

        UITable *table = [list_ table];
        [table setSeparatorStyle:1];
        [table addTableColumn:column];
        [table setDelegate:self];
    } return self;
}

- (void) setDelegate:(id)delegate {
    delegate_ = delegate;
}

- (void) reloadData {
    pkgSourceList list;
    _assert(list.ReadMainList());

    if (sources_ != nil)
        [sources_ release];

    sources_ = [[NSMutableArray arrayWithCapacity:16] retain];
    for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source)
        [sources_ addObject:[[[Source alloc] initWithMetaIndex:*source] autorelease]];

    [list_ reloadData];
}

- (NSString *) leftTitle {
    return @"Refresh All";
}

- (NSString *) rightTitle {
    return @"Edit";
}

@end
/* }}} */
/* Settings View {{{ */
@interface SettingsView : ResetView {
}

- (void) dealloc;
- (void) reloadData;
@end

@implementation SettingsView

- (void) dealloc {
    [super dealloc];
}

- (id) initWithFrame:(CGRect)frame database:(Database *)database {
    if ((self = [super initWithFrame:frame]) != nil) {
        database_ = database;
        sources_ = nil;

        CGSize navsize = [UINavigationBar defaultSize];
        CGRect navrect = {{0, 0}, navsize};
        CGRect bounds = [self bounds];

        navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
        [self addSubview:navbar_];

        [navbar_ setBarStyle:1];
        [navbar_ setDelegate:self];

        UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Settings"] autorelease];
        [navbar_ pushNavigationItem:navitem];
    } return self;
}

- (void) reloadData {
    [self resetView];
}

@end
/* }}} */