Files
gitx/PBWebChangesController.m
Nathan Kinsinger 53d92fb73e Cleanup the views when the repository window closes and stop memory leaks.
- make sure to remove themselves from KV and notification center observers
    - add the PBWebHistoryController to PBHistoryController so it can be told to close
    - replaced the -removeView methods with -closeView (-removeView was not being used)
    - clear any obj-c objects set in web scripting objects

This last item seems to be the reason that the web controllers and the current commit did not get collected which then held the repository document from being collected as well.
2010-07-04 09:46:22 -06:00

135 lines
3.7 KiB
Objective-C

//
// PBWebChangesController.m
// GitX
//
// Created by Pieter de Bie on 22-09-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBWebChangesController.h"
#import "PBGitIndexController.h"
#import "PBGitIndex.h"
@implementation PBWebChangesController
- (void) awakeFromNib
{
selectedFile = nil;
selectedFileIsCached = NO;
startFile = @"commit";
[super awakeFromNib];
[unstagedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"UnstagedFileSelected"];
[cachedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"cachedFileSelected"];
}
- (void)closeView
{
[[self script] removeWebScriptKey:@"Index"];
[unstagedFilesController removeObserver:self forKeyPath:@"selection"];
[cachedFilesController removeObserver:self forKeyPath:@"selection"];
[super closeView];
}
- (void) didLoad
{
[[self script] setValue:controller.index forKey:@"Index"];
[self refresh];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSArrayController *otherController;
otherController = object == unstagedFilesController ? cachedFilesController : unstagedFilesController;
int count = [[object selectedObjects] count];
if (count == 0) {
if([[otherController selectedObjects] count] == 0 && selectedFile) {
selectedFile = nil;
selectedFileIsCached = NO;
[self refresh];
}
return;
}
// TODO: Move this to commitcontroller
[otherController setSelectionIndexes:[NSIndexSet indexSet]];
if (count > 1) {
[self showMultiple: [object selectedObjects]];
return;
}
selectedFile = [[object selectedObjects] objectAtIndex:0];
selectedFileIsCached = object == cachedFilesController;
[self refresh];
}
- (void) showMultiple: (NSArray *)objects
{
[[self script] callWebScriptMethod:@"showMultipleFilesSelection" withArguments:[NSArray arrayWithObject:objects]];
}
- (void) refresh
{
if (!finishedLoading)
return;
id script = [view windowScriptObject];
[script callWebScriptMethod:@"showFileChanges"
withArguments:[NSArray arrayWithObjects:selectedFile ?: (id)[NSNull null],
[NSNumber numberWithBool:selectedFileIsCached], nil]];
}
- (void)stageHunk:(NSString *)hunk reverse:(BOOL)reverse
{
[controller.index applyPatch:hunk stage:YES reverse:reverse];
// FIXME: Don't need a hard refresh
[self refresh];
}
- (void) discardHunk:(NSString *)hunk
{
[controller.index applyPatch:hunk stage:NO reverse:YES];
[self refresh];
}
- (void) discardHunkAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[[alert window] orderOut:nil];
if (returnCode == NSAlertDefaultReturn)
[self discardHunk:contextInfo];
}
- (void)discardHunk:(NSString *)hunk altKey:(BOOL)altKey
{
if (!altKey) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Discard hunk"
defaultButton:nil
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"Are you sure you wish to discard the changes in this hunk?\n\nYou cannot undo this operation."];
[alert beginSheetModalForWindow:[[controller view] window]
modalDelegate:self
didEndSelector:@selector(discardHunkAlertDidEnd:returnCode:contextInfo:)
contextInfo:hunk];
} else {
[self discardHunk:hunk];
}
}
- (void) setStateMessage:(NSString *)state
{
id script = [view windowScriptObject];
[script callWebScriptMethod:@"setState" withArguments: [NSArray arrayWithObject:state]];
}
@end