From 25261e84b951c32ab22798d106984b6a846b6d3f Mon Sep 17 00:00:00 2001 From: Ged Murphy Date: Fri, 25 Jan 2008 15:28:38 +0000 Subject: [PATCH] continue rewriting stuff... svn path=/trunk/tools/RosTE/; revision=661 --- GUI/ConfigHandler.cs | 54 +------- GUI/MainForm.cs | 3 +- GUI/VirtualMachine.cs | 292 +++++++++++++++++++++++++++++++----------- 3 files changed, 228 insertions(+), 121 deletions(-) diff --git a/GUI/ConfigHandler.cs b/GUI/ConfigHandler.cs index 48e1f64..54dd070 100644 --- a/GUI/ConfigHandler.cs +++ b/GUI/ConfigHandler.cs @@ -21,7 +21,6 @@ namespace RosTEGUI public class MainConfig { - private VMConfig vmConfig = null; private DataSet dataSet = null; private string qemuPath; @@ -134,14 +133,14 @@ namespace RosTEGUI { if (dataSet != null && virtMachs != null) { - int id = virtMachs.Count + 1; + int id = virtMachs.Count; virtMachs.Add(new VirtMach(id, pathIn)); } return virtMachs.Count; } - public void DeleteVirtMach(int index) + public bool DeleteVirtMach(int index) { bool bRet = false; @@ -153,6 +152,8 @@ namespace RosTEGUI bRet = true; } } + + return bRet; } public int GetNumberOfVms() @@ -311,6 +312,7 @@ namespace RosTEGUI #region private methods private bool LoadMainSchema() { + XmlTextReader xtr = null; string filename = "MainConfig.xsd"; bool ret = false; @@ -320,57 +322,14 @@ namespace RosTEGUI try { FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); - XmlTextReader xtr = new XmlTextReader(fs); + xtr = new XmlTextReader(fs); dataSet.ReadXmlSchema(xtr); - xtr.Close(); ret = true; } catch (Exception ex) { Debug.LogMessage("error loading main config schema", ex.Message, ex.StackTrace, true); } - } - - return ret; - } - #endregion - } - - - - public class VMConfig - { - private DataSet data = null; - - public DataSet DataSet - { - get { return data; } - } - - /// - /// Load the schema for the virtual machine configuration file - /// - /// True on success, false on failure - public bool LoadVirtMachData() - { - XmlTextReader xtr = null; - string filename = "VMConfig.xsd"; - bool ret = false; - - data = new DataSet(); - if (File.Exists(filename)) - { - try - { - FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); - xtr = new XmlTextReader(fs); - data.ReadXmlSchema(xtr); - ret = true; - } - catch (Exception ex) - { - Debug.LogMessage("error loading VM config schema", ex.Message, ex.StackTrace, true); - } finally { if (xtr != null) @@ -380,5 +339,6 @@ namespace RosTEGUI return ret; } + #endregion } } \ No newline at end of file diff --git a/GUI/MainForm.cs b/GUI/MainForm.cs index a08406b..ddafaee 100644 --- a/GUI/MainForm.cs +++ b/GUI/MainForm.cs @@ -14,7 +14,6 @@ namespace RosTEGUI public partial class MainForm : Form { private MainConfig mainConf; - private VMConfig mainData; public MainForm() { @@ -44,6 +43,8 @@ namespace RosTEGUI VirtualMachine vm = new VirtualMachine(); if (vm.LoadVMConfig(image)) { + vm.LoadVmSettings(); + ListViewItem lvi = VirtMachListView.Items.Add(vm.ToString(), 0); lvi.SubItems.Add(vm.MemSize.ToString() + " MB"); lvi.Tag = vm; diff --git a/GUI/VirtualMachine.cs b/GUI/VirtualMachine.cs index 1e91b76..21058a3 100644 --- a/GUI/VirtualMachine.cs +++ b/GUI/VirtualMachine.cs @@ -5,13 +5,35 @@ using System.Data; using System.Xml; using System.Windows.Forms; using System.Collections; +using System.Collections.Generic; namespace RosTEGUI { + public struct VirtMachInfo + { + public int virtMachID; + public string name; + public string machType; + public string defDir; + public int memSize; + public bool setClockToHost; + public bool cdRomEnable; + public bool cdRomUsePhys; + public string cdRomPhysDrv; + public bool cdRomUseIso; + public string cdRomIsoImg; + public bool floppyEnable; + public bool floppyUsePhys; + public string floppyPhysDrv; + public bool floppyUseImg; + public string floppyIsoImg; + } + + public class VMHardDrive { - private VMConfig vmConfig; private DataRow hdDataRow; + private DataSet dataSet; #region properties @@ -47,12 +69,6 @@ namespace RosTEGUI get { return (bool)hdDataRow["BootImg"]; } set { hdDataRow["BootImg"] = value; } } - - public VMHardDrive(VMConfig vmConfigIn) - { - vmConfig = vmConfigIn; - } - #endregion public override string ToString() @@ -70,7 +86,7 @@ namespace RosTEGUI try { - DataTable hddt = vmConfig.DataSet.Tables["HardDisks"]; + DataTable hddt = dataSet.Tables["HardDisks"]; hdDataRow = hddt.NewRow(); hdDataRow["DiskID"] = hddt.Rows.Count; hdDataRow["Name"] = nameIn; @@ -93,7 +109,7 @@ namespace RosTEGUI public void DeleteHardDrive(int diskID) { - DataTable dt = vmConfig.DataSet.Tables["HardDisks"]; + DataTable dt = dataSet.Tables["HardDisks"]; //DataRow dr = dt.Rows.Find(diskID); <-- can't seem to apply a primary key?? // workaround for the above foreach (DataRow dr in dt.Rows) @@ -109,17 +125,23 @@ namespace RosTEGUI public void LoadHardDrive(int index) { - DataTable hddt = vmConfig.DataSet.Tables["HardDisks"]; + DataTable hddt = dataSet.Tables["HardDisks"]; hdDataRow = hddt.Rows[index]; } } public class VirtualMachine { - private VMConfig vmConfig; private DataRow vmDataRow; + private List virtMachInfo = null; private ArrayList hardDrives; private ArrayList netCards; + private DataSet dataSet = null; + + public DataSet DataSet + { + get { return dataSet; } + } #region Virtual machine properties @@ -318,7 +340,7 @@ namespace RosTEGUI try { - DataTable vmdt = vmConfig.DataSet.Tables["VMConfig"]; + DataTable vmdt = dataSet.Tables["VMConfig"]; vmDataRow = vmdt.NewRow(); vmDataRow["VirtMachID"] = vmdt.Rows.Count + 1; vmDataRow["Name"] = name; @@ -338,11 +360,11 @@ namespace RosTEGUI vmDataRow["FloppyIsoImg"] = string.Empty; vmdt.Rows.Add(vmDataRow); - VMHardDrive vmhd = new VMHardDrive(vmConfig); + VMHardDrive vmhd = new VMHardDrive(); vmhd.CreateHardDrive("Main Drive", "hda", dir, 768, true); hardDrives.Add(vmhd); - DataTable netdt = vmConfig.DataSet.Tables["NetCards"]; + DataTable netdt = dataSet.Tables["NetCards"]; netDataRow = netdt.NewRow(); netDataRow["CardID"] = netdt.Rows.Count + 1; netDataRow["VirtMachID"] = vmDataRow["VirtMachID"]; @@ -365,74 +387,18 @@ namespace RosTEGUI return ret; } - public bool LoadVMConfig(string path) - { - bool ret = false; - string fileName = path + "\\Config.xml"; - - if (File.Exists(fileName)) - { - try - { - FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); - XmlTextReader xtr = new XmlTextReader(fs); - vmConfig.DataSet.ReadXml(xtr, System.Data.XmlReadMode.ReadSchema); - xtr.Close(); - - DataTable vmdt = vmConfig.DataSet.Tables["VMConfig"]; - vmDataRow = vmdt.Rows[0]; - - DataTable hddt = vmConfig.DataSet.Tables["HardDisks"]; - for (int i = 0; i < hddt.Rows.Count; i++) - { - VMHardDrive vmhd = new VMHardDrive(vmConfig); - vmhd.LoadHardDrive(i); - hardDrives.Add(vmhd); - } - - DataTable netdt = vmConfig.DataSet.Tables["NetCards"]; - foreach (DataRow dr in netdt.Rows) - netCards.Add(dr); - - ret = true; - } - catch (Exception e) - { - MessageBox.Show("error loading the VM Config.xml: " + e.Message); - } - } - - return ret; - } - - public void SaveVMConfig() - { - try - { - string fileName = DefDir + "\\Config.xml"; - Directory.CreateDirectory(DefDir); - FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); - XmlTextWriter xtw = new XmlTextWriter(fs, System.Text.Encoding.Unicode); - vmConfig.DataSet.WriteXml(xtw, System.Data.XmlWriteMode.WriteSchema); - xtw.Close(); - } - catch (Exception e) - { - MessageBox.Show("error loading VM Config.xml: " + e.Message); - } - } #endregion public VirtualMachine() - { + {/* vmConfig = new VMConfig(); if (!vmConfig.LoadVirtMachData()) MessageBox.Show("Failed to load VM Schema"); hardDrives = new ArrayList(3); - netCards = new ArrayList(); + netCards = new ArrayList();*/ } public override string ToString() @@ -490,7 +456,7 @@ namespace RosTEGUI int sizeIn, bool bootImgIn) { - VMHardDrive vmhd = new VMHardDrive(vmConfig); + VMHardDrive vmhd = new VMHardDrive(); if (vmhd.CreateHardDrive(nameIn, driveIn, pathIn, sizeIn, bootImgIn)) { hardDrives.Add(vmhd); @@ -510,5 +476,185 @@ namespace RosTEGUI { return hardDrives; } + + + public bool LoadVmSettings() + { + bool bRet = false; + + if (dataSet != null) + { + if (virtMachInfo == null) + { + virtMachInfo = new List(); + } + + try + { + foreach (DataRow vmRow in dataSet.Tables["VMConfig"].Rows) + { + VirtMachInfo vmi = new VirtMachInfo(); + vmi.virtMachID = (int)vmRow["VirtMachID"]; + vmi.name = (string)vmRow["Name"]; + vmi.machType = (string)vmRow["MachType"]; + vmi.defDir = (string)vmRow["DefDir"]; + vmi.memSize = (int)vmRow["MemSize"]; + vmi.setClockToHost = (bool)vmRow["SetClockToHost"]; + vmi.cdRomEnable = (bool)vmRow["CdRomEnable"]; + vmi.cdRomUsePhys = (bool)vmRow["CdRomUsePhys"]; + vmi.cdRomPhysDrv = (string)vmRow["CdRomPhysDrv"]; + vmi.cdRomUseIso = (bool)vmRow["CdRomUseIso"]; + vmi.cdRomIsoImg = (string)vmRow["CdRomIsoImg"]; + vmi.floppyEnable = (bool)vmRow["FloppyEnable"]; + vmi.floppyUsePhys = (bool)vmRow["FloppyUsePhys"]; + vmi.floppyPhysDrv = (string)vmRow["FloppyPhysDrv"]; + vmi.floppyUseImg = (bool)vmRow["FloppyUseImg"]; + vmi.floppyIsoImg = (string)vmRow["FloppyIsoImg"]; + + virtMachInfo.Add(vmi); + } + + bRet = true; + } + catch (Exception ex) + { + Debug.LogMessage("error loading VM config", ex.Message, ex.StackTrace, true); + } + } + + return bRet; + } + + public void SaveVmSettings() + { + if (dataSet != null) + { + dataSet.Tables["VirtMach"].Rows.Clear(); + + try + { + foreach (VirtMachInfo vmi in virtMachInfo) + { + DataRow vmRow = dataSet.Tables["VirtMach"].NewRow(); + + vmRow["VirtMachID"] = vmi.virtMachID; + vmRow["Name"] = vmi.name; + vmRow["MachType"] = vmi.machType; + vmRow["DefDir"] = vmi.defDir; + vmRow["MemSize"] = vmi.memSize; + vmRow["SetClockToHost"] = vmi.setClockToHost; + vmRow["CdRomEnable"] = vmi.cdRomEnable; + vmRow["CdRomUsePhys"] = vmi.cdRomUsePhys; + vmRow["CdRomPhysDrv"] = vmi.cdRomPhysDrv; + vmRow["CdRomUseIso"] = vmi.cdRomUseIso; + vmRow["CdRomIsoImg"] = vmi.cdRomIsoImg; + vmRow["FloppyEnable"] = vmi.floppyEnable; + vmRow["FloppyUsePhys"] = vmi.floppyUsePhys; + vmRow["FloppyPhysDrv"] = vmi.floppyPhysDrv; + vmRow["FloppyUseImg"] = vmi.floppyUseImg; + vmRow["FloppyIsoImg"] = vmi.floppyIsoImg; + } + } + catch (Exception ex) + { + Debug.LogMessage("error loading VM config", ex.Message, ex.StackTrace, true); + } + } + } + + + public bool LoadVMConfig(string path) + { + XmlTextReader xtr = null; + string fileName = path + "\\Config.xml"; + bool ret = false; + + if (LoadVirtMachSchema()) + { + if (File.Exists(fileName)) + { + try + { + FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); + xtr = new XmlTextReader(fs); + dataSet.ReadXml(xtr, System.Data.XmlReadMode.ReadSchema); + xtr.Close(); + ret = true; + } + catch (Exception ex) + { + Debug.LogMessage("error loading VM config", ex.Message, ex.StackTrace, true); + } + finally + { + if (xtr != null) + xtr.Close(); + } + } + } + + return ret; + } + + public void SaveVMConfig() + { + XmlTextWriter xtw = null; + string fileName = DefDir + "\\Config.xml"; + + if (!Directory.Exists(DefDir)) + Directory.CreateDirectory(DefDir); + + if (dataSet == null) + { + if (!LoadVirtMachSchema()) + return; + } + + try + { + FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); + xtw = new XmlTextWriter(fs, System.Text.Encoding.Unicode); + dataSet.WriteXml(xtw, System.Data.XmlWriteMode.WriteSchema); + } + catch (Exception ex) + { + Debug.LogMessage("error saving VM config", ex.Message, ex.StackTrace, true); + } + finally + { + if (xtw != null) + xtw.Close(); + } + } + + private bool LoadVirtMachSchema() + { + XmlTextReader xtr = null; + string filename = "VMConfig.xsd"; + bool ret = false; + + dataSet = new DataSet(); + if (File.Exists(filename)) + { + try + { + FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); + xtr = new XmlTextReader(fs); + dataSet.ReadXmlSchema(xtr); + ret = true; + } + catch (Exception ex) + { + Debug.LogMessage("error loading VM config schema", ex.Message, ex.StackTrace, true); + } + finally + { + if (xtr != null) + xtr.Close(); + } + } + + return ret; + } } } \ No newline at end of file