// Copyright GreenKeeper Software 2005 #import "ProjectManagerController.h" #import "BOINCController.h" #import "ClientState.h" // Handles the project manager @implementation ProjectManagerController - (void)awakeFromNib { // Grab all attached projects from the client state file projects = [[[ClientState clientState] projects] retain]; // Listens to see if the selection in the table changes [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateButtons:) name:NSTableViewSelectionDidChangeNotification object:nil]; // Listens to see if client state changed and makes appropriate updates [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInterface:) name:@"clientStateChanged" object:nil]; // When a cell in the table is double clicked, [table setTarget:self]; [table setDoubleAction:@selector(goToProjectWebsite)]; lastRequestedAction = nil; } - (void)dealloc { // Local releases [projects release]; // Remove all listeners [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } // Opens the project manager window - (IBAction)openWindow:(id)sender { // Update the list of projects [projects release]; projects = [[[ClientState clientState] projects] retain]; [table reloadData]; // Makes sure windows appear at the front [NSApp activateIgnoringOtherApps:YES]; // Set the interface such that no item is selected [table deselectRow:[table selectedRow]]; // Opens the window [window makeKeyAndOrderFront:self]; } // Attaches the add project sheet to the project manager so the user can add a new project - (IBAction)openAddProjectSheet:(id)sender { [NSApp beginSheet:addProjectWindow modalForWindow:window modalDelegate:nil didEndSelector:nil contextInfo:nil]; [NSApp runModalForWindow:addProjectWindow]; [addProjectWindow close]; [NSApp endSheet:addProjectWindow]; } // Attaches a project to this computer - (IBAction)addProject:(id)sender { // Remove the add project sheet [NSApp stopModal]; [BOINCController attachProjectWithURL:[addProjectURLField stringValue] accountID:[addProjectAccountIDField stringValue]]; lastRequestedAction = @"add"; } // Closes the add project sheet - (IBAction)cancelAddProject:(id)sender { [NSApp stopModal]; } // Updates the selected project in the project manager - (IBAction)updateProject:(id)sender { // Grab index of selected row int rowIndex = [table selectedRow]; // Grab url of selected project NSString *url = [[projects objectAtIndex:1] objectAtIndex:rowIndex]; [BOINCController updateProjectWithURL:url]; lastRequestedAction = @"update"; } // Resets the selected project in the project manager - (IBAction)resetProject:(id)sender { // Double checks to make sure the user does want to remove the project NSString *message = @"You are about to reset a project!"; NSString *informativeText = @"Resetting a project will cause all its current data to be deleted. Are you sure you would like to reset the selected project?"; int choice = NSRunAlertPanel(message, informativeText, @"Okay", @"Cancel", nil); if (choice != NSAlertDefaultReturn) return; // Grab index of selected row int rowIndex = [table selectedRow]; // Grab url of selected project NSString *url = [[projects objectAtIndex:1] objectAtIndex:rowIndex]; [BOINCController resetProjectWithURL:url]; lastRequestedAction = @"reset"; } // Detaches the selected project in the project manager - (IBAction)removeProject:(id)sender { // Double checks to make sure the user does want to remove the project NSString *message = @"You are about to remove a project!"; NSString *informativeText = @"Are you sure you would like to permanently remove the selected project?"; int choice = NSRunAlertPanel(message, informativeText, @"Okay", @"Cancel", nil); if (choice != NSAlertDefaultReturn) return; // Grab index of selected row int rowIndex = [table selectedRow]; // Grab url of selected project NSString *url = [[projects objectAtIndex:1] objectAtIndex:rowIndex]; [BOINCController removeProjectWithURL:url]; lastRequestedAction = @"remove"; } // Called when user double clicks on a project in the table; opens that projects website - (void)goToProjectWebsite { int rowIndex = [table selectedRow]; if(rowIndex != -1) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: [[projects objectAtIndex:1 ] objectAtIndex:rowIndex]]]; } // Updates whether or not buttons are enabled when table selection changes // Add button is always enabled - (void)updateButtons:(NSNotification *)notification { if([table selectedRow] == -1) { [removeButton setEnabled:NO]; [updateButton setEnabled:NO]; [resetButton setEnabled:NO]; [addButton setEnabled:YES]; } else { [removeButton setEnabled:YES]; [updateButton setEnabled:YES]; [resetButton setEnabled:YES]; [addButton setEnabled:YES]; } } - (void)updateInterface:(NSNotification *)notification { int oldProjectCount = [[projects objectAtIndex:0] count]; [projects release]; projects = [[[ClientState clientState] projects] retain]; [table reloadData]; int newProjectCount = [[projects objectAtIndex:0] count]; // Project disappeared without user requesting it ---> means adding project was unsuccessful if(![lastRequestedAction isEqualToString:@"remove"] && newProjectCount < oldProjectCount) { [NSApp activateIgnoringOtherApps:YES]; NSRunAlertPanel(@"Adding project was not successful!", @"Please check the URL and your Account ID and try again.", @"Okay", nil, nil); [window makeKeyAndOrderFront:self]; [self openAddProjectSheet:self]; } else if([lastRequestedAction isEqualToString:@"add"]) { [addProjectAccountIDField setStringValue:@""]; [addProjectURLField setStringValue:@""]; [[NSApp keyWindow] makeFirstResponder:addProjectURLField]; } } // Method called by NSTableViews when instructed to reload data - (int)numberOfRowsInTableView:(NSTableView *)aTableView { if([projects count]) return [[projects objectAtIndex:0] count]; else return 0; } // Method called by NSTableViews when instructed to reload data - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex { if(aTableColumn == nameColumn) { return [[projects objectAtIndex:0] objectAtIndex:rowIndex]; } if(aTableColumn == urlColumn) { return [[projects objectAtIndex:1] objectAtIndex:rowIndex]; } } @end