AddTargetForm.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using DiskAccessLibrary;
  9. using ISCSI.Server;
  10. using Utilities;
  11. namespace ISCSIConsole
  12. {
  13. public partial class AddTargetForm : Form
  14. {
  15. public static int m_targetNumber = 1;
  16. public const string DefaultTargetIQN = "iqn.1991-05.com.microsoft";
  17. private List<Disk> m_disks = new List<Disk>();
  18. private ISCSITarget m_target;
  19. public AddTargetForm()
  20. {
  21. InitializeComponent();
  22. #if Win32
  23. btnAddPhysicalDisk.Visible = true;
  24. btnAddVolume.Visible = true;
  25. if (!SecurityHelper.IsAdministrator())
  26. {
  27. btnAddPhysicalDisk.Enabled = false;
  28. btnAddVolume.Enabled = false;
  29. }
  30. #endif
  31. }
  32. private void AddTargetForm_Load(object sender, EventArgs e)
  33. {
  34. txtTargetIQN.Text = String.Format("{0}:target{1}", DefaultTargetIQN, m_targetNumber);
  35. }
  36. private void btnCreateDiskImage_Click(object sender, EventArgs e)
  37. {
  38. if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
  39. {
  40. CreateRAMDiskForm createRAMDisk = new CreateRAMDiskForm();
  41. DialogResult result = createRAMDisk.ShowDialog();
  42. if (result == DialogResult.OK)
  43. {
  44. RAMDisk ramDisk = createRAMDisk.RAMDisk;
  45. AddDisk(ramDisk);
  46. }
  47. }
  48. else
  49. {
  50. CreateDiskImageForm createDiskImage = new CreateDiskImageForm();
  51. DialogResult result = createDiskImage.ShowDialog();
  52. if (result == DialogResult.OK)
  53. {
  54. DiskImage diskImage = createDiskImage.DiskImage;
  55. AddDisk(diskImage);
  56. }
  57. }
  58. }
  59. private void btnAddDiskImage_Click(object sender, EventArgs e)
  60. {
  61. SelectDiskImageForm selectDiskImage = new SelectDiskImageForm();
  62. DialogResult result = selectDiskImage.ShowDialog();
  63. if (result == DialogResult.OK)
  64. {
  65. DiskImage diskImage = selectDiskImage.DiskImage;
  66. AddDisk(diskImage);
  67. }
  68. }
  69. private void btnAddPhysicalDisk_Click(object sender, EventArgs e)
  70. {
  71. #if Win32
  72. SelectPhysicalDiskForm selectPhysicalDisk = new SelectPhysicalDiskForm();
  73. DialogResult result = selectPhysicalDisk.ShowDialog();
  74. if (result == DialogResult.OK)
  75. {
  76. AddDisk(selectPhysicalDisk.SelectedDisk);
  77. }
  78. #endif
  79. }
  80. private void btnAddVolume_Click(object sender, EventArgs e)
  81. {
  82. #if Win32
  83. SelectVolumeForm selectVolume = new SelectVolumeForm();
  84. DialogResult result = selectVolume.ShowDialog();
  85. if (result == DialogResult.OK)
  86. {
  87. VolumeDisk volumeDisk = new VolumeDisk(selectVolume.SelectedVolume);
  88. if (selectVolume.IsReadOnly)
  89. {
  90. volumeDisk.IsReadOnly = true;
  91. }
  92. AddDisk(volumeDisk);
  93. }
  94. #endif
  95. }
  96. private void AddDisk(Disk disk)
  97. {
  98. string description = String.Empty;
  99. string sizeString = FormattingHelper.GetStandardSizeString(disk.Size);
  100. if (disk is DiskImage)
  101. {
  102. description = ((DiskImage)disk).Path;
  103. }
  104. else if (disk is RAMDisk)
  105. {
  106. description = "RAM Disk";
  107. }
  108. #if Win32
  109. else if (disk is PhysicalDisk)
  110. {
  111. description = String.Format("Physical Disk {0}", ((PhysicalDisk)disk).PhysicalDiskIndex);
  112. }
  113. else if (disk is VolumeDisk)
  114. {
  115. description = String.Format("Volume");
  116. }
  117. #endif
  118. ListViewItem item = new ListViewItem(description);
  119. item.SubItems.Add(sizeString);
  120. listDisks.Items.Add(item);
  121. m_disks.Add(disk);
  122. }
  123. private void btnRemove_Click(object sender, EventArgs e)
  124. {
  125. if (listDisks.SelectedIndices.Count > 0)
  126. {
  127. int selectedIndex = listDisks.SelectedIndices[0];
  128. LockUtils.ReleaseDisk(m_disks[selectedIndex]);
  129. m_disks.RemoveAt(selectedIndex);
  130. listDisks.Items.RemoveAt(selectedIndex);
  131. }
  132. }
  133. private void btnOK_Click(object sender, EventArgs e)
  134. {
  135. if (!ISCSINameHelper.IsValidIQN(txtTargetIQN.Text))
  136. {
  137. MessageBox.Show("Target IQN is invalid", "Error");
  138. return;
  139. }
  140. m_target = new ISCSITarget(txtTargetIQN.Text, m_disks);
  141. m_targetNumber++;
  142. this.DialogResult = DialogResult.OK;
  143. this.Close();
  144. }
  145. private void btnCancel_Click(object sender, EventArgs e)
  146. {
  147. this.DialogResult = DialogResult.Cancel;
  148. this.Close();
  149. }
  150. public ISCSITarget Target
  151. {
  152. get
  153. {
  154. return m_target;
  155. }
  156. }
  157. private void listDisks_SelectedIndexChanged(object sender, EventArgs e)
  158. {
  159. btnRemove.Enabled = (listDisks.SelectedIndices.Count > 0);
  160. }
  161. private void listDisks_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
  162. {
  163. e.NewWidth = ((ListView)sender).Columns[e.ColumnIndex].Width;
  164. e.Cancel = true;
  165. }
  166. private void AddTargetForm_FormClosing(object sender, FormClosingEventArgs e)
  167. {
  168. if (this.DialogResult != DialogResult.OK)
  169. {
  170. LockUtils.ReleaseDisks(m_disks);
  171. }
  172. }
  173. private void AddTargetForm_KeyDown(object sender, KeyEventArgs e)
  174. {
  175. if (e.Control)
  176. {
  177. btnCreateDiskImage.Text = "Create RAM Disk";
  178. }
  179. }
  180. private void AddTargetForm_KeyUp(object sender, KeyEventArgs e)
  181. {
  182. btnCreateDiskImage.Text = "Create Virtual Disk";
  183. }
  184. private void AddTargetForm_Deactivate(object sender, EventArgs e)
  185. {
  186. btnCreateDiskImage.Text = "Create Virtual Disk";
  187. }
  188. }
  189. }