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 |
|
|
Disable buttons on empty selection |
36 |
|
|
|
37 |
|
|
*/ |
38 |
|
|
|
39 |
asvitkine |
1.1 |
@implementation VMListController |
40 |
|
|
|
41 |
|
|
+ (id) sharedInstance |
42 |
|
|
{ |
43 |
|
|
static VMListController *_sharedInstance = nil; |
44 |
|
|
if (!_sharedInstance) { |
45 |
|
|
_sharedInstance = [[VMListController allocWithZone:[self zone]] init]; |
46 |
|
|
} |
47 |
|
|
return _sharedInstance; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
- (id) init |
51 |
|
|
{ |
52 |
|
|
self = [super initWithWindowNibName:@"VMListWindow"]; |
53 |
|
|
|
54 |
|
|
NSArray *vms = [[NSUserDefaults standardUserDefaults] stringArrayForKey:@"vm_list"]; |
55 |
|
|
vmArray = [[NSMutableArray alloc] initWithCapacity:[vms count]]; |
56 |
|
|
[vmArray addObjectsFromArray:vms]; |
57 |
|
|
|
58 |
|
|
return self; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
- (void) awakeFromNib |
62 |
|
|
{ |
63 |
asvitkine |
1.2 |
[vmList setDataSource: self]; |
64 |
asvitkine |
1.4 |
[vmList setDelegate: self]; |
65 |
asvitkine |
1.2 |
[vmList reloadData]; |
66 |
asvitkine |
1.1 |
} |
67 |
|
|
|
68 |
asvitkine |
1.4 |
- (void) keyDown: (NSEvent *) event |
69 |
|
|
{ |
70 |
|
|
if ([event type] == NSKeyDown && [[event characters] length] > 0) { |
71 |
|
|
unichar key = [[event characters] characterAtIndex:0]; |
72 |
|
|
if (key == NSDeleteFunctionKey || key == NSDeleteCharacter) { |
73 |
|
|
[self deleteVirtualMachine:self]; |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
|
78 |
asvitkine |
1.1 |
- (int) numberOfRowsInTableView: (NSTableView *) table |
79 |
|
|
{ |
80 |
asvitkine |
1.2 |
return [vmArray count]; |
81 |
asvitkine |
1.1 |
} |
82 |
|
|
|
83 |
|
|
- (id) tableView: (NSTableView *) table objectValueForTableColumn: (NSTableColumn *) c row: (int) r |
84 |
|
|
{ |
85 |
asvitkine |
1.2 |
return [vmArray objectAtIndex: r]; // [[vmArray objectAtIndex: r] lastPathComponent]; |
86 |
asvitkine |
1.1 |
} |
87 |
|
|
|
88 |
|
|
//- (NSString *) tableView: (NSTableView *) table toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect |
89 |
|
|
// tableColumn: (NSTableColumn *) c row: (int) r mouseLocation: (NSPoint) loc |
90 |
|
|
//{ |
91 |
|
|
// return [vmArray objectAtIndex: r]; |
92 |
|
|
//} |
93 |
|
|
|
94 |
|
|
- (IBAction) newVirtualMachine: (id) sender |
95 |
|
|
{ |
96 |
|
|
NSSavePanel *save = [NSSavePanel savePanel]; |
97 |
|
|
[save setMessage: @"New SheepShaver Virtual Machine:"]; |
98 |
|
|
[save setRequiredFileType: @"sheepvm"]; |
99 |
|
|
[save setCanSelectHiddenExtension: YES]; |
100 |
|
|
[save setExtensionHidden: NO]; |
101 |
|
|
[save beginSheetForDirectory: nil |
102 |
|
|
file: @"New.sheepvm" |
103 |
|
|
modalForWindow: [self window] |
104 |
|
|
modalDelegate: self |
105 |
|
|
didEndSelector: @selector(_newVirtualMachineDone: returnCode: contextInfo:) |
106 |
|
|
contextInfo: nil]; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
- (IBAction) _newVirtualMachineDone: (NSSavePanel *) save returnCode: (int) returnCode contextInfo: (void *) contextInfo |
110 |
|
|
{ |
111 |
|
|
if (returnCode == NSOKButton) { |
112 |
asvitkine |
1.3 |
NSFileManager *manager = [NSFileManager defaultManager]; |
113 |
|
|
[manager createDirectoryAtPath:[save filename] attributes:nil]; |
114 |
|
|
[manager createFileAtPath:[[save filename] stringByAppendingPathComponent:@"prefs"] contents:nil attributes:nil]; |
115 |
|
|
[vmArray addObject:[save filename]]; |
116 |
|
|
[vmList reloadData]; |
117 |
|
|
[[NSUserDefaults standardUserDefaults] setObject:vmArray forKey:@"vm_list"]; |
118 |
|
|
[vmList selectRow:([vmArray count] - 1) byExtendingSelection:NO]; |
119 |
|
|
[self editVirtualMachineSettings:self]; |
120 |
|
|
if ([[VMSettingsController sharedInstance] cancelWasClicked]) { |
121 |
|
|
[manager removeFileAtPath:[save filename] handler:nil]; |
122 |
|
|
[vmArray removeObjectAtIndex:([vmArray count] - 1)]; |
123 |
|
|
[vmList reloadData]; |
124 |
|
|
} |
125 |
|
|
// TODO advanced: show sub-panel in save dialog that says "Create Disk:" |
126 |
asvitkine |
1.1 |
} |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
- (IBAction) importVirtualMachine: (id) sender |
130 |
|
|
{ |
131 |
|
|
NSOpenPanel *open = [NSOpenPanel openPanel]; |
132 |
|
|
[open setMessage:@"Import SheepShaver Virtual Machine:"]; |
133 |
|
|
[open setResolvesAliases:YES]; |
134 |
|
|
// Curiously, bundles are treated as "files" not "directories" by NSOpenPanel. |
135 |
|
|
[open setCanChooseDirectories:NO]; |
136 |
|
|
[open setCanChooseFiles:YES]; |
137 |
|
|
[open setAllowsMultipleSelection:NO]; |
138 |
|
|
[open beginSheetForDirectory: nil |
139 |
|
|
file: nil |
140 |
|
|
types: [NSArray arrayWithObject:@"sheepvm"] |
141 |
|
|
modalForWindow: [self window] |
142 |
|
|
modalDelegate: self |
143 |
|
|
didEndSelector: @selector(_importVirtualMachineDone: returnCode: contextInfo:) |
144 |
|
|
contextInfo: nil]; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
- (void) _importVirtualMachineDone: (NSOpenPanel *) open returnCode: (int) returnCode contextInfo: (void *) contextInfo |
148 |
|
|
{ |
149 |
|
|
if (returnCode == NSOKButton) { |
150 |
|
|
[vmArray addObject:[open filename]]; |
151 |
|
|
[vmList reloadData]; |
152 |
|
|
[[NSUserDefaults standardUserDefaults] setObject:vmArray forKey:@"vm_list"]; |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
|
156 |
asvitkine |
1.4 |
- (IBAction) editVirtualMachineSettings: (id) sender |
157 |
asvitkine |
1.1 |
{ |
158 |
|
|
int selectedRow = [vmList selectedRow]; |
159 |
asvitkine |
1.2 |
if (selectedRow >= 0) { |
160 |
asvitkine |
1.1 |
[[VMSettingsController sharedInstance] editSettingsFor:[vmArray objectAtIndex:selectedRow] sender:sender]; |
161 |
asvitkine |
1.2 |
} |
162 |
asvitkine |
1.1 |
} |
163 |
|
|
|
164 |
asvitkine |
1.4 |
- (IBAction) launchVirtualMachine: (id) sender |
165 |
asvitkine |
1.1 |
{ |
166 |
asvitkine |
1.2 |
int selectedRow = [vmList selectedRow]; |
167 |
|
|
if (selectedRow >= 0) { |
168 |
|
|
NSTask *sheep = [[NSTask alloc] init]; |
169 |
|
|
[sheep setLaunchPath:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"SheepShaver"]]; |
170 |
|
|
[sheep setArguments:[NSArray arrayWithObject:[vmArray objectAtIndex:selectedRow]]]; |
171 |
|
|
[sheep launch]; |
172 |
|
|
} |
173 |
asvitkine |
1.1 |
} |
174 |
|
|
|
175 |
asvitkine |
1.4 |
- (IBAction) deleteVirtualMachine: (id) sender |
176 |
|
|
{ |
177 |
|
|
int selectedRow = [vmList selectedRow]; |
178 |
|
|
if (selectedRow >= 0) { |
179 |
|
|
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; |
180 |
|
|
[alert setMessageText:@"Do you wish to remove the selected virtual machine from the list?"]; |
181 |
|
|
[alert addButtonWithTitle:@"Remove"]; |
182 |
|
|
[alert addButtonWithTitle:@"Cancel"]; |
183 |
|
|
[alert setAlertStyle:NSWarningAlertStyle]; |
184 |
|
|
[alert beginSheetModalForWindow:[self window] |
185 |
|
|
modalDelegate:self |
186 |
|
|
didEndSelector:@selector(_deleteVirtualMachineDone: returnCode: contextInfo:) |
187 |
|
|
contextInfo:nil]; |
188 |
|
|
|
189 |
|
|
} |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
- (void) _deleteVirtualMachineDone: (NSAlert *) alert returnCode: (int) returnCode contextInfo: (void *) contextInfo |
193 |
|
|
{ |
194 |
|
|
if (returnCode == NSAlertFirstButtonReturn) { |
195 |
|
|
[vmArray removeObjectAtIndex:[vmList selectedRow]]; |
196 |
|
|
[vmList deselectAll:self]; |
197 |
|
|
[vmList reloadData]; |
198 |
|
|
[[NSUserDefaults standardUserDefaults] setObject:vmArray forKey:@"vm_list"]; |
199 |
|
|
} |
200 |
|
|
} |
201 |
|
|
|
202 |
asvitkine |
1.1 |
@end |