summaryrefslogtreecommitdiffstats
path: root/src/kim/agent/mac/SelectIdentityController.m
blob: b89e698da3529ed4190bdbf3150d23c4e92dddbd (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
/*
 * Copyright 2008 Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 * require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#import "SelectIdentityController.h"
#import "IPCClient.h"
#import "KerberosFormatters.h"

@implementation SelectIdentityController

@synthesize associatedClient;

// ---------------------------------------------------------------------------

- (id) init
{
    return [self initWithWindowNibName: @"SelectIdentity"];
}

// ---------------------------------------------------------------------------

- (void) dealloc
{
    [identityOptionsController removeObserver:self forKeyPath:identity_string_keypath];
    [refreshTimer release];
    [identities release];
    [super dealloc];
}

// ---------------------------------------------------------------------------

- (void) awakeFromNib 
{
    NSString *key = nil;
    NSString *message = nil;
    
    [[self window] center];
    [[self window] setLevel:NSScreenSaverWindowLevel];
    
    longTimeFormatter.displaySeconds = NO;
    longTimeFormatter.displayShortFormat = NO;
    
    [identityTableView setDoubleAction:@selector(select:)];
    identities = [[Identities alloc] init];
    [identitiesController setContent:identities];
    refreshTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(timedRefresh:) userInfo:nil repeats:true];    

    [kerberosIconImageView setBadgePath:associatedClient.path];
    
    if ([associatedClient.name isEqualToString:[[NSBundle mainBundle] bundlePath]]) {
        key = @"SelectIdentityRequest";
        message = NSLocalizedStringFromTable(key, @"SelectIdentity", NULL);
    }
    else {
        key = @"SelectIdentityApplicationRequest";
        message = [NSString stringWithFormat:
                   NSLocalizedStringFromTable(key, @"SelectIdentity", NULL),
                   associatedClient.name];
    }
    [headerTextField setStringValue:message];
    
    optionsBoxHeight = [ticketOptionsBox frame].size.height + [ticketOptionsBox frame].origin.y - [ticketOptionsToggleButton frame].origin.y - [ticketOptionsToggleButton frame].size.height;
    [self toggleOptionsVisibility:nil];
    
    [identityOptionsController addObserver:self
                                forKeyPath:identity_string_keypath
                                   options:NSKeyValueObservingOptionNew
                                   context:NULL];
}

- (void)  observeValueForKeyPath:(NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context:(void *) context
{
    if (object == identityOptionsController && [keyPath isEqualToString:identity_string_keypath]) {
        BOOL enabled = [KIMUtilities validateIdentity:[identityOptionsController valueForKeyPath:identity_string_keypath]];
        [identityOptionsController setValue:[NSNumber numberWithBool:enabled] 
                                 forKeyPath:@"content.canClickOK"];
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

// ---------------------------------------------------------------------------

- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
{
    NSRect newFrame = [window frame];
    CGFloat oldHeight = [[identityTableScrollView contentView] frame].size.height;
    CGFloat newHeight = [identityTableView numberOfRows] * 
                        ([identityTableView rowHeight] + [identityTableView intercellSpacing].height);
    CGFloat yDelta = newHeight - oldHeight;
    
    newFrame.origin.y -= yDelta;
    newFrame.size.height += yDelta;
    
    return newFrame;
}

// ---------------------------------------------------------------------------

- (void) setContent: (NSMutableDictionary *) newContent
{
    [self window]; // wake up the nib connections
    [glueController setContent:newContent];
}

// ---------------------------------------------------------------------------

- (IBAction) newIdentity: (id) sender
{
    identityOptionsController.content = [[[glueController valueForKeyPath:@"content.hints.options"] 
                                          mutableCopy] autorelease];

    [self showOptions:@"new"];
}

// ---------------------------------------------------------------------------

- (IBAction) addToFavorites: (id) sender
{
    Identity *anIdentity = [identityArrayController.selectedObjects lastObject];
    identityOptionsController.content = nil;

    anIdentity.favorite = TRUE;
    
    [self saveOptions];
}

// ---------------------------------------------------------------------------

- (IBAction) removeFromFavorites: (id) sender
{
    Identity *anIdentity = [identityArrayController.selectedObjects lastObject];
    identityOptionsController.content = nil;

    anIdentity.favorite = FALSE;
    
    [self saveOptions];
}

// ---------------------------------------------------------------------------

- (IBAction) changePassword: (id) sender
{
    Identity *selectedIdentity = nil;
    
    // ignore double-click on header
    if ([sender respondsToSelector:@selector(clickedRow)] && [sender clickedRow] < 0) {
        return;
    }
    selectedIdentity = [[identityArrayController selectedObjects] lastObject];
    
    [associatedClient didSelectIdentity: selectedIdentity.identity
                                options: [identityOptionsController valueForKeyPath:@"content.options"]
                    wantsChangePassword: YES];
}

// ---------------------------------------------------------------------------

- (IBAction) select: (id) sender
{
    Identity *selectedIdentity = nil;
    
    // ignore double-click on header
    if ([sender respondsToSelector:@selector(clickedRow)] && [sender clickedRow] < 0) {
        return;
    }
    selectedIdentity = [[identityArrayController selectedObjects] lastObject];

    [associatedClient didSelectIdentity: selectedIdentity.identity
                                options: [identityOptionsController content]
                    wantsChangePassword: NO];
}

// ---------------------------------------------------------------------------

- (IBAction) cancel: (id) sender
{
    [associatedClient didCancel];
}

// ---------------------------------------------------------------------------

- (IBAction) editOptions: (id) sender
{
    Identity *anIdentity = [identityArrayController.selectedObjects lastObject];
    anIdentity.favorite = TRUE;
    
    [identityOptionsController setContent:anIdentity.options];
    
    [self showOptions:@"edit"];
}

// ---------------------------------------------------------------------------

- (IBAction) cancelOptions: (id) sender
{
    identityOptionsController.content = nil;
    [NSApp endSheet:ticketOptionsWindow returnCode:NSUserCancelledError];
    
    // dump changed settings
    [identities reload];
}

// ---------------------------------------------------------------------------

- (IBAction) doneOptions: (id) sender
{
//    Identity *anIdentity = identityOptionsController.content;
    
    
    [NSApp endSheet: ticketOptionsWindow];
}

// ---------------------------------------------------------------------------

- (IBAction) checkboxDidChange: (id) sender
{
    if ([[identityOptionsController valueForKeyPath:uses_default_options_keypath] boolValue]) {
        // merge defaults onto current options
        NSMutableDictionary *currentOptions = [identityOptionsController content];
        NSDictionary *defaultOptions = [KIMUtilities dictionaryForKimOptions:NULL];
        NSLog(@"using default ticket options");
        [currentOptions addEntriesFromDictionary:defaultOptions];
        // update the sliders, since their values aren't bound
        [validLifetimeSlider setDoubleValue:[[identityOptionsController valueForKeyPath:valid_lifetime_keypath] doubleValue]];
        [renewableLifetimeSlider setDoubleValue:[[identityOptionsController valueForKeyPath:renewal_lifetime_keypath] doubleValue]];
    }    
}

// ---------------------------------------------------------------------------

- (IBAction) sliderDidChange: (id) sender
{
    NSInteger increment = 0;
    NSInteger newValue = 0;
    NSString *keyPath = nil;
    if ([sender isEqual:validLifetimeSlider]) {
        increment = VALID_LIFETIME_INCREMENT;
        keyPath = valid_lifetime_keypath;
    }
    else if ([sender isEqual:renewableLifetimeSlider]) {
        increment = RENEWABLE_LIFETIME_INCREMENT;
        keyPath = renewal_lifetime_keypath;
    }
    if (increment > 0) {
        newValue = ([sender integerValue] / increment) * increment;
        [identityOptionsController setValue:[NSNumber numberWithInteger:
                                             (newValue < increment) ? increment : newValue] 
                                 forKeyPath:keyPath];
    }
}

// ---------------------------------------------------------------------------

- (void) showOptions: (NSString *) contextInfo
{
    Identity *anIdentity = [[identityArrayController selectedObjects] lastObject];
    BOOL isIdentityNameNotEditable = (!anIdentity.hasCCache || [contextInfo isEqualToString:@"new"]);
    NSString *identityString = ([contextInfo isEqualToString:@"new"]) ? @"" : anIdentity.identity;
    
    [identityOptionsController setValue:identityString
                             forKeyPath:identity_string_keypath];    
    [identityOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities minValidLifetime]]
                           forKeyPath:min_valid_keypath];
    [identityOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities maxValidLifetime]]
                           forKeyPath:max_valid_keypath];
    [identityOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities minRenewableLifetime]]
                           forKeyPath:min_renewable_keypath];
    [identityOptionsController setValue:[NSNumber numberWithInteger:[KIMUtilities maxRenewableLifetime]]
                           forKeyPath:max_renewable_keypath];
    [identityOptionsController setValue:[NSNumber numberWithBool:!isIdentityNameNotEditable]
                             forKeyPath:@"content.hasCCache"];
    
    [validLifetimeSlider setIntegerValue:
     [[identityOptionsController valueForKeyPath:valid_lifetime_keypath] integerValue]];
    [renewableLifetimeSlider setIntegerValue:
     [[identityOptionsController valueForKeyPath:renewal_lifetime_keypath] integerValue]];
    [self sliderDidChange:validLifetimeSlider];
    [self sliderDidChange:renewableLifetimeSlider];
    
    [NSApp beginSheet: ticketOptionsWindow 
       modalForWindow: [self window] 
        modalDelegate: self 
       didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
          contextInfo: contextInfo];
}

// ---------------------------------------------------------------------------

- (void) didEndSheet: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo
{
    kim_error err = KIM_NO_ERROR;
    if (returnCode != NSUserCancelledError) {
        if ([(NSString *)contextInfo isEqualToString:@"new"]) {
            Identity *newIdentity = [[Identity alloc] 
                                     initWithIdentity:[identityOptionsController valueForKeyPath:identity_string_keypath] 
                                     options:identityOptionsController.content];
            newIdentity.favorite = YES;

            err = [identities addIdentity:newIdentity];

            if (err) {
                NSLog(@"%s received error %@ trying to add identity %@", _cmd, [KIMUtilities stringForLastKIMError:err], [newIdentity description]);
            }
            [newIdentity release];
            [self saveOptions];

        }
        else if ([(NSString *)contextInfo isEqualToString:@"edit"]) {
            Identity *editedIdentity = [[identityArrayController selectedObjects] lastObject];
            editedIdentity.favorite = YES;
            editedIdentity.identity = [identityOptionsController valueForKeyPath:identity_string_keypath];
            editedIdentity.options = identityOptionsController.content;

            [self saveOptions];
            
        }
    } else {
        [identityOptionsController setContent:nil];
    }
    [sheet orderOut:self];

}

// ---------------------------------------------------------------------------

- (void) saveOptions
{
    // attempt to preserve the selection
    Identity *anIdentity = [[identityArrayController selectedObjects] lastObject];
    NSUInteger a, b, c;
    
    a = [identityArrayController.content indexOfObject: anIdentity];
    b = NSNotFound;
    
    [identities synchronizePreferences];
    
    /*
     * select same object as before if it's still in the array
     * if not, select same index as before or end of array, whichever is less
     */
    
    b = [identityArrayController.content indexOfObject:anIdentity];
    c = [identityArrayController.content count] - 1;


    [identityArrayController setSelectionIndex: (b == NSNotFound) ? (a > c) ? c : a : b];
}

// ---------------------------------------------------------------------------

- (IBAction) toggleOptionsVisibility: (id) sender
{
    NSRect newFrame = [NSWindow contentRectForFrameRect:[ticketOptionsWindow frame] styleMask:[ticketOptionsWindow styleMask]];
    CGFloat newHeight;
    
    if ([ticketOptionsBox isHidden]) {
        newHeight = newFrame.size.height + optionsBoxHeight;
        newFrame.origin.y += newFrame.size.height;
        newFrame.origin.y -= newHeight;
        newFrame.size.height = newHeight;
        newFrame = [NSWindow frameRectForContentRect:newFrame styleMask:[ticketOptionsWindow styleMask]];

        [ticketOptionsWindow setFrame:newFrame display:YES animate:YES];
        [ticketOptionsBox setHidden:NO];
        [sender setTitle:NSLocalizedStringFromTable(@"SelectIdentityHideOptions", @"SelectIdentity", NULL)];
    }
    else {
        newHeight = newFrame.size.height - optionsBoxHeight;
        newFrame.origin.y += newFrame.size.height;
        newFrame.origin.y -= newHeight;
        newFrame.size.height = newHeight;
        newFrame = [NSWindow frameRectForContentRect:newFrame styleMask:[ticketOptionsWindow styleMask]];
        
        [ticketOptionsBox setHidden:YES];
        [ticketOptionsWindow setFrame:newFrame display:YES animate:YES];
        [sender setTitle:NSLocalizedStringFromTable(@"SelectIdentityShowOptions", @"SelectIdentity", NULL)];
    }
}

// ---------------------------------------------------------------------------

- (void) timedRefresh:(NSTimer *)timer
{
    // refetch data to update expiration times
    [identityArrayController rearrangeObjects];
}

@end