~cszikszoy/do-plugins/vbox

« back to all changes in this revision

Viewing changes to VirtualBox/src/VMThread.cs

  • Committer: chris at szikszoy
  • Date: 2008-10-20 22:55:43 UTC
  • Revision ID: chris@szikszoy.com-20081020225543-owhlraynxgxp4v6m
Added VirtualBox plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// VMThread.cs
 
2
//
 
3
//  GNOME Do is the legal property of its developers.
 
4
//  Please refer to the COPYRIGHT file distributed with this
 
5
//  source distribution.
 
6
//
 
7
//  This program is free software: you can redistribute it and/or modify
 
8
//  it under the terms of the GNU General Public License as published by
 
9
//  the Free Software Foundation, either version 3 of the License, or
 
10
//  (at your option) any later version.
 
11
//
 
12
//  This program is distributed in the hope that it will be useful,
 
13
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
//  GNU General Public License for more details.
 
16
//
 
17
//  You should have received a copy of the GNU General Public License
 
18
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
 
 
21
using System;
 
22
using System.Collections.Generic;
 
23
using System.Diagnostics;
 
24
 
 
25
namespace VirtualBox
 
26
{
 
27
        public class VMThread
 
28
        {
 
29
                private string op1;
 
30
                private string op2;
 
31
                private VMState NewState;
 
32
                private VMItem vm;
 
33
                
 
34
                //complicated constructor if you want to supply everything
 
35
                public VMThread(string o1, string o2, VMState ns, ref VMItem v)
 
36
                {
 
37
                        this.op1 = o1;
 
38
                        this.op2 = o2;
 
39
                        this.NewState = ns;
 
40
                        this.vm = v;
 
41
                }
 
42
                
 
43
                //empty constructor, only for complex actions
 
44
                public VMThread(ref VMItem v)
 
45
                {
 
46
                        this.vm = v;    
 
47
                }
 
48
                
 
49
                //simple constructor, to make things easy
 
50
                public VMThread(VMState ns, ref VMItem v)
 
51
                {
 
52
                        NewState = ns;
 
53
                        vm = v;
 
54
                        op1 = "VBoxManage";
 
55
                        op2 = "controlvm " + v.Uuid + " ";
 
56
                        switch (NewState) //do some action, depending on what the desired NewState is
 
57
                        {
 
58
                        case VMState.saved:
 
59
                                op2 += "savestate";
 
60
                                break;
 
61
                        case VMState.off:
 
62
                                op2 += "poweroff";
 
63
                                break;
 
64
                        case VMState.on:
 
65
                                if (v.Status == VMState.paused)
 
66
                                        op2 += "resume";
 
67
                                else if ((v.Status == VMState.off) || (v.Status == VMState.saved))
 
68
                                        op2 = "startvm " + v.Uuid + " -type gui";
 
69
                                break;
 
70
                        case VMState.paused:
 
71
                                op2 += "pause";
 
72
                                break;
 
73
                        case VMState.headless:
 
74
                                if (v.Status == VMState.paused)
 
75
                                        op2 += "resume";
 
76
                                else if ((v.Status == VMState.off) || (v.Status == VMState.saved))
 
77
                                        op2 = "startvm " + v.Uuid + " -type vrdp";
 
78
                                break;
 
79
                        }       
 
80
                }
 
81
                
 
82
                public void DoAction()
 
83
                {
 
84
                        vm.Status = VMState.limbo;
 
85
                        ProcessStartInfo ps = new ProcessStartInfo (op1, op2);
 
86
                        ps.UseShellExecute = false;
 
87
                        ps.RedirectStandardOutput = true;
 
88
                        using (Process p = Process.Start (ps)) {
 
89
                                p.WaitForExit ();
 
90
                                if (p.HasExited)
 
91
                                        vm.Status = NewState;
 
92
                        }       
 
93
                }
 
94
                
 
95
                public void DoShutdownRestoreAction()
 
96
                {
 
97
                        vm.Status = VMState.limbo;
 
98
                        List<ProcessStartInfo> Processes = new List<ProcessStartInfo>();
 
99
                        Processes.Add( new ProcessStartInfo ("VBoxManage", "controlvm " + vm.Uuid + " poweroff") );
 
100
                        Processes.Add( new ProcessStartInfo ("sleep", "2") );
 
101
                        Processes.Add( new ProcessStartInfo ("VBoxManage", "snapshot " + vm.Uuid + " discardcurrent -state") );
 
102
                        
 
103
                        foreach (ProcessStartInfo ps in Processes)
 
104
                        {
 
105
                                ps.UseShellExecute = false;
 
106
                                ps.RedirectStandardOutput = true;
 
107
                                using (Process p = Process.Start (ps))
 
108
                                        p.WaitForExit ();
 
109
                        }
 
110
                        vm.Status = VMState.off;
 
111
                }
 
112
        }
 
113
}