2 // NJMappingsController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "NJMappingsController.h"
11 #import "NJMappingsController.h"
13 #import "NJOutputController.h"
16 #define PB_ROW @"com.yukkurigames.Enjoyable.MappingRow"
18 @implementation NJMappingsController {
19 NSMutableArray *_mappings;
20 NJMapping *manualMapping;
21 NSString *draggingName;
25 if ((self = [super init])) {
26 _mappings = [[NSMutableArray alloc] init];
27 _currentMapping = [[NJMapping alloc] initWithName:@"(default)"];
28 manualMapping = _currentMapping;
29 [_mappings addObject:_currentMapping];
34 - (void)awakeFromNib {
35 [tableView registerForDraggedTypes:@[PB_ROW, NSURLPboardType]];
36 [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
39 - (NJMapping *)objectForKeyedSubscript:(NSString *)name {
40 for (NJMapping *mapping in _mappings)
41 if ([name isEqualToString:mapping.name])
46 - (NJMapping *)objectAtIndexedSubscript:(NSUInteger)idx {
47 return idx < _mappings.count ? _mappings[idx] : nil;
50 - (void)mappingsChanged {
52 [tableView reloadData];
53 popoverActivate.title = _currentMapping.name;
54 [self updateInterfaceForCurrentMapping];
55 [NSNotificationCenter.defaultCenter
56 postNotificationName:NJEventMappingListChanged
60 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
61 objects:(__unsafe_unretained id [])buffer
62 count:(NSUInteger)len {
63 return [_mappings countByEnumeratingWithState:state
68 - (void)activateMappingForProcess:(NSRunningApplication *)app {
69 NJMapping *oldMapping = manualMapping;
70 NSArray *names = app.possibleMappingNames;
72 for (NSString *name in names) {
73 NJMapping *mapping = self[name];
75 [self activateMapping:self[name]];
82 [self activateMapping:oldMapping];
83 if ([oldMapping.name.lowercaseString isEqualToString:@"@application"]) {
84 oldMapping.name = app.bestMappingName;
85 [self mappingsChanged];
88 manualMapping = oldMapping;
91 - (void)updateInterfaceForCurrentMapping {
92 NSUInteger selected = [_mappings indexOfObject:_currentMapping];
93 [removeButton setEnabled:selected != 0];
94 [moveDown setEnabled:selected && selected != _mappings.count - 1];
95 [moveUp setEnabled:selected > 1];
96 popoverActivate.title = _currentMapping.name;
97 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selected] byExtendingSelection:NO];
98 [NSUserDefaults.standardUserDefaults setInteger:selected forKey:@"selected"];
101 - (void)activateMapping:(NJMapping *)mapping {
103 mapping = manualMapping;
104 if (mapping == _currentMapping)
106 NSLog(@"Switching to mapping %@.", mapping.name);
107 manualMapping = mapping;
108 _currentMapping = mapping;
109 [self updateInterfaceForCurrentMapping];
110 [outputController loadCurrent];
111 [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
112 object:_currentMapping];
115 - (IBAction)addPressed:(id)sender {
116 NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"];
117 [_mappings addObject:newMapping];
118 [self activateMapping:newMapping];
119 [self mappingsChanged];
120 [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
123 - (IBAction)removePressed:(id)sender {
124 if (tableView.selectedRow == 0)
127 NSInteger selectedRow = tableView.selectedRow;
128 [_mappings removeObjectAtIndex:selectedRow];
129 [self activateMapping:_mappings[MIN(selectedRow, _mappings.count - 1)]];
130 [self mappingsChanged];
133 - (void)tableViewSelectionDidChange:(NSNotification *)notify {
134 [self activateMapping:self[tableView.selectedRow]];
137 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
138 return self[index].name;
141 - (void)tableView:(NSTableView *)view
142 setObjectValue:(NSString *)obj
143 forTableColumn:(NSTableColumn *)col
144 row:(NSInteger)index {
145 self[index].name = obj;
146 [self mappingsChanged];
149 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
150 return _mappings.count;
153 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
158 NSLog(@"Saving mappings to defaults.");
159 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count];
160 for (NJMapping *mapping in _mappings)
161 [ary addObject:[mapping serialize]];
162 [NSUserDefaults.standardUserDefaults setObject:ary forKey:@"mappings"];
166 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
167 NSArray *mappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
168 [self loadAllFrom:mappings andActivate:selected];
171 - (void)loadAllFrom:(NSArray *)storedMappings andActivate:(NSUInteger)selected {
172 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
174 // have to do two passes in case mapping1 refers to mapping2 via a NJOutputMapping
175 for (NSDictionary *storedMapping in storedMappings) {
176 NJMapping *mapping = [[NJMapping alloc] initWithName:storedMapping[@"name"]];
177 [newMappings addObject:mapping];
180 for (unsigned i = 0; i < storedMappings.count; ++i) {
181 NSDictionary *entries = storedMappings[i][@"entries"];
182 NJMapping *mapping = newMappings[i];
183 for (id key in entries) {
184 NJOutput *output = [NJOutput outputDeserialize:entries[key]
185 withMappings:newMappings];
187 mapping.entries[key] = output;
191 if (newMappings.count) {
192 _mappings = newMappings;
193 if (selected >= newMappings.count)
195 [self activateMapping:_mappings[selected]];
196 [self mappingsChanged];
200 - (NJMapping *)mappingWithURL:(NSURL *)url error:(NSError **)error {
201 NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
203 NSDictionary *serialization = !*error
204 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
208 if (!([serialization isKindOfClass:NSDictionary.class]
209 && [serialization[@"name"] isKindOfClass:NSString.class]
210 && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
211 *error = [NSError errorWithDomain:@"Enjoyable"
213 description:@"This isn't a valid mapping file."];
217 NSDictionary *entries = serialization[@"entries"];
218 NJMapping *mapping = [[NJMapping alloc] initWithName:serialization[@"name"]];
219 for (id key in entries) {
220 NSDictionary *value = entries[key];
221 if ([key isKindOfClass:NSString.class]) {
222 NJOutput *output = [NJOutput outputDeserialize:value
223 withMappings:_mappings];
225 mapping.entries[key] = output;
231 - (void)addMappingWithContentsOfURL:(NSURL *)url {
232 NSWindow *window = popoverActivate.window;
234 NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url
238 if (mapping && !error) {
240 NJMapping *mergeInto = self[mapping.name];
241 for (id key in mapping.entries) {
242 if (mergeInto.entries[key]
243 && ![mergeInto.entries[key] isEqual:mapping.entries[key]]) {
250 NSAlert *conflictAlert = [[NSAlert alloc] init];
251 conflictAlert.messageText = @"Replace existing mappings?";
252 conflictAlert.informativeText =
253 [NSString stringWithFormat:
254 @"This file contains inputs you've already mapped in \"%@\". Do you "
255 @"want to merge them and replace your existing mappings, or import this "
256 @"as a separate mapping?", mapping.name];
257 [conflictAlert addButtonWithTitle:@"Merge"];
258 [conflictAlert addButtonWithTitle:@"Cancel"];
259 [conflictAlert addButtonWithTitle:@"New Mapping"];
260 NSInteger res = [conflictAlert runModal];
261 if (res == NSAlertSecondButtonReturn)
263 else if (res == NSAlertThirdButtonReturn)
268 [mergeInto.entries addEntriesFromDictionary:mapping.entries];
271 [_mappings addObject:mapping];
274 [self activateMapping:mapping];
275 [self mappingsChanged];
277 if (conflict && !mergeInto) {
278 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
279 [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
284 [window presentError:error
285 modalForWindow:window
287 didPresentSelector:nil
292 - (void)importPressed:(id)sender {
293 NSOpenPanel *panel = [NSOpenPanel openPanel];
294 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
295 NSWindow *window = NSApplication.sharedApplication.keyWindow;
296 [panel beginSheetModalForWindow:window
297 completionHandler:^(NSInteger result) {
298 if (result != NSFileHandlingPanelOKButton)
301 [self addMappingWithContentsOfURL:panel.URL];
306 - (void)exportPressed:(id)sender {
307 NSSavePanel *panel = [NSSavePanel savePanel];
308 panel.allowedFileTypes = @[ @"enjoyable" ];
309 NJMapping *mapping = _currentMapping;
310 panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent];
311 NSWindow *window = NSApplication.sharedApplication.keyWindow;
312 [panel beginSheetModalForWindow:window
313 completionHandler:^(NSInteger result) {
314 if (result != NSFileHandlingPanelOKButton)
318 [mapping writeToURL:panel.URL error:&error];
320 [window presentError:error
321 modalForWindow:window
323 didPresentSelector:nil
329 - (IBAction)mappingPressed:(id)sender {
330 [popover showRelativeToRect:popoverActivate.bounds
331 ofView:popoverActivate
332 preferredEdge:NSMinXEdge];
335 - (void)popoverWillShow:(NSNotification *)notification {
336 popoverActivate.state = NSOnState;
339 - (void)popoverWillClose:(NSNotification *)notification {
340 popoverActivate.state = NSOffState;
343 - (IBAction)moveUpPressed:(id)sender {
344 NSUInteger idx = [_mappings indexOfObject:_currentMapping];
345 if (idx > 1 && idx != NSNotFound) {
346 [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1];
347 [self mappingsChanged];
351 - (IBAction)moveDownPressed:(id)sender {
352 NSUInteger idx = [_mappings indexOfObject:_currentMapping];
353 if (idx < _mappings.count - 1) {
354 [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1];
355 [self mappingsChanged];
359 - (BOOL)tableView:(NSTableView *)tableView_
360 acceptDrop:(id <NSDraggingInfo>)info
362 dropOperation:(NSTableViewDropOperation)dropOperation {
363 NSPasteboard *pboard = [info draggingPasteboard];
364 if ([pboard.types containsObject:PB_ROW]) {
365 NSString *value = [pboard stringForType:PB_ROW];
366 NSUInteger srcRow = [value intValue];
367 [_mappings moveObjectAtIndex:srcRow toIndex:row];
368 [self mappingsChanged];
370 } else if ([pboard.types containsObject:NSURLPboardType]) {
371 NSURL *url = [NSURL URLFromPasteboard:pboard];
373 NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url
377 [tableView_ presentError:error];
380 [_mappings insertObject:mapping atIndex:row];
381 [self mappingsChanged];
389 - (NSDragOperation)tableView:(NSTableView *)tableView_
390 validateDrop:(id <NSDraggingInfo>)info
391 proposedRow:(NSInteger)row
392 proposedDropOperation:(NSTableViewDropOperation)dropOperation {
393 NSPasteboard *pboard = [info draggingPasteboard];
394 if ([pboard.types containsObject:PB_ROW]) {
395 [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove];
396 return NSDragOperationMove;
397 } else if ([pboard.types containsObject:NSURLPboardType]) {
398 NSURL *url = [NSURL URLFromPasteboard:pboard];
399 if ([url.pathExtension isEqualToString:@"enjoyable"]) {
400 [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove];
401 return NSDragOperationCopy;
403 return NSDragOperationNone;
406 return NSDragOperationNone;
410 - (NSArray *)tableView:(NSTableView *)tableView_
411 namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
412 forDraggedRowsWithIndexes:(NSIndexSet *)indexSet {
413 NJMapping *toSave = self[indexSet.firstIndex];
414 NSString *filename = [[toSave.name stringByFixingPathComponent]
415 stringByAppendingPathExtension:@"enjoyable"];
416 NSURL *dst = [dropDestination URLByAppendingPathComponent:filename];
417 dst = [NSFileManager.defaultManager generateUniqueURLWithBase:dst];
419 if (![toSave writeToURL:dst error:&error]) {
420 [tableView_ presentError:error];
423 return @[dst.lastPathComponent];
427 - (BOOL)tableView:(NSTableView *)tableView_
428 writeRowsWithIndexes:(NSIndexSet *)rowIndexes
429 toPasteboard:(NSPasteboard *)pboard {
430 if (rowIndexes.count == 1 && rowIndexes.firstIndex != 0) {
431 [pboard declareTypes:@[PB_ROW, NSFilesPromisePboardType] owner:nil];
432 [pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW];
433 [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];
435 } else if (rowIndexes.count == 1 && rowIndexes.firstIndex == 0) {
436 [pboard declareTypes:@[NSFilesPromisePboardType] owner:nil];
437 [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];