root/branches/0_9_1/qcontrol/cocoaControlDiskImage.m

Revision 61, 6.0 KB (checked in by mike, 3 years ago)

[new] support for VM snapshots
[new] VPC7 importer: diskimages are converted to qcow2
[new] qcow replaced by qcow2
[fix] only supported diskimages (i.e. qcow2) prompt with option for saving
[fix] updated OS X Intel Patches for QEMU 0.9.0

Line 
1/*
2 * QEMU Cocoa Control Diskimage Window
3 *
4 * Copyright (c) 2005 - 2007 Mike Kronenberg
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#import "cocoaControlDiskImage.h"
26#import "cocoaControlEditPC.h"
27
28@implementation cocoaControlDiskImage
29-(id) init
30{
31//      NSLog(@"cocoaControlEditPC: init");
32        if ((self = [super init])) {
33                [ [ NSNotificationCenter defaultCenter ] addObserver:self
34                        selector:@selector(checkATaskStatus:)
35                        name:NSTaskDidTerminateNotification
36                        object:nil ];
37                return self;
38        }
39       
40        return nil;
41}
42
43- (void) dealloc
44{
45//      NSLog(@"cocoaControlEditPC: dealloc");
46
47        [ [ NSNotificationCenter defaultCenter ] removeObserver:self ];
48        [ super dealloc ];
49}
50
51- (void) checkATaskStatus:(NSNotification *)aNotification
52{
53//      NSLog(@"cocoaControlEditPC: checkATaskStatus\n");
54
55        int status = [ [ aNotification object ] terminationStatus ];
56        if (status == 0)  {
57                [ dIProgressIndicator stopAnimation:self ];
58                [ NSApp endSheet:dIProgressPanel ];
59                [ dIWindow close ];
60                [ self release ];
61        } else
62                NSLog(@"cocoaControlDiskImage failed.");
63}
64- (id) dIWindow
65{
66        return dIWindow;
67}
68
69- (void) setQSender:(id)sender
70//      NSLog(@"cocoaControlEditPC: setQSender");
71{
72        qSender = sender;
73}
74
75- (void)dIPanelDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
76{
77//      NSLog(@"cocoaControlEditPC: dIPanelDidEnd");
78
79        [ dIWindow orderOut:self ];
80        [ dIWindow release ];
81        [ self release ];
82}
83
84- (IBAction)dIWindowClose:(id)sender
85{
86//      NSLog(@"cocoaControlDiskImage: dIWindowClose");
87       
88        if (qSender) { /* sheet for EditPCPanel */
89                [ NSApp endSheet:dIWindow ];
90        } else { /* standalone Window */
91                [ dIWindow close ];
92                [ self release ];
93        }
94}
95
96- (IBAction)dIWindowCreate:(id)sender
97{
98//      NSLog(@"cocoaControlDiskImage: dIWindowCreate");
99
100        /* check size */
101        if ([ dISize intValue ] < 1) {
102                [ dISize setTextColor: [NSColor redColor ] ];
103                return;
104        }
105       
106        if (qSender) { /* sheet for EditPCPanel */
107                [ qSender setCustomDIType:[ [ NSArray arrayWithObjects:@"raw", @"qcow2", nil ] objectAtIndex:[ dIFormat indexOfSelectedItem ] ] size:[ dISize intValue ] ];
108                [ self dIWindowClose:self];
109        } else { /* standalone Window */
110                NSSavePanel *sp = [ [ NSSavePanel alloc ] init ];
111                [ sp setRequiredFileType:[ [ NSArray arrayWithObjects:@"raw", @"qcow2", nil ] objectAtIndex:[ dIFormat indexOfSelectedItem ] ] ];
112                [ sp beginSheetForDirectory:NSHomeDirectory()
113                        file:[ NSString stringWithFormat: NSLocalizedStringFromTable(@"dIWindowCreate:file", @"Localizable", @"cocoaControlDiskImage"),[ [ NSArray arrayWithObjects:@"raw", @"qcow2", nil ] objectAtIndex:[ dIFormat indexOfSelectedItem ] ] ]
114                        modalForWindow:dIWindow
115                        modalDelegate:self
116                        didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
117                        contextInfo:nil];
118        }
119}
120
121- (void)savePanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
122{
123//      NSLog(@"cocoaControlDiskImage: savePanelDidEnd");
124
125        if(returnCode == NSOKButton)
126        {
127                dIFileName = [ [ NSString stringWithFormat:@"%@", [ sheet filename ] ] retain ];
128               
129                /* hide Save Sheet */
130                [ sheet orderOut:self ];
131               
132                /* prepare dIPanel */
133                [ dIProgressIndicator setUsesThreadedAnimation:YES ];
134                [ dIName setStringValue:[ NSString stringWithFormat: NSLocalizedStringFromTable(@"savePanelDidEnd:dIName", @"Localizable", @"cocoaControlDiskImage"), dIFileName ] ];
135               
136                /* show Progress Sheet */
137                [ NSApp beginSheet: dIProgressPanel
138                        modalForWindow: dIWindow
139                        modalDelegate: self
140                        didEndSelector: nil
141                        contextInfo: nil ];
142               
143                [ dIProgressIndicator startAnimation:self ];
144               
145                /* create Timer for Progressbar */
146//              NSTimer *timer = [ NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector( dIProgressbarUpdate: ) userInfo:nil repeats:YES ];
147               
148                /* create Image */
149                NSArray *arguments = [ NSArray arrayWithObjects:@"create",@"-f",[ [ NSArray arrayWithObjects:@"raw", @"qcow2", nil ] objectAtIndex:[ dIFormat indexOfSelectedItem ] ],dIFileName,[ NSString stringWithFormat:@"%@M",[ dISize stringValue ] ],nil ];
150                NSTask *task;
151                task = [ [ NSTask alloc ] init ];
152                [ task setLaunchPath: [ NSString stringWithFormat:@"%@/MacOS/qemu-img", [ [ [ NSBundle mainBundle ] resourcePath ] stringByDeletingLastPathComponent ] ] ];
153                [ task setArguments: arguments ];
154                [ task launch ];
155                [ task release ];
156        }
157}
158
159- (void)dIProgressPanelDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
160{
161//      NSLog(@"cocoaControlDiskImage: dIProgressPanelDidEnd");
162
163        [ sheet orderOut:self ];
164}
165
166- (IBAction)dIProgressPanelStop:(id)sender
167{
168//      NSLog(@"cocoaControlDiskImage: dIProgressPanelStop");
169
170        [ NSApp endSheet:dIProgressPanel ];
171}
172
173- (void) dIProgressbarUpdate:(id)sender
174{
175//      NSLog(@"cocoaControlDiskImage: dIProgressbarUpdate");
176
177        int fsize;
178        NSFileManager *fileManager = [ NSFileManager defaultManager ];
179        NSDictionary *fattrs = [ fileManager fileAttributesAtPath:dIFileName traverseLink:YES ];
180        if (!fattrs) {
181                NSLog(@"no File");
182        } else {
183                NSLog(@"we got a File");
184                if ((fsize = [ fattrs fileSize ])) {
185                        NSLog(@"File size: %d", fsize);
186                } else {
187                        NSLog(@"no File size");
188                }
189        }
190//      [ sender invalidate ];
191}
192@end
Note: See TracBrowser for help on using the browser.