1 |
asvitkine |
1.1 |
/* |
2 |
|
|
* VMListController.mm - SheepShaver VM manager in Cocoa on Mac OS X |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2009 Alexei Svitkine |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#import "VMListController.h" |
22 |
|
|
#import "VMSettingsController.h" |
23 |
|
|
|
24 |
asvitkine |
1.4 |
/* |
25 |
|
|
|
26 |
|
|
TODO: |
27 |
|
|
|
28 |
|
|
Verify if VM exists |
29 |
|
|
Create Disk on New VM |
30 |
|
|
Keep track of VMs that are running and disallow editing settings, re-launching, etc.. |
31 |
|
|
Drag-drop to re-arrange order of VMs |
32 |
|
|
Drag VM from Finder to import |
33 |
|
|
Don't show Preferences menu in spawned SheepShaver instances - or make them |
34 |
|
|
use the same nib file as this app! |
35 |
|
|
|
36 |
|
|
*/ |
37 |
|
|
|
38 |
asvitkine |
1.1 |
@implementation VMListController |
39 |
|
|
|
40 |
|
|
+ (id) sharedInstance |
41 |
|
|
{ |
42 |
|
|
static VMListController *_sharedInstance = nil; |
43 |
|
|
if (!_sharedInstance) { |
44 |
|
|
_sharedInstance = [[VMListController allocWithZone:[self zone]] init]; |
45 |
|
|
} |
46 |
|
|
return _sharedInstance; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
- (id) init |
50 |
|
|
{ |
51 |
|
|
self = [super initWithWindowNibName:@"VMListWindow"]; |
52 |
|
|
|
53 |
|
|
NSArray *vms = [[NSUserDefaults standardUserDefaults] stringArrayForKey:@"vm_list"]; |
54 |
|
|
vmArray = [[NSMutableArray alloc] initWithCapacity:[vms count]]; |
55 |
|
|
[vmArray addObjectsFromArray:vms]; |
56 |
|
|
|
57 |
|
|
return self; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
- (void) awakeFromNib |
61 |
|
|
{ |
62 |
asvitkine |
1.2 |
[vmList setDataSource: self]; |
63 |
asvitkine |
1.4 |
[vmList setDelegate: self]; |
64 |
asvitkine |
1.2 |
[vmList reloadData]; |
65 |
asvitkine |
1.1 |
} |
66 |
|
|
|
67 |
asvitkine |
1.4 |
- (void) keyDown: (NSEvent *) event |
68 |
|
|
{ |
69 |
|
|
if ([event type] == NSKeyDown && [[event characters] length] > 0) { |
70 |
|
|
unichar key = [[event characters] characterAtIndex:0]; |
71 |
|
|
if (key == NSDeleteFunctionKey || key == NSDeleteCharacter) { |
72 |
|
|
[self deleteVirtualMachine:self]; |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
asvitkine |
1.1 |
- (int) numberOfRowsInTableView: (NSTableView *) table |
78 |
|
|
{ |
79 |
asvitkine |
1.2 |
return [vmArray count]; |
80 |
asvitkine |
1.1 |
} |
81 |
|
|
|
82 |
|
|
- (id) tableView: (NSTableView *) table objectValueForTableColumn: (NSTableColumn *) c row: (int) r |
83 |
|
|
{ |
84 |
asvitkine |
1.2 |
return [vmArray objectAtIndex: r]; // [[vmArray objectAtIndex: r] lastPathComponent]; |
85 |
asvitkine |
1.1 |
} |
86 |
|
|
|
87 |
asvitkine |
1.5 |
- (void) tableViewSelectionDidChange: (NSNotification *) notification |
88 |
|
|
{ |
89 |
|
|
if ([vmList selectedRow] >= 0) { |
90 |
|
|
[settingsButton setEnabled:YES]; |
91 |
|
|
[launchButton setEnabled:YES]; |
92 |
|
|
} else { |
93 |
|
|
[settingsButton setEnabled:NO]; |
94 |
|
|
[launchButton setEnabled:NO]; |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
asvitkine |
1.1 |
//- (NSString *) tableView: (NSTableView *) table toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect |
99 |
|
|
// tableColumn: (NSTableColumn *) c row: (int) r mouseLocation: (NSPoint) loc |
100 |
|
|
//{ |
101 |
|
|
// return [vmArray objectAtIndex: r]; |
102 |
|
|
//} |
103 |
|
|
|
104 |
|
|
- (IBAction) newVirtualMachine: (id) sender |
105 |
|
|
{ |
106 |
|
|
NSSavePanel *save = [NSSavePanel savePanel]; |
107 |
|
|
[save setMessage: @"New SheepShaver Virtual Machine:"]; |
108 |
|
|
[save setRequiredFileType: @"sheepvm"]; |
109 |
|
|
[save setCanSelectHiddenExtension: YES]; |
110 |
|
|
[save setExtensionHidden: NO]; |
111 |
|
|
[save beginSheetForDirectory: nil |
112 |
|
|
file: @"New.sheepvm" |
113 |
|
|
modalForWindow: [self window] |
114 |
|
|
modalDelegate: self |
115 |
|
|
didEndSelector: @selector(_newVirtualMachineDone: returnCode: contextInfo:) |
116 |
|
|
contextInfo: nil]; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
- (IBAction) _newVirtualMachineDone: (NSSavePanel *) save returnCode: (int) returnCode contextInfo: (void *) contextInfo |
120 |
|
|
{ |
121 |
|
|
if (returnCode == NSOKButton) { |
122 |
asvitkine |
1.3 |
NSFileManager *manager = [NSFileManager defaultManager]; |
123 |
|
|
[manager createDirectoryAtPath:[save filename] attributes:nil]; |
124 |
|
|
[manager createFileAtPath:[[save filename] stringByAppendingPathComponent:@"prefs"] contents:nil attributes:nil]; |
125 |
|
|
[vmArray addObject:[save filename]]; |
126 |
|
|
[vmList reloadData]; |
127 |
|
|
[[NSUserDefaults standardUserDefaults] setObject:vmArray forKey:@"vm_list"]; |
128 |
|
|
[vmList selectRow:([vmArray count] - 1) byExtendingSelection:NO]; |
129 |
|
|
[self editVirtualMachineSettings:self]; |
130 |
|
|
if ([[VMSettingsController sharedInstance] cancelWasClicked]) { |
131 |
|
|
[manager removeFileAtPath:[save filename] handler:nil]; |
132 |
|
|
[vmArray removeObjectAtIndex:([vmArray count] - 1)]; |
133 |
|
|
[vmList reloadData]; |
134 |
|
|
} |
135 |
|
|
// TODO advanced: show sub-panel in save dialog that says "Create Disk:" |
136 |
asvitkine |
1.1 |
} |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
- (IBAction) importVirtualMachine: (id) sender |
140 |
|
|
{ |
141 |
|
|
NSOpenPanel *open = [NSOpenPanel openPanel]; |
142 |
|
|
[open setMessage:@"Import SheepShaver Virtual Machine:"]; |
143 |
|
|
[open setResolvesAliases:YES]; |
144 |
|
|
// Curiously, bundles are treated as "files" not "directories" by NSOpenPanel. |
145 |
|
|
[open setCanChooseDirectories:NO]; |
146 |
|
|
[open setCanChooseFiles:YES]; |
147 |
|
|
[open setAllowsMultipleSelection:NO]; |
148 |
|
|
[open beginSheetForDirectory: nil |
149 |
|
|
file: nil |
150 |
|
|
types: [NSArray arrayWithObject:@"sheepvm"] |
151 |
|
|
modalForWindow: [self window] |
152 |
|
|
modalDelegate: self |
153 |
|
|
didEndSelector: @selector(_importVirtualMachineDone: returnCode: contextInfo:) |
154 |
|
|
contextInfo: nil]; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
- (void) _importVirtualMachineDone: (NSOpenPanel *) open returnCode: (int) returnCode contextInfo: (void *) contextInfo |
158 |
|
|
{ |
159 |
|
|
if (returnCode == NSOKButton) { |
160 |
|
|
[vmArray addObject:[open filename]]; |
161 |
|
|
[vmList reloadData]; |
162 |
|
|
[[NSUserDefaults standardUserDefaults] setObject:vmArray forKey:@"vm_list"]; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
asvitkine |
1.4 |
- (IBAction) editVirtualMachineSettings: (id) sender |
167 |
asvitkine |
1.1 |
{ |
168 |
|
|
int selectedRow = [vmList selectedRow]; |
169 |
asvitkine |
1.2 |
if (selectedRow >= 0) { |
170 |
asvitkine |
1.1 |
[[VMSettingsController sharedInstance] editSettingsFor:[vmArray objectAtIndex:selectedRow] sender:sender]; |
171 |
asvitkine |
1.2 |
} |
172 |
asvitkine |
1.1 |
} |
173 |
|
|
|
174 |
asvitkine |
1.4 |
- (IBAction) launchVirtualMachine: (id) sender |
175 |
asvitkine |
1.1 |
{ |
176 |
asvitkine |
1.2 |
int selectedRow = [vmList selectedRow]; |
177 |
|
|
if (selectedRow >= 0) { |
178 |
|
|
NSTask *sheep = [[NSTask alloc] init]; |
179 |
|
|
[sheep setLaunchPath:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"SheepShaver"]]; |
180 |
|
|
[sheep setArguments:[NSArray arrayWithObject:[vmArray objectAtIndex:selectedRow]]]; |
181 |
|
|
[sheep launch]; |
182 |
|
|
} |
183 |
asvitkine |
1.1 |
} |
184 |
|
|
|
185 |
asvitkine |
1.4 |
- (IBAction) deleteVirtualMachine: (id) sender |
186 |
|
|
{ |
187 |
|
|
int selectedRow = [vmList selectedRow]; |
188 |
|
|
if (selectedRow >= 0) { |
189 |
|
|
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
190 |
|
|
[alert setMessageText:@"Do you wish to remove the selected virtual machine from the list?"]; |
191 |
|
|
[alert addButtonWithTitle:@"Remove"]; |
192 |
|
|
[alert addButtonWithTitle:@"Cancel"]; |
193 |
|
|
[alert setAlertStyle:NSWarningAlertStyle]; |
194 |
|
|
[alert beginSheetModalForWindow:[self window] |
195 |
|
|
modalDelegate:self |
196 |
|
|
didEndSelector:@selector(_deleteVirtualMachineDone: returnCode: contextInfo:) |
197 |
|
|
contextInfo:nil]; |
198 |
|
|
|
199 |
|
|
} |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
- (void) _deleteVirtualMachineDone: (NSAlert *) alert returnCode: (int) returnCode contextInfo: (void *) contextInfo |
203 |
|
|
{ |
204 |
|
|
if (returnCode == NSAlertFirstButtonReturn) { |
205 |
|
|
[vmArray removeObjectAtIndex:[vmList selectedRow]]; |
206 |
|
|
[vmList deselectAll:self]; |
207 |
|
|
[vmList reloadData]; |
208 |
|
|
[[NSUserDefaults standardUserDefaults] setObject:vmArray forKey:@"vm_list"]; |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
asvitkine |
1.1 |
@end |