aboutsummaryrefslogtreecommitdiffstats
path: root/PreferencesController.m
blob: cba62419f35a2d3d1e1b812ff3b745a1caa42249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#import "PreferencesController.h"

@implementation PreferencesController
+ (PreferencesController *)sharedController
{
    static PreferencesController *sharedController = nil;
    
    if (sharedController == nil) {
        sharedController = [[PreferencesController alloc] init];
    }
    return sharedController;
}

- (id)init
{
    self = [self initWithWindowNibName: @"Preferences"];
    [self setWindowFrameAutosaveName: @"preferencesWindow"];
    return self;
}

- (IBAction)addStartupWorld: (id)sender
{
    NSOpenPanel *openPanel;
    
    openPanel = [NSOpenPanel openPanel];
    [openPanel setAllowsMultipleSelection: YES];
    [openPanel beginSheetForDirectory: nil
                                 file: nil
                                types: [NSArray arrayWithObject: @"moxie"]
                       modalForWindow: [self window]
                        modalDelegate: self
                       didEndSelector: @selector(openPanelDidEnd:returnCode:contextInfo:)
                          contextInfo: NULL];
}

- (void)openPanelDidEnd: (NSOpenPanel *)sheet
             returnCode: (int)returnCode
            contextInfo: (void *)contextInfo
{
    if (returnCode == NSOKButton) {
        NSEnumerator *pathEnum;
        NSMutableArray *newContents;
        NSString *path;
        
        newContents = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] startupWorlds]];
        pathEnum = [[sheet filenames] objectEnumerator];
        while ((path = [pathEnum nextObject]) != nil) {
            [newContents addObject: path];
        }
        
        [[NSUserDefaults standardUserDefaults] setStartupWorlds: newContents];
        [self refreshStartupTableView];
    }
}

- (IBAction)removeStartupWorld: (id)sender
{
	NSIndexSet *selectedIndexes;
    NSMutableArray *newStartupItems;
    __block int removedRows;
    
    newStartupItems = [[[NSUserDefaults standardUserDefaults] startupWorlds] mutableCopy];
    removedRows = 0;
    selectedIndexes = [theStartupItemsTableView selectedRowIndexes];
	[selectedIndexes enumerateIndexesUsingBlock: ^(NSUInteger idx, BOOL *stop) {
        [newStartupItems removeObjectAtIndex: idx-removedRows];
        removedRows++;
	}];
    [[NSUserDefaults standardUserDefaults] setStartupWorlds: newStartupItems];
    [self refreshStartupTableView];
}

- (void)refreshStartupTableView
{
    NSEnumerator *startupEnum;
    NSMutableArray *newContent;
    NSString *path;
    
    newContent = [NSMutableArray array];
    startupEnum = [[[NSUserDefaults standardUserDefaults] startupWorlds] objectEnumerator];
    while ((path = [startupEnum nextObject]) != nil) {
        NSDictionary *tmpDict;
        
        tmpDict = [NSDictionary dictionaryWithObject: [[path lastPathComponent] stringByDeletingPathExtension]
                                              forKey: @"name"];
        [newContent addObject: tmpDict];
    }
    [theStartupItemsController setContent: newContent];
}

- (void)windowDidLoad
{
    NSUserDefaults *prefs;
    [super windowDidLoad];

    // Set the colors in the status panel. NSPreferencesController doesn't use accessor methods, so
    // we have to do this by hand.
    prefs = [NSUserDefaults standardUserDefaults];
    [prefs setRecentActivityColorData: [prefs recentActivityColorData]];
    [prefs setConnectedColorData: [prefs connectedColorData]];
    [prefs setDisconnectedColorData: [prefs disconnectedColorData]];
    [self refreshStartupTableView];

    [theStartupItemsTableView registerForDraggedTypes:
        [NSArray arrayWithObjects: NSFilenamesPboardType, NSURLPboardType, nil]];
}

- (NSString *)REPLFontName
{
    return [[NSUserDefaults standardUserDefaults] REPLFontName];
}
@end

@implementation PreferencesController (StartupItemTableViewDataSource)
- (NSDragOperation)tableView: (NSTableView *)aTableView
                validateDrop: (id <NSDraggingInfo>)sender
                 proposedRow: (int)i
       proposedDropOperation: (NSDragOperation)operation;
{
    NSArray *paths;
    NSPasteboard *pb;
    
    pb = [sender draggingPasteboard];
    paths = [pb propertyListForType: NSFilenamesPboardType];
    if ([[paths objectAtIndex: 0] hasSuffix: @".moxie"]) {
        [aTableView setDropRow: -1 dropOperation: NSTableViewDropOn];
        return NSDragOperationLink;
    }
    return NSDragOperationNone;
}

- (BOOL)tableView: (NSTableView *)aTableView
       acceptDrop: (id <NSDraggingInfo>)sender
              row: (int)anIndex
    dropOperation: (NSTableViewDropOperation)anOperation
{
    NSEnumerator *pathEnum;
    NSMutableArray *newContents;
    NSPasteboard *pb;
    NSString *path;
    
    newContents = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] startupWorlds]];
    pb = [sender draggingPasteboard];
    pathEnum = [[pb propertyListForType: NSFilenamesPboardType] objectEnumerator];
    while ((path = [pathEnum nextObject]) != nil) {
        if ([path hasSuffix: @".moxie"]) {
            [newContents addObject: path];
        }
    }
    
    [[NSUserDefaults standardUserDefaults] setStartupWorlds: newContents];
    [self refreshStartupTableView];
    return [newContents count] > 0;
}
@end