From: Justin Anderson Date: Tue, 23 Sep 2008 22:20:04 +0000 (+0000) Subject: KIM Identity selection dialog work. Updated to match changes to KIM API X-Git-Tag: krb5-1.7-alpha1~423 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ca50f2b4f47afccb424c470344c78da0c04d00b7;p=krb5.git KIM Identity selection dialog work. Updated to match changes to KIM API ticket: 6055 git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20748 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/kim/agent/mac/Identities.h b/src/kim/agent/mac/Identities.h index 556d0b075..82819f8ee 100644 --- a/src/kim/agent/mac/Identities.h +++ b/src/kim/agent/mac/Identities.h @@ -22,6 +22,8 @@ * or implied warranty. */ +#import + @interface Identity : NSObject { kim_identity kimIdentity; int state; @@ -29,6 +31,8 @@ int favorite; } +@property(readonly) NSString *principal; +@property(readonly) NSString *timeRemaining; @property int state; @property cc_time_t expirationTime; @property(readonly) int favorite; @@ -47,7 +51,7 @@ NSConnection *threadConnection; } -@property(readonly) NSArray *identities; +@property(readonly, copy) NSArray *identities; - (int) update; diff --git a/src/kim/agent/mac/Identities.m b/src/kim/agent/mac/Identities.m index f950bc13e..211d980ee 100644 --- a/src/kim/agent/mac/Identities.m +++ b/src/kim/agent/mac/Identities.m @@ -23,6 +23,7 @@ */ #import "Identities.h" +#import @implementation Identity @@ -67,6 +68,80 @@ return (!err && kim_comparison_is_equal_to (comparison)); } +// --------------------------------------------------------------------------- + +- (NSString *)principal +{ + kim_error err = KIM_NO_ERROR; + kim_string display_string = NULL; + NSString *result = nil; + + err = kim_identity_get_display_string(kimIdentity, &display_string); + + if (!err) { + result = [NSString stringWithCString:display_string encoding:NSUTF8StringEncoding]; + } + else { + result = @"-"; + } + return result; +} + +// --------------------------------------------------------------------------- + +- (NSDate *)expirationDate +{ + return [NSDate dateWithTimeIntervalSince1970:expirationTime]; +} + +// --------------------------------------------------------------------------- + +- (NSString *)timeRemaining +{ + NSString *result = nil; + + if (expirationTime > 0) { + time_t now = time(NULL); + time_t lifetime = expirationTime - now; + time_t seconds = (lifetime % 60); + time_t minutes = (lifetime / 60 % 60); + time_t hours = (lifetime / 3600 % 24); + time_t days = (lifetime / 86400); + + if (seconds > 0) { seconds = 0; minutes++; } + if (minutes > 59) { minutes = 0; hours++; } + if (hours > 23) { hours = 0; days++; } + + result = [NSString stringWithFormat:@"%02ld:%02ld", hours, minutes]; + } else { + result = @"Expired"; + } + + + NSLog(@"timeRemaining = %@ (expirationTime == %ld)", result, expirationTime); + return result; +} + +- (NSString *)description +{ + NSString *result = nil; + kim_error err = KIM_NO_ERROR; + kim_string display_name = NULL; + + err = kim_identity_get_display_string(kimIdentity, &display_name); + + if (!err) { + result = [NSString stringWithCString:display_name encoding:NSUTF8StringEncoding]; + } + return result; +} + +@end + +@interface Identities () + +@property(readwrite, copy) NSArray *identities; + @end @implementation Identities @@ -131,14 +206,15 @@ } if (!err) { - kim_favorite_identities kimFavoriteIdentities = NULL; + kim_preferences kimPreferences = NULL; + kim_options kimOptions = NULL; kim_count i; kim_count count = 0; - err = kim_favorite_identities_create (&kimFavoriteIdentities); + err = kim_preferences_create(&kimPreferences); if (!err) { - err = kim_favorite_identities_get_number_of_identities (kimFavoriteIdentities, + err = kim_preferences_get_number_of_favorite_identities(kimPreferences, &count); } @@ -146,8 +222,10 @@ kim_identity kimIdentity = NULL; Identity *identity = NULL; - err = kim_favorite_identities_get_identity_at_index (kimFavoriteIdentities, - i, &kimIdentity); + err = kim_preferences_get_favorite_identity_at_index(kimPreferences, + i, + &kimIdentity, + &kimOptions); if (!err) { identity = [[[Identity alloc] initWithFavoriteIdentity: kimIdentity] autorelease]; @@ -162,7 +240,7 @@ kim_identity_free (&kimIdentity); } - kim_favorite_identities_free (&kimFavoriteIdentities); + kim_preferences_free(&kimPreferences); } if (!err) { @@ -270,8 +348,8 @@ } if (!err) { - [identity setState: state]; - [identity setExpirationTime: expirationTime]; + identity.state = state; + identity.expirationTime = expirationTime; } } @@ -285,9 +363,8 @@ } if (!err) { - if (identities) { [identities release]; } - - identities = [[NSArray alloc] initWithArray: newIdentities]; + /* Use @property setter to trigger KVO notifications */ + self.identities = newIdentities; if (!identities) { err = ENOMEM; } } diff --git a/src/kim/agent/mac/KerberosAgentController.m b/src/kim/agent/mac/KerberosAgentController.m index 9313a8c14..20b74015a 100644 --- a/src/kim/agent/mac/KerberosAgentController.m +++ b/src/kim/agent/mac/KerberosAgentController.m @@ -34,7 +34,9 @@ { SelectIdentityController *controller = [[SelectIdentityController alloc] init]; int result = [controller runWindow]; - + if (result != 0) { + NSLog(@"SelectIdentityController -runWindow result was %d", result); + } } diff --git a/src/kim/agent/mac/KerberosFormatters.h b/src/kim/agent/mac/KerberosFormatters.h new file mode 100644 index 000000000..2d5336fce --- /dev/null +++ b/src/kim/agent/mac/KerberosFormatters.h @@ -0,0 +1,40 @@ +/* + * 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 + + +@interface KerberosTimeFormatter : NSFormatter { + +} + +- (NSString *)stringForObjectValue:(id)anObject; + +- (BOOL)getObjectValue:(id *)anObject + forString:(NSString *)string + errorDescription:(NSString **)error; + +- (NSAttributedString *)attributedStringForObjectValue:(id)anObject + withDefaultAttributes:(NSDictionary *)attributes; +@end diff --git a/src/kim/agent/mac/KerberosFormatters.m b/src/kim/agent/mac/KerberosFormatters.m new file mode 100644 index 000000000..e1675bb70 --- /dev/null +++ b/src/kim/agent/mac/KerberosFormatters.m @@ -0,0 +1,110 @@ +/* + * 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 "KerberosFormatters.h" + +#define EXPIRED_STRING @"Expired" + +@implementation KerberosTimeFormatter + +/* + * For display of Kerberos expiration times. + * Converts an NSDate into an NSString like "09:53" for 9 hours and 53 minutes + * in the future. Returns @"Expired" if expiration date is before now. + */ +- (NSString *)stringForObjectValue:(id)anObject +{ + NSString *result = nil; + if (anObject == nil || ![anObject respondsToSelector:@selector(timeIntervalSince1970)]) { + result = [NSString stringWithFormat:@"%s given invalid object %@", + _cmd, NSStringFromClass([anObject class])]; + } + else { + time_t lifetime = [(NSDate *)anObject timeIntervalSince1970] - time(NULL); + + if (lifetime > 0) { + time_t seconds = (lifetime % 60); + time_t minutes = (lifetime / 60 % 60); + time_t hours = (lifetime / 3600 % 24); + time_t days = (lifetime / 86400); + + if (seconds > 0) { seconds = 0; minutes++; } + if (minutes > 59) { minutes = 0; hours++; } + if (hours > 23) { hours = 0; days++; } + + result = [NSString stringWithFormat:@"%02ld:%02ld", hours, minutes]; + } else { + result = EXPIRED_STRING; + } + } + + return result; +} + +/* + * Converts NSStrings like @"09:53" into NSDate representation of that point + * in the future. If string is @"Expired", NSDate is set to 1970. + */ + +- (BOOL)getObjectValue:(id *)anObject + forString:(NSString *)string + errorDescription:(NSString **)error +{ + NSArray *tokens = [string componentsSeparatedByString:@":"]; + *anObject = nil; + + if ([tokens count] == 2) { + NSInteger hours = [[tokens objectAtIndex:0] longValue]; + NSInteger minutes = [[tokens objectAtIndex:1] longValue]; + *anObject = [NSDate dateWithTimeIntervalSince1970:(hours * 60 * 60) + (minutes * 60)]; + } else if ([string isEqualToString:EXPIRED_STRING]) { + *anObject = [NSDate dateWithTimeIntervalSince1970:0]; + } + + if (*anObject == nil) { + return false; + } + return true; +} + +- (NSAttributedString *)attributedStringForObjectValue:(id)anObject + withDefaultAttributes:(NSDictionary *)attributes +{ + NSAttributedString *resultString = nil; + NSString *plainString = [self stringForObjectValue:anObject]; + NSMutableDictionary *newAttributes = [attributes mutableCopy]; + + if ([plainString isEqualToString:EXPIRED_STRING]) { + [newAttributes setObject:[NSColor redColor] + forKey:NSForegroundColorAttributeName]; + [newAttributes setObject: [NSNumber numberWithFloat: 0.3] + forKey: NSObliquenessAttributeName]; + } + + resultString = [[NSAttributedString alloc] initWithString:plainString attributes:newAttributes]; + [newAttributes release]; + + return [resultString autorelease]; +} +@end diff --git a/src/kim/agent/mac/SelectIdentityController.h b/src/kim/agent/mac/SelectIdentityController.h index 7fd4025e8..741f897f6 100644 --- a/src/kim/agent/mac/SelectIdentityController.h +++ b/src/kim/agent/mac/SelectIdentityController.h @@ -24,10 +24,12 @@ #import #import "BadgedImageView.h" - +#import "Identities.h" @interface SelectIdentityController : NSWindowController { + IBOutlet NSArrayController *identityArrayController; + IBOutlet BadgedImageView *kerberosIconImageView; IBOutlet NSTextField *headerTextField; IBOutlet NSTextField *explanationTextField; @@ -39,6 +41,9 @@ IBOutlet NSButton *removeIdentityButton; IBOutlet NSButton *selectIdentityButton; IBOutlet NSButton *cancelButton; + + Identities *identities; + NSTimer *refreshTimer; } - (IBAction) add: (id) sender; @@ -49,4 +54,6 @@ - (int) runWindow; +- (void) timedRefresh:(NSTimer *)timer; + @end diff --git a/src/kim/agent/mac/SelectIdentityController.m b/src/kim/agent/mac/SelectIdentityController.m index ac3dc0726..e7453a906 100644 --- a/src/kim/agent/mac/SelectIdentityController.m +++ b/src/kim/agent/mac/SelectIdentityController.m @@ -33,7 +33,12 @@ { if ((self = [super initWithWindowNibName: windowNibName])) { NSLog (@"SelectIdentityController initializing"); - + identities = [[Identities alloc] init]; + [identities addObserver:self + forKeyPath:@"identities" + options:NSKeyValueObservingOptionNew + context:@"SelectIdentityController"]; + refreshTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(timedRefresh:) userInfo:nil repeats:true]; } return self; @@ -50,6 +55,9 @@ - (void) dealloc { + [refreshTimer release]; + [identities removeObserver:self forKeyPath:@"identities"]; + [identities release]; [super dealloc]; } @@ -64,31 +72,37 @@ - (void) windowDidLoad { - [explanationTextField setStringValue: @"Some explanation text"]; + + [explanationTextField setStringValue: @"Some explanation text"]; + } // --------------------------------------------------------------------------- - (IBAction) add: (id) sender { + NSLog(@"Add identity"); } // --------------------------------------------------------------------------- - (IBAction) remove: (id) sender { + NSLog(@"Remove identity"); } // --------------------------------------------------------------------------- - (IBAction) select: (id) sender { + NSLog(@"Select identity: %@", identityArrayController.selectedObjects.description); } // --------------------------------------------------------------------------- - (IBAction) cancel: (id) sender { + NSLog(@"Cancel identity selection"); } // --------------------------------------------------------------------------- @@ -108,5 +122,23 @@ return 0; } +// --------------------------------------------------------------------------- + +- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if ([(NSString *)context isEqualToString:@"SelectIdentityController"]) { + NSLog(@"========== identities array changed =========="); + identityArrayController.content = [[identities.identities mutableCopy] autorelease]; + } + else { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + } +} + +- (void) timedRefresh:(NSTimer *)timer +{ + [identityArrayController rearrangeObjects]; +} + @end diff --git a/src/kim/agent/mac/resources/English.lproj/SelectIdentity.xib b/src/kim/agent/mac/resources/English.lproj/SelectIdentity.xib index 938c63362..8e912b203 100644 --- a/src/kim/agent/mac/resources/English.lproj/SelectIdentity.xib +++ b/src/kim/agent/mac/resources/English.lproj/SelectIdentity.xib @@ -2,9 +2,9 @@ 1050 - 9E17 - 670 - 949.33 + 9F33 + 672 + 949.34 352.00 YES @@ -132,6 +132,7 @@ YES + principal 3.429741e+02 1.499741e+02 1.000000e+03 @@ -166,6 +167,7 @@ + timeRemaining 1.003135e+02 1.000000e+02 1.500000e+02 @@ -187,7 +189,7 @@ 1140981312 - 71435264 + -2076048384 Text Cell @@ -323,7 +325,7 @@ 292 - {{42, 30}, {22, 22}} + {{43, 30}, {22, 22}} YES @@ -391,6 +393,26 @@ {419, 320} {600, 622} + + + YES + timeRemaining + principal + isFavorite + description + expirationDate + + YES + + YES + YES + YES + YES + YES + + + KerberosTimeFormatter + @@ -483,22 +505,6 @@ 300169 - - - remove: - - - - 300170 - - - - add: - - - - 300171 - identityTableView @@ -523,6 +529,139 @@ 300181 + + + identityArrayController + + + + 300184 + + + + content: arrangedObjects + + + + + + content: arrangedObjects + content + arrangedObjects + 2 + + + 300185 + + + + value: arrangedObjects.principal + + + + + + value: arrangedObjects.principal + value + arrangedObjects.principal + 2 + + + 300187 + + + + remove: + + + + 300189 + + + + insert: + + + + 300191 + + + + formatter + + + + 300197 + + + + value: arrangedObjects.expirationDate + + + + + + value: arrangedObjects.expirationDate + value + arrangedObjects.expirationDate + + NSConditionallySetsEditable + + + 2 + + + 300198 + + + + selectionIndexes: selectionIndexes + + + + + + selectionIndexes: selectionIndexes + selectionIndexes + selectionIndexes + + 2 + + + 300199 + + + + enabled: canRemove + + + + + + enabled: canRemove + enabled + canRemove + 2 + + + 300200 + + + + enabled: canInsert + + + + + + enabled: canInsert + enabled + canInsert + 2 + + + 300201 + @@ -736,6 +875,17 @@ + + 300183 + + + Identity Array Controller + + + 300196 + + + @@ -766,6 +916,8 @@ 300154.IBPluginDependency 300155.IBPluginDependency 300156.IBPluginDependency + 300183.IBPluginDependency + 300196.IBPluginDependency 5.IBEditorWindowLastContentRect 5.IBPluginDependency 5.IBWindowTemplateEditedContentRect @@ -809,9 +961,11 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{606, 810}, {491, 315}} com.apple.InterfaceBuilder.CocoaPlugin - {{606, 810}, {491, 315}} + com.apple.InterfaceBuilder.CocoaPlugin + {{606, 541}, {491, 315}} + com.apple.InterfaceBuilder.CocoaPlugin + {{606, 541}, {491, 315}} {{503, 256}, {419, 465}} @@ -847,7 +1001,7 @@ - 300182 + 300201 @@ -868,6 +1022,14 @@ + + KerberosTimeFormatter + NSFormatter + + IBProjectSource + ../Sources/kim/agent/mac/KerberosFormatters.h + + SelectIdentityController NSWindowController @@ -896,6 +1058,7 @@ cancelButton explanationTextField headerTextField + identityArrayController identityTableColumn identityTableView kerberosIconImageView @@ -909,6 +1072,7 @@ NSButton NSTextField NSTextField + NSArrayController NSTableColumn NSTableView BadgedImageView @@ -922,6 +1086,14 @@ ../Sources/kim/agent/mac/SelectIdentityController.h + + SelectIdentityController + NSWindowController + + IBUserSource + + + 0