Merge branch 'pu/pb/index_quick_fix' into stable
[GitX.git] / PBWebChangesController.m
blobd24a8afd5633aa2989f6cf6cb16c4bc081755380
1 //
2 //  PBWebChangesController.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 22-09-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBWebChangesController.h"
10 #import "PBGitIndexController.h"
12 @implementation PBWebChangesController
14 - (void) awakeFromNib
16         selectedFile = nil;
17         selectedFileIsCached = NO;
19         startFile = @"commit";
20         [super awakeFromNib];
22         [unstagedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"UnstagedFileSelected"];
23         [cachedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"cachedFileSelected"];
26 - (void) didLoad
28         [[self script] setValue:indexController forKey:@"IndexController"];
29         [self refresh];
32 - (BOOL) amend
34         return controller.amend;
37 - (void)observeValueForKeyPath:(NSString *)keyPath
38                                           ofObject:(id)object
39                                                 change:(NSDictionary *)change
40                                            context:(void *)context
42         NSArrayController *otherController;
43         otherController = object == unstagedFilesController ? cachedFilesController : unstagedFilesController;
44         int count = [[object selectedObjects] count];
45         if (count == 0) {
46                 if([[otherController selectedObjects] count] == 0 && selectedFile) {
47                         selectedFile = nil;
48                         selectedFileIsCached = NO;
49                         [self refresh];
50                 }
51                 return;
52         }
54         // TODO: Move this to commitcontroller
55         [otherController setSelectionIndexes:[NSIndexSet indexSet]];
57         if (count > 1) {
58                 [self showMultiple: [object selectedObjects]];
59                 return;
60         }
62         selectedFile = [[object selectedObjects] objectAtIndex:0];
63         selectedFileIsCached = object == cachedFilesController;
65         [self refresh];
68 - (void) showMultiple: (NSArray *)objects
70         [[self script] callWebScriptMethod:@"showMultipleFilesSelection" withArguments:[NSArray arrayWithObject:objects]];
73 - (void) refresh
75         if (!finishedLoading)
76                 return;
78         id script = [view windowScriptObject];
79         [script callWebScriptMethod:@"showFileChanges"
80                       withArguments:[NSArray arrayWithObjects:selectedFile ?: (id)[NSNull null],
81                                      [NSNumber numberWithBool:selectedFileIsCached], nil]];
84 - (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse
86         [controller stageHunk: hunk reverse:reverse];
87         [self refresh];
90 - (void)discardHunk:(NSString *)hunk altKey:(BOOL)altKey
92         int ret = NSAlertDefaultReturn;
93         if (!altKey) {
94                 ret = [[NSAlert alertWithMessageText:@"Discard hunk"
95                         defaultButton:nil
96                         alternateButton:@"Cancel"
97                         otherButton:nil
98                         informativeTextWithFormat:@"Are you sure you wish to discard the changes in this hunk?\n\nYou cannot undo this operation."] runModal];
99         }
101         if (ret == NSAlertDefaultReturn) {
102                 [controller discardHunk:hunk];
103                 [self refresh];
104         }
107 - (void) setStateMessage:(NSString *)state
109         id script = [view windowScriptObject];
110         [script callWebScriptMethod:@"setState" withArguments: [NSArray arrayWithObject:state]];
113 - (void) setContextSize:(int)size
115         if (size == indexController.contextSize)
116                 return;
118         indexController.contextSize = size;
119         [self refresh];
121 @end