5 // Created by Claudio Bisegni on 10/05/07.
6 // Copyright (c) 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
9 #import "AFSCommanderPref.h"
10 #import "IpConfiguratorCommander.h"
11 #import "TokenCredentialController.h"
12 #import "InfoController.h"
14 #import "PListManager.h"
15 #import "DialogUtility.h"
16 #import "NSString+search.h"
17 #include <sys/param.h>
20 #include <sys/types.h>
21 #include <sys/fcntl.h>
22 #include <sys/errno.h>
26 #import <CoreServices/CoreServices.h>
29 #define ADD_CELL_CONTROL_TAG 1
30 #define REMOVE_CELL_CONTROL_TAG 2
32 #define TABLE_TOKENS_LIST 1
33 #define TABLE_CELL_LIST 2
34 #define TABLE_LINK_LIST 3
37 #define TAB_CELL_SERV_DB 2
42 #define CELLSRVDB_TABLE_USR_DFLT_CHECK_COLUMN 0
43 #define CELLSRVDB_TABLE_DFLT_CHECK_COLUMN 1
44 #define CELLSRVDB_TABLE_NAME_COLUMN 2
45 #define CELLSRVDB_TABLE_DESCRIPTION_COLUMN 3
48 #define TABLE_COLUMN_LINK_NAME 0
49 #define TABLE_COLUMN_LINK_PATH 1
52 @implementation AFSCommanderPref
54 // -------------------------------------------------------------------------------
56 // -------------------------------------------------------------------------------
57 - (id)initWithBundle:(NSBundle *)bundle
59 if ( ( self = [super initWithBundle:bundle] ) != nil ) {
60 //appID = kAfsCommanderID;
66 // -------------------------------------------------------------------------------
68 // -------------------------------------------------------------------------------
69 - (NSView *) mainView {
70 if (prefStartUp == 1){
73 if (Gestalt(gestaltSystemVersionMajor, &osxMJVers) == noErr && Gestalt(gestaltSystemVersionMinor, &osxMnVers) == noErr) {
74 if (osxMJVers == 10 && osxMnVers>= 5) {
75 [afsCommanderView setFrameSize:NSMakeSize(668, [afsCommanderView frame].size.height)];
81 return afsCommanderView;
84 // -------------------------------------------------------------------------------
86 // -------------------------------------------------------------------------------
87 - (void) mainViewDidLoad
90 [((NSTableView*)cellList) setDelegate:self];
91 [((NSTableView*)cellList) setTarget:self];
92 [((NSTableView*)cellList) setDoubleAction:@selector(tableDoubleAction:)];
97 // -------------------------------------------------------------------------------
99 // -------------------------------------------------------------------------------
102 //try to install the launchd file for backgrounder
103 //Remove launchd ctrl file
105 [PListManager installBackgrounderLaunchdFile:YES
106 resourcePath:[[self bundle] resourcePath]];
108 @catch (NSException * e) {
109 NSDictionary *excecptDic = [e userInfo];
110 NSNumber *keyNum = [excecptDic objectForKey:@"agent_folder_error"];
111 if(keyNum && [keyNum boolValue]) {
112 // the dir HOME_LAUNCHD_AGENT_FOLDER (PListManager.h) must be created
113 NSBeginAlertSheet([[NSString stringWithString:kDoYouWantCreateTheDirectory] stringByAppendingString:HOME_LAUNCHD_AGENT_FOLDER],
114 @"Create", @"Cancel", nil,
115 [[self mainView] window], self, @selector(credentialAtLoginTimeEventCreationLaunchAgentDir:returnCode:contextInfo:), NULL,
124 // Set Developer info
125 [textFieldDevInfoLabel setStringValue:kDevelopInfo];
127 tokensLock = [[NSLock alloc] init];
129 //Initialization cellservdb and token list
130 filteredCellDB = nil;
133 [self readPreferenceFile];
135 // alloc the afs property mananger
136 afsProperty = [[AFSPropertyManager alloc] init];
138 // register preference pane to detect menuextra killed by user
139 [[NSDistributedNotificationCenter defaultCenter] addObserver:self
140 selector:@selector(refreshTokensNotify:)
142 object:kMExtraTokenOperation];
144 [[NSDistributedNotificationCenter defaultCenter] addObserver:self
145 selector:@selector(refreshGui:)
147 object:kMenuExtraEventOccured];
149 //Register for mount/unmount afs volume
150 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
151 selector:@selector(afsVolumeMountChange:)
152 name:NSWorkspaceDidMountNotification object:nil];
154 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
155 selector:@selector(afsVolumeMountChange:)
156 name:NSWorkspaceDidUnmountNotification object:nil];
158 // set self as table data source
159 [cellList setDataSource:self];
160 [tokensTable setDataSource:self];
161 //[tableViewLink setDataSource:self];
162 //check the afs state
165 // let show the configuration after prefpane is open
166 [self refreshConfiguration:nil];
168 // refresh the token list
169 //[self refreshTokens:nil];
171 //refresh table to reflect the NSSearchField contained text
172 [self searchCellTextEvent:nil];
175 // -------------------------------------------------------------------------------
176 // credentialAtLoginTimeEventCreationLaunchAgentDir:
177 // -------------------------------------------------------------------------------
178 - (void) credentialAtLoginTimeEventCreationLaunchAgentDir:(NSWindow*)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo {
180 switch (returnCode) {
182 if([[NSFileManager defaultManager] createDirectoryAtPath:[HOME_LAUNCHD_AGENT_FOLDER stringByExpandingTildeInPath]
183 withIntermediateDirectories:NO
188 [PListManager installBackgrounderLaunchdFile:YES
189 resourcePath:[[self bundle] resourcePath]];
190 [self showMessage:kDirectoryCreated];
192 [self showMessage:kErrorCreatingDirectory];
201 // -------------------------------------------------------------------------------
203 // -------------------------------------------------------------------------------
206 // remove self as datasource
207 [((NSTableView*)cellList) setDataSource:nil];
208 [((NSTableView*)tokensTable) setDataSource:nil];
210 //release the afs property manager
211 if(afsProperty) [afsProperty release];
212 //release tokens list
213 if(tokenList) [tokenList release];
214 //Remove the cell temp array
215 if(filteredCellDB) [filteredCellDB release];
217 [self writePreferenceFile];
219 // unregister preference pane to detect menuextra killed by user
220 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self
222 object:kMExtraClosedNotification];
223 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self
225 object:kMExtraTokenOperation];
226 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self
228 object:kMenuExtraEventOccured];
229 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self
230 name:NSWorkspaceDidMountNotification object:nil];
231 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self
232 name:NSWorkspaceDidUnmountNotification object:nil];
235 [tokensLock release];
239 // -------------------------------------------------------------------------------
241 // -------------------------------------------------------------------------------
243 //start the time for check tokens validity
244 if(timerForCheckTokensList) return;
245 timerForCheckTokensList = [NSTimer scheduledTimerWithTimeInterval:TOKENS_REFRESH_TIME_IN_SEC
247 selector:@selector(refreshTokens:)
250 [timerForCheckTokensList fire];
253 // -------------------------------------------------------------------------------
255 // -------------------------------------------------------------------------------
257 if(!timerForCheckTokensList) return;
258 [timerForCheckTokensList invalidate];
259 timerForCheckTokensList = nil;
263 // -------------------------------------------------------------------------------
264 // readPreferenceFile:
265 // -------------------------------------------------------------------------------
266 - (void) readPreferenceFile
268 // read the preference for aklog use
269 NSNumber *useAklogPrefValue = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_USE_AKLOG, (CFStringRef)kAfsCommanderID,
270 kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
271 NSNumber *aklogTokenAtLogin = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_AKLOG_TOKEN_AT_LOGIN, (CFStringRef)kAfsCommanderID,
272 kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
273 [useAklogCheck setState:[useAklogPrefValue intValue]];
274 [aklogCredentialAtLoginTime setEnabled:useAklogPrefValue && [useAklogPrefValue boolValue]];
275 [aklogCredentialAtLoginTime setState:aklogTokenAtLogin && [aklogTokenAtLogin boolValue]];
277 //check krb5 at login time
278 [installKRB5AuthAtLoginButton setState:[PListManager checkKrb5AtLoginTimeLaunchdEnable]];
280 //check for AFS enable at startup
281 NSNumber *afsEnableStartupTime = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_START_AFS_AT_STARTUP,
282 (CFStringRef)kAfsCommanderID, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
283 if(afsEnableStartupTime)
284 startAFSAtLogin = [afsEnableStartupTime boolValue];
286 startAFSAtLogin = false;
287 //set the check button state
288 [checkButtonAfsAtBootTime setState:startAFSAtLogin];
290 NSNumber *showStatusMenu = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_SHOW_STATUS_MENU, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
291 [(NSButton*)afsMenucheckBox setState: [showStatusMenu boolValue]];
294 [backgrounderActivationCheck setState:[PListManager launchdJobState:BACKGROUNDER_P_FILE]];
296 //link enabled status
297 NSNumber *linkEnabledStatus = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_USE_LINK, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
298 [checkEnableLink setState:[linkEnabledStatus boolValue]];
301 NSData *prefData = (NSData*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_LINK_CONFIGURATION, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
302 linkConfiguration = (NSMutableDictionary*)[NSPropertyListSerialization propertyListFromData:prefData
303 mutabilityOption:NSPropertyListMutableContainers
305 errorDescription:nil];
309 // -------------------------------------------------------------------------------
311 // -------------------------------------------------------------------------------
312 - (void) writePreferenceFile
314 //Set the preference for afs path
315 /*CFPreferencesSetValue((CFStringRef)PREFERENCE_AFS_SYS_PAT,
316 (CFStringRef)[((NSTextField*) installationPathTextField ) stringValue],
317 (CFStringRef)kAfsCommanderID, kCFPreferencesAnyUser, kCFPreferencesAnyHost);*/
319 //Set the preference for aklog use
320 CFPreferencesSetValue((CFStringRef)PREFERENCE_USE_AKLOG,
321 (CFNumberRef)[NSNumber numberWithInt:[useAklogCheck state]],
322 (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
324 //set AFS enable state at startup
325 CFPreferencesSetValue((CFStringRef)PREFERENCE_START_AFS_AT_STARTUP,
326 (CFNumberRef)[NSNumber numberWithBool:startAFSAtLogin],
327 (CFStringRef)kAfsCommanderID, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
330 CFPreferencesSetValue((CFStringRef)PREFERENCE_AKLOG_TOKEN_AT_LOGIN,
331 (CFNumberRef)[NSNumber numberWithBool:[aklogCredentialAtLoginTime state]],
332 (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
335 CFPreferencesSetValue((CFStringRef)PREFERENCE_SHOW_STATUS_MENU,
336 (CFNumberRef)[NSNumber numberWithBool:[afsMenucheckBox state]],
337 (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
339 //write preference for link
340 CFPreferencesSetValue((CFStringRef)PREFERENCE_USE_LINK,
341 (CFNumberRef)[NSNumber numberWithBool:[checkEnableLink state]],
342 (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
344 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
345 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
346 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAFSMenuExtraID object:kPrefChangeNotification];
349 // -------------------------------------------------------------------------------
350 // saveConfiguration:
351 // -------------------------------------------------------------------------------
352 - (IBAction) saveConfiguration:(id) sender
356 //[afsProperty setCellName:[cellNameTextEdit stringValue]];
357 [afsProperty setCellName:[afsProperty getDefaultCellName]];
359 //save configurations
360 [afsProperty saveConfigurationFiles:YES];
363 //Reload all configuration
364 [self refreshConfiguration:nil];
366 //refresh table to reflect the NSSearchField contained text
367 [self searchCellTextEvent:nil];
369 //Show dialog for notifity al saving process ar gone ell
370 [self showMessage:kConfigurationSaved];
371 }@catch(NSException *e){
372 [self showMessage:[e reason]];
374 [((NSTableView*)cellList) reloadData];
379 // -------------------------------------------------------------------------------
380 // saveCacheManagerParam:
381 // -------------------------------------------------------------------------------
382 - (IBAction) saveCacheManagerParam:(id) sender
385 //Update the value form view to afs property manager class
386 [self updateCacheParamFromView];
387 [afsProperty saveCacheConfigurationFiles:YES];
388 [self showMessage:kSavedCacheConfiguration];
389 }@catch(NSException *e){
390 [self showMessage:[e reason]];
392 [((NSTableView*)cellList) reloadData];
396 // -------------------------------------------------------------------------------
397 // refreshConfiguration:
398 // -------------------------------------------------------------------------------
399 - (IBAction) refreshConfiguration:(id) sender
401 NSString *afsBasePath = PREFERENCE_AFS_SYS_PAT_STATIC;
404 [afsProperty setPath:afsBasePath];
406 // load configuration
407 [afsProperty loadConfiguration];
409 //set the afs version label
410 [afsVersionLabel setStringValue:[afsProperty getAfsVersion]];
412 //set the current default cell
413 [afsDefaultCellLabel setStringValue:[afsProperty getDefaultCellName]];
416 [self fillCacheParamView];
418 //Filter the cellServDb and allocate filtered array
419 [self filterCellServDB:nil];
421 }@catch(NSException *e){
422 [self showMessage:[e reason]];
424 [((NSTableView*)cellList) reloadData];
428 // -------------------------------------------------------------------------------
429 // fillCacheParamView:
430 // -------------------------------------------------------------------------------
431 -(void) fillCacheParamView
433 [dynRoot setState:[afsProperty dynRoot]?NSOnState:NSOffState];
434 [afsDB setState:[afsProperty afsDB]?NSOnState:NSOffState];
435 [statCacheEntry setIntValue:[afsProperty statCacheEntry]];
436 [dCacheDim setIntValue:[afsProperty dCacheDim]];
437 [cacheDimension setIntValue:[afsProperty cacheDimension]];
438 [daemonNumber setIntValue:[afsProperty daemonNumber]];
439 [afsRootMountPoint setStringValue:[afsProperty afsRootMountPoint]];
440 [nVolEntry setIntValue:[afsProperty nVolEntry]];
442 //new version property
443 //[verbose setEnabled:[afsProperty useAfsdConfConfigFile]];
444 [verbose setState:[afsProperty verbose]?NSOnState:NSOffState];
448 // -------------------------------------------------------------------------------
449 // updateCacheParamFromView:
450 // -------------------------------------------------------------------------------
451 -(void) updateCacheParamFromView
453 NSString *tmpAfsPath = [afsRootMountPoint stringValue];
454 if(!tmpAfsPath || ([tmpAfsPath length] == 0) || ([tmpAfsPath characterAtIndex:0] != '/'))
455 @throw [NSException exceptionWithName:@"updateCacheParamFromView"
456 reason:kBadAfsRootMountPoint
460 [afsProperty setDynRoot:[dynRoot state]==NSOnState];
461 [afsProperty setAfsDB:[afsDB state]==NSOnState];
462 [afsProperty setStatCacheEntry:[statCacheEntry intValue]];
463 [afsProperty setDCacheDim:[dCacheDim intValue]];
464 [afsProperty setCacheDimension:[cacheDimension intValue]];
465 [afsProperty setDaemonNumber:[daemonNumber intValue]];
466 [afsProperty setAfsRootMountPoint:tmpAfsPath];
467 [afsProperty setNVolEntry:[nVolEntry intValue]];
468 [afsProperty setVerbose:[verbose state]==NSOnState];
472 // -------------------------------------------------------------------------------
474 // -------------------------------------------------------------------------------
475 - (IBAction) showCellIP:(id) sender
477 int rowSelected = [((NSTableView *) cellList) selectedRow];
478 [self modifyCellByIDX:rowSelected];
481 // -------------------------------------------------------------------------------
483 // -------------------------------------------------------------------------------
484 -(void) modifyCellByIDX:(int) idx
486 [self modifyCell:[self getCellByIDX:idx]];
489 // -------------------------------------------------------------------------------
491 // -------------------------------------------------------------------------------
492 -(void) modifyCell:(DBCellElement*) cellElement
494 [NSBundle loadNibNamed:@"IpPanel" owner:self];
495 [((IpConfiguratorCommander*) ipConfControllerCommander) setWorkCell:cellElement];
496 [NSApp beginSheet: ipConfigurationSheet
497 modalForWindow: [[self mainView] window]
499 didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
503 // -------------------------------------------------------------------------------
505 // -------------------------------------------------------------------------------
506 - (IBAction) addRemoveCell:(id) sender
508 switch([((NSControl*) sender) tag]){
509 case ADD_CELL_CONTROL_TAG:
511 DBCellElement *newCell = [[DBCellElement alloc] init];
514 [newCell setCellName:kNewCellName];
515 [newCell setCellComment:kNewCellComment];
517 [[afsProperty getCellList] addObject:newCell];
521 [self modifyCell:newCell];
525 case REMOVE_CELL_CONTROL_TAG:
528 NSIndexSet *selectedIndex = [(NSTableView*)cellList selectedRowIndexes];
529 if( [selectedIndex count] > 0) {
530 index = [selectedIndex firstIndex];
532 DBCellElement *cellElement = (DBCellElement*)[filteredCellDB objectAtIndex:index];
533 [[afsProperty getCellList] removeObject:cellElement];
534 } while ((index = [selectedIndex indexGreaterThanIndex:index]) != NSNotFound);
539 //Filter the cellServDb and allocate filtered array
540 [self searchCellTextEvent:nil];
541 [(NSTableView*)cellList deselectAll:nil];
542 [(NSTableView*)cellList reloadData];
545 // -------------------------------------------------------------------------------
547 // -------------------------------------------------------------------------------
548 - (void) repairHelperTool
553 NSLog(@"repairHelperTool");
554 NSString *afshlpPath = [[self bundle] pathForResource:@"afshlp" ofType:nil];
558 // Open tool exclusively, so nobody can change it while we bless it.
559 fdTool = open([afshlpPath UTF8String], O_NONBLOCK | O_RDONLY | O_EXLOCK, 0);
563 NSLog(@"Exclusive open while repairing tool failed: %d.", errno);
567 if(fstat(fdTool, &st))
569 NSLog(@"fstat failed.");
575 status = [[AuthUtil shared] autorize];
577 fchown(fdTool, 0, st.st_gid);
579 // Disable group and world writability and make setuid root.
580 fchmod(fdTool, (st.st_mode & (~(S_IWGRP | S_IWOTH)))/* | S_ISUID*/);
581 const char *args[] = {"root", [afshlpPath UTF8String],0L};
582 [[AuthUtil shared] execUnixCommand:"/usr/sbin/chown"
585 [[AuthUtil shared] deautorize];
587 } else NSLog(@"st_uid = 0");
593 NSLog(@"Self-repair done.");
598 // -------------------------------------------------------------------------------
600 // -------------------------------------------------------------------------------
601 - (IBAction) startStopAfs:(id) sender
603 BOOL currentAfsState = NO;
605 currentAfsState = [afsProperty checkAfsStatus];
606 // make the parameter to call the root helper app
609 NSLog(@"Shutting down afs");
610 [afsProperty shutdown];
613 NSLog(@"Starting up afs");
614 [afsProperty startup];
616 [self refreshGui:nil];
618 @catch (NSException * e) {
619 [self showMessage:[e reason]];
622 [[AuthUtil shared] deautorize];
626 // -------------------------------------------------------------------------------
628 // -------------------------------------------------------------------------------
629 - (void) refreshGui:(NSNotification *)notification{
630 BOOL afsIsUp = [afsProperty checkAfsStatus];
632 [tokensButton setEnabled:afsIsUp];
633 [unlogButton setEnabled:afsIsUp];
637 // -------------------------------------------------------------------------------
638 // -(void) refreshTokensNotify:(NSNotification*)notification
639 // -------------------------------------------------------------------------------
640 -(void) refreshTokensNotify:(NSNotification*)notification {
641 [self refreshTokens:nil];
644 // -------------------------------------------------------------------------------
645 // afsVolumeMountChange: Track the afs volume state change
646 // -------------------------------------------------------------------------------
647 - (void) afsVolumeMountChange:(NSNotification *)notification{
648 // Cehck if is mounted or unmounted afs
649 if([[[notification userInfo] objectForKey:@"NSDevicePath"] isEqualToString:@"/afs"]){
651 [self refreshTokens:nil];
655 // -------------------------------------------------------------------------------
657 // -------------------------------------------------------------------------------
658 - (IBAction) info:(id) sender
660 [((InfoController*) infoController) showHtmlResource:[[self bundle] pathForResource:@"license" ofType:@"rtf"]];
662 [NSApp beginSheet: infoSheet
663 modalForWindow: [[self mainView] window]
665 didEndSelector: @selector(didEndInfoSheet:returnCode:contextInfo:)
669 // -------------------------------------------------------------------------------
670 // tableDoubleAction:
671 // -------------------------------------------------------------------------------
672 - (IBAction) tableDoubleAction:(id) sender
674 [self showCellIP:nil];
677 // -------------------------------------------------------------------------------
679 // -------------------------------------------------------------------------------
680 - (IBAction) getNewToken:(id) sender
682 BOOL useAklog = [useAklogCheck state] == NSOnState;
684 //[AFSPropertyManager aklog];
685 [afsProperty getTokens:false
688 [self refreshTokens:nil];
689 //Inform afs menuextra to updata afs status
690 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAFSMenuExtraID object:kMExtraAFSStateChange];
693 [NSBundle loadNibNamed:@"CredentialPanel" owner:self];
694 [NSApp beginSheet: credentialSheet
695 modalForWindow: [[self mainView] window]
697 didEndSelector: @selector(didEndCredentialSheet:returnCode:contextInfo:)
703 // -------------------------------------------------------------------------------
704 // getCurrentCellInDB:
705 // -------------------------------------------------------------------------------
706 - (IBAction) unlog:(id) sender
709 NSIndexSet *selectedIndex = [(NSTableView*)tokensTable selectedRowIndexes];
710 if( [selectedIndex count] > 0) {
711 index = [selectedIndex firstIndex];
713 NSString *tokenDesc = [tokenList objectAtIndex:index];
714 NSString *cellToUnlog = [tokenDesc estractTokenByDelimiter:@"afs@"
716 [afsProperty unlog:cellToUnlog];
717 } while ((index = [selectedIndex indexGreaterThanIndex: index]) != NSNotFound);
719 [afsProperty unlog:nil];
721 [self refreshTokens:nil];
722 //Inform afs menuextra to updata afs status
723 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAFSMenuExtraID object:kMExtraAFSStateChange];
728 // -------------------------------------------------------------------------------
730 // -------------------------------------------------------------------------------
731 - (IBAction) aklogSwitchEvent:(id) sender
733 //afs menu extra is loaded inform it to read preference
735 if(![useAklogCheck state]) {
736 //deselect the checkbox
737 [aklogCredentialAtLoginTime setState:NO];
740 [self writePreferenceFile];
742 //Enable disable aklog at login time checkbox according the useAklog checkbox
743 [aklogCredentialAtLoginTime setEnabled:[useAklogCheck state]];
746 @catch (NSException * e) {
747 [self showMessage:[e reason]];
753 // -------------------------------------------------------------------------------
754 // credentialAtLoginTimeEvent:
755 // -------------------------------------------------------------------------------
756 - (IBAction) credentialAtLoginTimeEvent:(id) sender {
757 [self writePreferenceFile];
760 // -------------------------------------------------------------------------------
761 // afsStartupSwitchEvent:
762 // -------------------------------------------------------------------------------
763 - (IBAction) afsStartupSwitchEvent:(id) sender {
764 NSString *rootHelperApp = [[self bundle] pathForResource:@"afshlp" ofType:@""];
766 startAFSAtLogin = [checkButtonAfsAtBootTime state];
767 const char *startupConfigureOption[] = {"start_afs_at_startup", startAFSAtLogin?"enable":"disable", 0L};
768 if([[AuthUtil shared] autorize] == noErr) {
769 //now disable the launchd configuration
770 [[AuthUtil shared] execUnixCommand:[rootHelperApp UTF8String]
771 args:startupConfigureOption
777 // -------------------------------------------------------------------------------
778 // afsMenuActivationEvent:
779 // -------------------------------------------------------------------------------
780 - (IBAction) krb5KredentialAtLoginTimeEvent:(id) sender {
782 NSString *rootHelperApp = [[self bundle] pathForResource:@"afshlp" ofType:@""];
783 const char *args[] = {"enable_krb5_startup", [[installKRB5AuthAtLoginButton stringValue] UTF8String], "", 0L};
786 [self repairHelperTool];
787 if([[AuthUtil shared] autorize] == noErr) {
788 [[AuthUtil shared] execUnixCommand:[rootHelperApp UTF8String]
792 //check if all is gone well
793 [installKRB5AuthAtLoginButton setState:[PListManager checkKrb5AtLoginTimeLaunchdEnable]];
797 // -------------------------------------------------------------------------------
798 // afsMenuActivationEvent:
799 // -------------------------------------------------------------------------------
800 -(IBAction) afsMenuActivationEvent:(id) sender
802 CFPreferencesSetValue((CFStringRef)PREFERENCE_SHOW_STATUS_MENU,
803 (CFNumberRef)[NSNumber numberWithBool:[afsMenucheckBox state]],
804 (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
806 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
807 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
809 //notify the backgrounder
810 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAFSMenuExtraID object:kMExtraAFSMenuChangeState];
813 // -------------------------------------------------------------------------------
814 // searchCellTextEvent:
815 // Fileter the CellServDB list according to NSSearch content
816 // -------------------------------------------------------------------------------
817 - (IBAction) searchCellTextEvent:(id) sender
820 NSString *searchText = [[textSearchField stringValue] lowercaseString]; //filter string
821 [self filterCellServDB:searchText];
822 [((NSTableView*)cellList) reloadData];
825 // -------------------------------------------------------------------------------
826 // clearCellServDBFiltering:
827 // clear the NSSearchField and showw all CellServDB table
828 // -------------------------------------------------------------------------------
829 - (void) clearCellServDBFiltering {
830 //Clear the text search
831 [textSearchField setStringValue:@""];
832 //load the temp array with all cell servdb
833 [self searchCellTextEvent:nil];
835 // --------------------------------------o-----------------------------------------
837 // make the NSMutableArray with all cellservdb or filtered element
838 // -------------------------------------------------------------------------------
839 - (void) filterCellServDB:(NSString*)textToFilter {
840 DBCellElement *cellElement; //Filtered element
841 BOOL doFilter = !(textToFilter == nil || ([textToFilter length] == 0));
843 // We can do filtering and make the temp array
845 [filteredCellDB release];
847 filteredCellDB = [[NSMutableArray alloc] init];
848 NSEnumerator *e = [[afsProperty getCellList] objectEnumerator];
849 while(cellElement = (DBCellElement*)[e nextObject]) {
850 // check if the element can be get
852 //Get the CellServDB array enumerator
853 NSRange rsltRng = [[[cellElement getCellName] lowercaseString] rangeOfString:textToFilter];
854 if(rsltRng.location != NSNotFound) {
855 //we can add this cell to filtered
856 [filteredCellDB addObject:[cellElement retain]];
859 [filteredCellDB addObject:[cellElement retain]];
865 // -------------------------------------------------------------------------------
866 // getCurrentCellInDB:
867 // -------------------------------------------------------------------------------
868 - (DBCellElement*) getCurrentCellInDB
870 int rowSelected = [((NSTableView *) cellList) selectedRow];
871 return [self getCellByIDX:rowSelected];
874 // -------------------------------------------------------------------------------
875 // getCurrentCellInDB:
876 // -------------------------------------------------------------------------------
877 - (DBCellElement*) getCellByIDX:(int) idx
879 //NSMutableArray *cellArray = [afsProperty getCellList];
880 DBCellElement *cellElement = (DBCellElement*)[filteredCellDB objectAtIndex:idx];
884 // -------------------------------------------------------------------------------
886 // -------------------------------------------------------------------------------
887 -(void) showMessage:(NSString*) message{
888 NSAlert *alert = [[NSAlert alloc] init];
890 [alert setMessageText:message];
891 [alert beginSheetModalForWindow:[[self mainView] window]
898 // -------------------------------------------------------------------------------
900 // -------------------------------------------------------------------------------
903 BOOL afsIsUp = [afsProperty checkAfsStatus];
904 BOOL afsEnabledAtStartup = [afsProperty checkAfsStatusForStartup];
907 [((NSButton *)startStopButton) setTitle: (afsIsUp?kAfsButtonShutdown:kAfsButtonStartup)];
909 NSMutableAttributedString *colorTitle =[[NSMutableAttributedString alloc] initWithAttributedString:[((NSButton *)startStopButton) attributedTitle]];
910 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
912 [colorTitle addAttribute:NSForegroundColorAttributeName
913 value:(afsIsUp?[NSColor redColor]:[NSColor blackColor])
916 [((NSButton *)startStopButton) setAttributedTitle:colorTitle];
917 [checkButtonAfsAtBootTime setState:afsEnabledAtStartup];
925 // -------------------------------------------------------------------------------
927 // -------------------------------------------------------------------------------
928 - (void) refreshTokens:(NSTimer*)theTimer;
930 if(![tokensLock tryLock]) return;
935 tokenList = [afsProperty getTokenList];
936 [((NSTableView*)tokensTable) reloadData];
940 // -------------------------------------------------------------------------------
942 // -------------------------------------------------------------------------------
943 - (IBAction) addLink:(id) sender {
944 [NSBundle loadNibNamed:@"SymLinkEdit" owner:self];
946 [NSApp beginSheet: lyncCreationSheet
947 modalForWindow: [[self mainView] window]
949 didEndSelector: @selector(didEndSymlinkSheet:returnCode:contextInfo:)
954 // -------------------------------------------------------------------------------
956 // -------------------------------------------------------------------------------
957 - (IBAction) removeLink:(id) sender {
958 if(!linkConfiguration) return;
960 NSArray *keys = [linkConfiguration allKeys];
961 NSIndexSet *linkToRemove = [tableViewLink selectedRowIndexes];
962 if( [linkToRemove count] > 0) {
963 index = [linkToRemove firstIndex];
965 [linkConfiguration removeObjectForKey:[keys objectAtIndex:index]];
966 } while ((index = [linkToRemove indexGreaterThanIndex:index]) != -1);
969 //write the new configuration
970 NSData *prefData = nil;
971 if([linkConfiguration count] > 0) {
972 prefData = [NSPropertyListSerialization dataWithPropertyList:linkConfiguration
973 format:NSPropertyListXMLFormat_v1_0
977 CFPreferencesSetValue((CFStringRef)PREFERENCE_LINK_CONFIGURATION,
979 (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
981 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
983 //reload the new data
984 [tableViewLink reloadData];
987 // -------------------------------------------------------------------------------
989 // -------------------------------------------------------------------------------
990 - (IBAction) enableLink:(id) sender {
991 [self writePreferenceFile];
994 // -------------------------------------------------------------------------------
996 // -------------------------------------------------------------------------------
997 - (IBAction) manageBackgrounderActivation:(id)sender {
998 [PListManager launchctlCommand:[(NSButton*)sender state]
1000 option:[NSArray arrayWithObjects:@"-S", @"Aqua", nil]
1001 plistName:[NSString stringWithFormat:@"%@.plist", BACKGROUNDER_P_FILE]];
1002 //re ad the status to check taht all is gone well
1003 [backgrounderActivationCheck setState:[PListManager launchdJobState:BACKGROUNDER_P_FILE]];
1006 // -------------------------------------------------------------------------------
1007 // tableViewLinkPerformClick:
1008 // -------------------------------------------------------------------------------
1009 - (IBAction) tableViewLinkPerformClick:(id) sender {
1010 NSLog(@"tableViewLinkPerformClick");
1013 // -------------------------------------------------------------------------------
1014 // - (void)tabView:(NSTabView *)tabView willSelectTabViewItem: (NSTabViewItem *)tabViewItem
1015 // -------------------------------------------------------------------------------
1016 - (void)tabView:(NSTabView *)tabView willSelectTabViewItem: (NSTabViewItem *)tabViewItem
1018 //check to see if the cache param tab is the tab that will be selected
1019 if([((NSString*)[tabViewItem identifier]) intValue] == TAB_LINK)
1021 [tableViewLink reloadData];
1027 @implementation AFSCommanderPref (NSTableDataSource)
1030 // -------------------------------------------------------------------------------
1032 // Manage the checkbox of CellServDB Table
1034 // -------------------------------------------------------------------------------
1035 - (void)tableView:(NSTableView *)table
1036 setObjectValue:(id)data
1037 forTableColumn:(NSTableColumn *)col
1040 NSString *identifier = (NSString*)[col identifier];
1041 switch([table tag]){
1042 case TABLE_TOKENS_LIST:
1045 case TABLE_CELL_LIST:
1046 // we are editing checkbox for cellservdb table
1047 if([identifier intValue] == CELLSRVDB_TABLE_USR_DFLT_CHECK_COLUMN) {
1048 // set the user default cell
1049 DBCellElement *cellElement = (DBCellElement*)[filteredCellDB objectAtIndex:row];
1050 [afsProperty setDefaultCellByName:[cellElement getCellName]];
1051 //[afsDefaultCellLabel setStringValue:[afsProperty getDefaultCellName]];
1052 [((NSTableView*)cellList) reloadData];
1053 } else if([identifier intValue] == CELLSRVDB_TABLE_DFLT_CHECK_COLUMN) {
1054 // set the cell for wich the user want to get token
1055 DBCellElement *cellElement = (DBCellElement*)[filteredCellDB objectAtIndex:row];
1056 [cellElement setUserDefaultForToken:![cellElement userDefaultForToken]];
1064 // -------------------------------------------------------------------------------
1066 // refresh delegate method for two AFSCommander table
1067 // -------------------------------------------------------------------------------
1068 - (id) tableView:(NSTableView *) aTableView
1069 objectValueForTableColumn:(NSTableColumn *) aTableColumn
1074 NSString *identifier = (NSString*)[aTableColumn identifier];
1075 switch([aTableView tag]){
1076 case TABLE_TOKENS_LIST:
1077 //We are refreshing tokens table
1078 result = [self getTableTokensListValue:[identifier intValue] row:rowIndex];
1081 case TABLE_CELL_LIST:
1082 //We are refreshing cell db table
1083 result = [self getTableCelListValue:[identifier intValue] row:rowIndex];
1086 case TABLE_LINK_LIST:
1087 result = [self getTableLinkValue:[identifier intValue] row:rowIndex];
1096 // -------------------------------------------------------------------------------
1097 // getTableCelListValue:
1098 // -------------------------------------------------------------------------------
1099 - (id)getTableTokensListValue:(int) colId row:(int)row
1102 if(!tokenList) return nil;
1105 result = (NSString*)[tokenList objectAtIndex:row];
1112 // -------------------------------------------------------------------------------
1113 // getTableCelListValue:
1114 // -------------------------------------------------------------------------------
1115 - (id)getTableCelListValue:(int) colId row:(int)row
1118 //NSMutableArray *cellArray = [afsProperty getCellList];
1119 DBCellElement *cellElement = (DBCellElement*)[filteredCellDB objectAtIndex:row];
1121 case CELLSRVDB_TABLE_USR_DFLT_CHECK_COLUMN:
1122 result = [NSNumber numberWithInt:[cellElement userDefaultForCell]];
1125 case CELLSRVDB_TABLE_DFLT_CHECK_COLUMN:
1126 result = [NSNumber numberWithInt:[cellElement userDefaultForToken]];
1128 case CELLSRVDB_TABLE_NAME_COLUMN:
1129 result = [cellElement getCellName];
1132 case CELLSRVDB_TABLE_DESCRIPTION_COLUMN:
1133 result = [cellElement getCellComment];
1139 // -------------------------------------------------------------------------------
1140 // getTableCelListValue:
1141 // -------------------------------------------------------------------------------
1142 - (id)getTableLinkValue:(int) colId row:(int)row
1145 NSArray *allKey = [linkConfiguration allKeys];
1147 case TABLE_COLUMN_LINK_NAME:
1148 result = [allKey objectAtIndex:row];
1151 case TABLE_COLUMN_LINK_PATH:
1152 result = [linkConfiguration objectForKey:[allKey objectAtIndex:row]];
1159 // -------------------------------------------------------------------------------
1160 // numberOfRowsInTableView:
1161 // -------------------------------------------------------------------------------
1162 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
1165 //NSMutableArray *cellArray = nil;
1166 switch([aTableView tag]){
1167 case TABLE_TOKENS_LIST:
1168 if(tokenList) rowCount = [tokenList count];
1171 case TABLE_CELL_LIST:
1172 //cellArray = [afsProperty getCellList];
1173 if(filteredCellDB) rowCount = [filteredCellDB count];
1176 case TABLE_LINK_LIST:
1177 if(linkConfiguration) rowCount = [linkConfiguration count];
1187 @implementation AFSCommanderPref (TableDelegate)
1188 // -------------------------------------------------------------------------------
1189 // selectionShouldChangeInTableView:
1190 // -------------------------------------------------------------------------------
1191 - (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTable
1193 switch([aTable tag]){
1194 case TABLE_TOKENS_LIST:
1198 case TABLE_CELL_LIST:
1199 [self tableViewCellmanageButtonState:[aTable selectedRow]];
1202 case TABLE_LINK_LIST:
1211 // -------------------------------------------------------------------------------
1213 // -------------------------------------------------------------------------------
1214 - (BOOL)tableView:(NSTableView *)aTable shouldSelectRow:(int)aRow
1216 switch([aTable tag]){
1217 case TABLE_TOKENS_LIST:
1221 case TABLE_CELL_LIST:
1222 [self tableViewCellmanageButtonState:aRow];
1225 case TABLE_LINK_LIST:
1234 // -------------------------------------------------------------------------------
1236 // -------------------------------------------------------------------------------
1237 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
1238 NSTableView *aTable = [aNotification object];
1239 switch([aTable tag]){
1240 case TABLE_TOKENS_LIST:
1243 case TABLE_CELL_LIST:
1246 case TABLE_LINK_LIST:
1247 [self tableViewLinkmanageButtonState:[aTable selectedRowIndexes]];
1253 // -------------------------------------------------------------------------------
1254 // manageButtonState:
1255 // -------------------------------------------------------------------------------
1256 -(void) tableViewCellmanageButtonState:(int) rowSelected {
1257 [((NSControl*) cellIpButton) setEnabled:rowSelected >= 0];
1258 [((NSControl*) removeCellButton) setEnabled:rowSelected >= 0];
1261 // -------------------------------------------------------------------------------
1262 // manageButtonState:
1263 // -------------------------------------------------------------------------------
1264 -(void) tableViewLinkmanageButtonState:(NSIndexSet *) rowsSelectedIndex {
1265 [buttonRemoveLink setEnabled:[rowsSelectedIndex count]>0];
1270 @implementation AFSCommanderPref (ModalDelegate)
1271 // -------------------------------------------------------------------------------
1273 // -------------------------------------------------------------------------------
1274 - (void)didEndSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
1276 [sheet orderOut:self];
1277 //Filter the cellServDb and allocate filtered array
1278 [self searchCellTextEvent:nil];
1279 [((NSTableView*)cellList) reloadData];
1282 // -------------------------------------------------------------------------------
1283 // Klog credential request
1284 // -------------------------------------------------------------------------------
1285 - (void)didEndCredentialSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
1287 if([((TokenCredentialController*)credentialCommander) takenToken] == YES){
1288 /*[AFSPropertyManager klog:[((TokenCredentialController*)credentialCommander) uName]
1289 uPwd:[((TokenCredentialController*)credentialCommander) uPwd] ];*/
1290 [afsProperty getTokens:true
1291 usr:[((TokenCredentialController*)credentialCommander) uName]
1292 pwd:[((TokenCredentialController*)credentialCommander) uPwd]];
1294 [sheet orderOut:self];
1295 [self refreshTokens:nil];
1296 //Inform afs menuextra to updata afs status
1297 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAFSMenuExtraID object:kMExtraAFSStateChange];
1301 // -------------------------------------------------------------------------------
1302 // Klog credential request
1303 // -------------------------------------------------------------------------------
1304 - (void)didEndInfoSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
1306 [sheet orderOut:self];
1309 // -------------------------------------------------------------------------------
1311 // -------------------------------------------------------------------------------
1312 - (void)didEndSymlinkSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
1314 [lyncCreationSheet orderOut:self];
1315 [self readPreferenceFile];
1316 [tableViewLink reloadData];