Add a new File Access preferences pane.
[macgdbp.git] / Source / BreakpointManager.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "BreakpointManager.h"
18
19 #import "AppDelegate.h"
20 #import "PreferenceNames.h"
21
22 @interface BreakpointManager (Private)
23 - (void)updateDisplaysForFile:(NSString*)file;
24 @end
25
26 @implementation BreakpointManager {
27 NSMutableArray* _breakpoints;
28 NSMutableArray* _savedBreakpoints;
29
30 DebuggerBackEnd* __weak _connection;
31 }
32
33 - (id)init
34 {
35 if (self = [super init])
36 {
37 _breakpoints = [[NSMutableArray alloc] init];
38 _savedBreakpoints = [[NSMutableArray alloc] init];
39
40 NSArray* savedBreakpoints = [[NSUserDefaults standardUserDefaults] arrayForKey:kPrefBreakpoints];
41 if (savedBreakpoints) {
42 [_savedBreakpoints addObjectsFromArray:savedBreakpoints];
43 for (NSDictionary* d in savedBreakpoints) {
44 [_breakpoints addObject:[[Breakpoint alloc] initWithDictionary:d]];
45 }
46 }
47 }
48 return self;
49 }
50
51 /**
52 * Registers a breakpoint at a given line
53 */
54 - (void)addBreakpoint:(Breakpoint*)bp;
55 {
56 if ([_breakpoints containsObject:bp])
57 return;
58
59 if (bp.type == kBreakpointTypeFile && !bp.secureBookmark) {
60 // There is no secure bookmark for this file, so first see if any other
61 // bookmarks exist for the same file. If not, try and create a bookmark.
62 if (!bp.secureBookmark) {
63 [bp createSecureBookmark];
64 /*
65 for (Breakpoint* other in _breakpoints) {
66 if ([other.file isEqualToString:bp.file] && other.secureBookmark) {
67 bp.secureBookmark = other.secureBookmark;
68 break;
69 }
70 }
71
72 if (!bp.secureBookmark) {
73 [bp createSecureBookmark];
74 }
75 */
76 }
77 }
78
79 [self willChangeValueForKey:@"breakpoints"];
80 [_breakpoints addObject:bp];
81 [self didChangeValueForKey:@"breakpoints"];
82
83 [_connection addBreakpoint:bp];
84
85 [_savedBreakpoints addObject:[bp dictionary]];
86 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
87 [self updateDisplaysForFile:[bp file]];
88 }
89
90 - (Breakpoint*)removeBreakpoint:(Breakpoint*)bp
91 {
92 // Use the -isEqual: test to find the object in |_breakpoints| that also has
93 // the debugger id and secure bookmark data.
94 NSUInteger idx = [_breakpoints indexOfObject:bp];
95 if (idx == NSNotFound)
96 return nil;
97
98 bp = [_breakpoints objectAtIndex:idx];
99
100 [self willChangeValueForKey:@"breakpoints"];
101 [_breakpoints removeObject:bp];
102 [self didChangeValueForKey:@"breakpoints"];
103
104 [_connection removeBreakpoint:bp];
105
106 [_savedBreakpoints removeObject:[bp dictionary]];
107 [[NSUserDefaults standardUserDefaults] setObject:_savedBreakpoints forKey:kPrefBreakpoints];
108
109 if (bp.file)
110 [self updateDisplaysForFile:bp.file];
111
112 return bp;
113 }
114
115 /**
116 * Returns all the breakpoints for a given file
117 */
118 - (NSSet<NSNumber*>*)breakpointsForFile:(NSString*)file
119 {
120 NSMutableSet<NSNumber*>* matches = [NSMutableSet set];
121 for (Breakpoint* b in _breakpoints) {
122 if ([b.file isEqualToString:file]) {
123 [matches addObject:@(b.line)];
124 }
125 }
126
127 return matches;
128 }
129
130
131 - (BOOL)hasBreakpoint:(Breakpoint*)breakpoint
132 {
133 return [_breakpoints containsObject:breakpoint];
134 }
135
136 #pragma mark Private
137
138 /**
139 * This marks BSSourceView needsDisplay, rearranges the objects in the breakpoints controller,
140 * and sets the markers for the BSLineNumberView
141 */
142 - (void)updateDisplaysForFile:(NSString*)file
143 {
144 AppDelegate* appDel = [NSApp delegate];
145 [[[appDel breakpoint] arrayController] rearrangeObjects];
146 [[[appDel debugger] sourceViewer] setNeedsDisplay:YES];
147 [[[appDel debugger] sourceViewer] setMarkers:[self breakpointsForFile:file]];
148 }
149
150 @end