Testing: add missing file
[GitX.git] / PBWebChangesController.m
blob933fecc22a807d0d993c117124f040308e6590ad
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"
11 #import "PBGitIndex.h"
13 @implementation PBWebChangesController
15 - (void) awakeFromNib
17         selectedFile = nil;
18         selectedFileIsCached = NO;
20         startFile = @"commit";
21         [super awakeFromNib];
23         [unstagedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"UnstagedFileSelected"];
24         [cachedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"cachedFileSelected"];
27 - (void) didLoad
29         [[self script] setValue:controller.index forKey:@"Index"];
30         [self refresh];
33 - (void)observeValueForKeyPath:(NSString *)keyPath
34                                           ofObject:(id)object
35                                                 change:(NSDictionary *)change
36                                            context:(void *)context
38         NSArrayController *otherController;
39         otherController = object == unstagedFilesController ? cachedFilesController : unstagedFilesController;
40         int count = [[object selectedObjects] count];
41         if (count == 0) {
42                 if([[otherController selectedObjects] count] == 0 && selectedFile) {
43                         selectedFile = nil;
44                         selectedFileIsCached = NO;
45                         [self refresh];
46                 }
47                 return;
48         }
50         // TODO: Move this to commitcontroller
51         [otherController setSelectionIndexes:[NSIndexSet indexSet]];
53         if (count > 1) {
54                 [self showMultiple: [object selectedObjects]];
55                 return;
56         }
58         selectedFile = [[object selectedObjects] objectAtIndex:0];
59         selectedFileIsCached = object == cachedFilesController;
61         [self refresh];
64 - (void) showMultiple: (NSArray *)objects
66         [[self script] callWebScriptMethod:@"showMultipleFilesSelection" withArguments:[NSArray arrayWithObject:objects]];
69 - (void) refresh
71         if (!finishedLoading)
72                 return;
74         id script = [view windowScriptObject];
75         [script callWebScriptMethod:@"showFileChanges"
76                       withArguments:[NSArray arrayWithObjects:selectedFile ?: (id)[NSNull null],
77                                      [NSNumber numberWithBool:selectedFileIsCached], nil]];
80 - (void)stageHunk:(NSString *)hunk reverse:(BOOL)reverse
82         [controller.index applyPatch:hunk stage:YES reverse:reverse];
83         // FIXME: Don't need a hard refresh
85         [self refresh];
88 - (void)discardHunk:(NSString *)hunk altKey:(BOOL)altKey
90         int ret = NSAlertDefaultReturn;
91         if (!altKey) {
92                 ret = [[NSAlert alertWithMessageText:@"Discard hunk"
93                         defaultButton:nil
94                         alternateButton:@"Cancel"
95                         otherButton:nil
96                         informativeTextWithFormat:@"Are you sure you wish to discard the changes in this hunk?\n\nYou cannot undo this operation."] runModal];
97         }
99         if (ret == NSAlertDefaultReturn) {
100                 [controller.index applyPatch:hunk stage:NO reverse:YES];
101                 [self refresh];
102         }
105 - (void) setStateMessage:(NSString *)state
107         id script = [view windowScriptObject];
108         [script callWebScriptMethod:@"setState" withArguments: [NSArray arrayWithObject:state]];
111 @end