~cszikszoy/do-plugins/vbox

« back to all changes in this revision

Viewing changes to VirtualBox/src/VMItem.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
// VMItem.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
using System;
 
21
using System.Diagnostics;
 
22
using System.Collections.Generic;
 
23
using System.IO;
 
24
using System.Xml;
 
25
using System.Threading;
 
26
 
 
27
using Do.Universe;
 
28
 
 
29
namespace VirtualBox
 
30
{
 
31
        public enum VMState
 
32
        {
 
33
                saved,
 
34
                on,
 
35
                paused,
 
36
                off,
 
37
                limbo,
 
38
                headless
 
39
        }
 
40
 
 
41
        public class VMItem : IItem
 
42
        {
 
43
                string name;
 
44
                string uuid;
 
45
                string ico_file;
 
46
                bool has_saved_states = false;
 
47
                VMState state;
 
48
                
 
49
                public VMItem (string xmlLineEntry) {
 
50
                        uuid = xmlLineEntry.Substring(xmlLineEntry.IndexOf("uuid=\"{")+7,36);
 
51
                        //find machine specific xml file
 
52
                        int StartMachinePath = xmlLineEntry.IndexOf("src=\"")+5;
 
53
                        int EndMachinePath = xmlLineEntry.LastIndexOf(".xml\" ");
 
54
                        string home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
 
55
                        string MachineSource = ("~/.VirtualBox/"+xmlLineEntry.Substring(StartMachinePath,(EndMachinePath+4) - StartMachinePath)).Replace ("~", home);
 
56
                        //parse machine specific xml file
 
57
                        try
 
58
                        {
 
59
                                StreamReader re = File.OpenText(MachineSource);
 
60
                                string input = null;
 
61
                                while ((input = re.ReadLine()) != null)
 
62
                                {
 
63
                                        if ((input.Length > 20) && (input.Substring(0,11) == "  <Machine "))
 
64
                                        {
 
65
                                                int start = input.IndexOf("OSType=\"")+8;
 
66
                                                int end = input.IndexOf("\" lastState");
 
67
                                                ico_file = "os_" + input.Substring(start,end-start) + ".png";
 
68
                                        }
 
69
                                }
 
70
                                re.Close();
 
71
                        }
 
72
                        catch //can't find icon, assign default VBox Icon
 
73
                        {
 
74
                                ico_file = "VirtualBox_64px.png@"+GetType().Assembly.FullName;
 
75
                        }
 
76
                        ico_file += "@"+GetType().Assembly.FullName;                              
 
77
                        string[] split = xmlLineEntry.Substring(StartMachinePath,EndMachinePath -StartMachinePath).Split('/');
 
78
                        name = split[1];
 
79
                        //determine the state of thte VM
 
80
                        ProcessStartInfo ps = new ProcessStartInfo ("VBoxManage", "showvminfo " + uuid);
 
81
                        ps.UseShellExecute = false;
 
82
                        ps.RedirectStandardOutput = true;
 
83
                        using (Process p = Process.Start (ps)) {
 
84
                                p.WaitForExit ();
 
85
                                string output = p.StandardOutput.ReadToEnd ();
 
86
                                int s = output.IndexOf("State:");
 
87
                                int e = output.IndexOf("\n", s);
 
88
                                string outputState = output.Substring(s, e-s);
 
89
                                //States: saved, running, paused, powered off
 
90
                                if (outputState.Contains("saved"))
 
91
                                        state = VMState.saved;
 
92
                                else if (outputState.Contains("running"))
 
93
                                        state = VMState.on;
 
94
                                else if (outputState.Contains("paused"))
 
95
                                        state = VMState.paused;
 
96
                                else if (outputState.Contains("powered off"))
 
97
                                        state = VMState.off;
 
98
                                if (output.Contains("Snapshots:"))
 
99
                                    has_saved_states = true;
 
100
                        }                   
 
101
                }
 
102
                
 
103
                public string Name { get { return name; } }
 
104
                public string Uuid { get { return uuid; } }
 
105
                public bool HasSavedStates { get { return has_saved_states; } }
 
106
                public VMState Status 
 
107
                { 
 
108
                        get { return state; }
 
109
                        set { state = value; }
 
110
                }
 
111
                public string Description { get { return "Virtual Machine: " + name; } }
 
112
                public string Icon { get { return ico_file; } }
 
113
        }
 
114
}
 
 
b'\\ No newline at end of file'