2 // NJMappingsController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "NJMappingsController.h"
11 #import "NJMappingsController.h"
15 #define PB_ROW @"com.yukkurigames.Enjoyable.MappingRow"
17 @implementation NJMappingsController {
18 NSMutableArray *_mappings;
19 NJMapping *_manualMapping;
23 if ((self = [super init])) {
24 _mappings = [[NSMutableArray alloc] init];
25 _currentMapping = [[NJMapping alloc] initWithName:@"(default)"];
26 _manualMapping = _currentMapping;
27 [_mappings addObject:_currentMapping];
32 - (void)awakeFromNib {
33 [tableView registerForDraggedTypes:@[PB_ROW, NSURLPboardType]];
34 [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
37 - (NJMapping *)objectForKeyedSubscript:(NSString *)name {
38 for (NJMapping *mapping in _mappings)
39 if ([name isEqualToString:mapping.name])
44 - (NJMapping *)objectAtIndexedSubscript:(NSUInteger)idx {
45 return idx < _mappings.count ? _mappings[idx] : nil;
48 - (void)mappingsChanged {
50 [tableView reloadData];
51 [self updateInterfaceForCurrentMapping];
52 [NSNotificationCenter.defaultCenter
53 postNotificationName:NJEventMappingListChanged
57 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
58 objects:(__unsafe_unretained id [])buffer
59 count:(NSUInteger)len {
60 return [_mappings countByEnumeratingWithState:state
65 - (void)activateMappingForProcess:(NSRunningApplication *)app {
66 NJMapping *oldMapping = _manualMapping;
67 NSArray *names = app.possibleMappingNames;
69 for (NSString *name in names) {
70 NJMapping *mapping = self[name];
72 [self activateMapping:mapping];
79 [self activateMapping:oldMapping];
80 if ([oldMapping.name.lowercaseString isEqualToString:@"@application"]) {
81 oldMapping.name = app.bestMappingName;
82 [self mappingsChanged];
85 _manualMapping = oldMapping;
88 - (void)updateInterfaceForCurrentMapping {
89 NSUInteger selected = [_mappings indexOfObject:_currentMapping];
90 removeButton.enabled = selected != 0;
91 moveUp.enabled = selected > 1;
92 moveDown.enabled = selected && selected != _mappings.count - 1;
93 popoverActivate.title = _currentMapping.name;
94 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selected] byExtendingSelection:NO];
95 [NSUserDefaults.standardUserDefaults setInteger:selected forKey:@"selected"];
98 - (void)activateMapping:(NJMapping *)mapping {
100 mapping = _manualMapping;
101 if (mapping == _currentMapping)
103 NSLog(@"Switching to mapping %@.", mapping.name);
104 _manualMapping = mapping;
105 _currentMapping = mapping;
106 [self updateInterfaceForCurrentMapping];
107 [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
108 object:_currentMapping];
111 - (IBAction)addPressed:(id)sender {
112 NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"];
113 [_mappings addObject:newMapping];
114 [self activateMapping:newMapping];
115 [self mappingsChanged];
116 [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
119 - (IBAction)removePressed:(id)sender {
120 if (tableView.selectedRow == 0)
123 NSInteger selectedRow = tableView.selectedRow;
124 [_mappings removeObjectAtIndex:selectedRow];
125 [self activateMapping:_mappings[MIN(selectedRow, _mappings.count - 1)]];
126 [self mappingsChanged];
129 - (void)tableViewSelectionDidChange:(NSNotification *)notify {
130 [self activateMapping:self[tableView.selectedRow]];
133 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
134 return self[index].name;
137 - (void)tableView:(NSTableView *)view
138 setObjectValue:(NSString *)obj
139 forTableColumn:(NSTableColumn *)col
140 row:(NSInteger)index {
141 self[index].name = obj;
142 [self mappingsChanged];
145 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
146 return _mappings.count;
149 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
154 NSLog(@"Saving mappings to defaults.");
155 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count];
156 for (NJMapping *mapping in _mappings)
157 [ary addObject:[mapping serialize]];
158 [NSUserDefaults.standardUserDefaults setObject:ary forKey:@"mappings"];
162 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
163 NSArray *mappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
164 [self loadAllFrom:mappings andActivate:selected];
167 - (void)loadAllFrom:(NSArray *)storedMappings andActivate:(NSUInteger)selected {
168 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
170 // Requires two passes to deal with inter-mapping references. First make
171 // an empty mapping for each serialized mapping. Then, deserialize the
172 // data pointing to the empty mappings. Then merge that data back into
173 // its equivalent empty one, which is the one we finally use.
174 for (NSDictionary *storedMapping in storedMappings) {
175 NJMapping *mapping = [[NJMapping alloc] initWithName:storedMapping[@"name"]];
176 [newMappings addObject:mapping];
179 for (unsigned i = 0; i < storedMappings.count; ++i) {
180 NJMapping *realMapping = [[NJMapping alloc] initWithSerialization:storedMappings[i]
181 mappings:newMappings];
182 [newMappings[i] mergeEntriesFrom:realMapping];
185 if (newMappings.count) {
186 _mappings = newMappings;
187 if (selected >= newMappings.count)
189 [self activateMapping:_mappings[selected]];
190 [self mappingsChanged];
194 - (void)addMappingWithContentsOfURL:(NSURL *)url {
195 NSWindow *window = popoverActivate.window;
197 NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url
201 if (mapping && !error) {
202 NJMapping *mergeInto = self[mapping.name];
203 BOOL conflict = [mergeInto hasConflictWith:mapping];
206 NSAlert *conflictAlert = [[NSAlert alloc] init];
207 conflictAlert.messageText = @"Replace existing mappings?";
208 conflictAlert.informativeText =
209 [NSString stringWithFormat:
210 @"This file contains inputs you've already mapped in \"%@\". Do you "
211 @"want to merge them and replace your existing mappings, or import this "
212 @"as a separate mapping?", mapping.name];
213 [conflictAlert addButtonWithTitle:@"Merge"];
214 [conflictAlert addButtonWithTitle:@"Cancel"];
215 [conflictAlert addButtonWithTitle:@"New Mapping"];
216 NSInteger res = [conflictAlert runModal];
217 if (res == NSAlertSecondButtonReturn)
219 else if (res == NSAlertThirdButtonReturn)
224 [mergeInto mergeEntriesFrom:mapping];
227 [_mappings addObject:mapping];
230 [self activateMapping:mapping];
231 [self mappingsChanged];
233 if (conflict && !mergeInto) {
234 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
235 [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
240 [window presentError:error
241 modalForWindow:window
243 didPresentSelector:nil
248 - (void)importPressed:(id)sender {
249 NSOpenPanel *panel = [NSOpenPanel openPanel];
250 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
251 NSWindow *window = NSApplication.sharedApplication.keyWindow;
252 [panel beginSheetModalForWindow:window
253 completionHandler:^(NSInteger result) {
254 if (result != NSFileHandlingPanelOKButton)
257 [self addMappingWithContentsOfURL:panel.URL];
262 - (void)exportPressed:(id)sender {
263 NSSavePanel *panel = [NSSavePanel savePanel];
264 panel.allowedFileTypes = @[ @"enjoyable" ];
265 NJMapping *mapping = _currentMapping;
266 panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent];
267 NSWindow *window = NSApplication.sharedApplication.keyWindow;
268 [panel beginSheetModalForWindow:window
269 completionHandler:^(NSInteger result) {
270 if (result != NSFileHandlingPanelOKButton)
274 [mapping writeToURL:panel.URL error:&error];
276 [window presentError:error
277 modalForWindow:window
279 didPresentSelector:nil
285 - (IBAction)mappingPressed:(id)sender {
286 [popover showRelativeToRect:popoverActivate.bounds
287 ofView:popoverActivate
288 preferredEdge:NSMinXEdge];
291 - (void)popoverWillShow:(NSNotification *)notification {
292 popoverActivate.state = NSOnState;
295 - (void)popoverWillClose:(NSNotification *)notification {
296 popoverActivate.state = NSOffState;
299 - (IBAction)moveUpPressed:(id)sender {
300 if ([_mappings moveFirstwards:_currentMapping upTo:1])
301 [self mappingsChanged];
304 - (IBAction)moveDownPressed:(id)sender {
305 if ([_mappings moveLastwards:_currentMapping])
306 [self mappingsChanged];
309 - (BOOL)tableView:(NSTableView *)tableView_
310 acceptDrop:(id <NSDraggingInfo>)info
312 dropOperation:(NSTableViewDropOperation)dropOperation {
313 NSPasteboard *pboard = [info draggingPasteboard];
314 if ([pboard.types containsObject:PB_ROW]) {
315 NSString *value = [pboard stringForType:PB_ROW];
316 NSUInteger srcRow = [value intValue];
317 [_mappings moveObjectAtIndex:srcRow toIndex:row];
318 [self mappingsChanged];
320 } else if ([pboard.types containsObject:NSURLPboardType]) {
321 NSURL *url = [NSURL URLFromPasteboard:pboard];
323 NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url
327 [tableView_ presentError:error];
330 [_mappings insertObject:mapping atIndex:row];
331 [self mappingsChanged];
339 - (NSDragOperation)tableView:(NSTableView *)tableView_
340 validateDrop:(id <NSDraggingInfo>)info
341 proposedRow:(NSInteger)row
342 proposedDropOperation:(NSTableViewDropOperation)dropOperation {
343 NSPasteboard *pboard = [info draggingPasteboard];
344 if ([pboard.types containsObject:PB_ROW]) {
345 [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove];
346 return NSDragOperationMove;
347 } else if ([pboard.types containsObject:NSURLPboardType]) {
348 NSURL *url = [NSURL URLFromPasteboard:pboard];
349 if ([url.pathExtension isEqualToString:@"enjoyable"]) {
350 [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove];
351 return NSDragOperationCopy;
353 return NSDragOperationNone;
356 return NSDragOperationNone;
360 - (NSArray *)tableView:(NSTableView *)tableView_
361 namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
362 forDraggedRowsWithIndexes:(NSIndexSet *)indexSet {
363 NJMapping *toSave = self[indexSet.firstIndex];
364 NSString *filename = [[toSave.name stringByFixingPathComponent]
365 stringByAppendingPathExtension:@"enjoyable"];
366 NSURL *dst = [dropDestination URLByAppendingPathComponent:filename];
367 dst = [NSFileManager.defaultManager generateUniqueURLWithBase:dst];
369 if (![toSave writeToURL:dst error:&error]) {
370 [tableView_ presentError:error];
373 return @[dst.lastPathComponent];
377 - (BOOL)tableView:(NSTableView *)tableView_
378 writeRowsWithIndexes:(NSIndexSet *)rowIndexes
379 toPasteboard:(NSPasteboard *)pboard {
380 if (rowIndexes.count == 1 && rowIndexes.firstIndex != 0) {
381 [pboard declareTypes:@[PB_ROW, NSFilesPromisePboardType] owner:nil];
382 [pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW];
383 [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];
385 } else if (rowIndexes.count == 1 && rowIndexes.firstIndex == 0) {
386 [pboard declareTypes:@[NSFilesPromisePboardType] owner:nil];
387 [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];