wfrm_Users.cs
上传用户:horngjaan
上传日期:2009-12-12
资源大小:2882k
文件大小:11k
源码类别:

Email服务器

开发平台:

C#

  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Windows.Forms;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using LumiSoft.UI.Controls;
  10. namespace LumiSoft.MailServer
  11. {
  12. /// <summary>
  13. /// Summary description for User.
  14. /// </summary>
  15. public class wfrm_Users : System.Windows.Forms.Form
  16. {
  17. private LumiSoft.UI.Controls.WDataGrid grid;
  18. private LumiSoft.UI.Controls.WLabel mt_domain;
  19. private LumiSoft.UI.Controls.WComboBox m_pDomains;
  20. private LumiSoft.UI.Controls.WButton m_pNew;
  21. private LumiSoft.UI.Controls.WButton m_pEdit;
  22. private LumiSoft.UI.Controls.WButton m_pDelete;
  23. /// <summary>
  24. /// Required designer variable.
  25. /// </summary>
  26. private System.ComponentModel.Container components = null;
  27. private ServerAPI m_ServerAPI = null;
  28. private DataView  m_DvUsers   = null;
  29. /// <summary>
  30. /// Default constructor.
  31. /// </summary>
  32. /// <param name="serverAPI"></param>
  33. public wfrm_Users(ServerAPI serverAPI)
  34. {
  35. //
  36. // Required for Windows Form Designer support
  37. //
  38. InitializeComponent();
  39. //
  40. // TODO: Add any constructor code after InitializeComponent call
  41. //
  42. try
  43. {
  44. m_ServerAPI = serverAPI;
  45. InitGrid();
  46. RefreshForm();
  47. }
  48. catch(Exception x)
  49. {
  50. wfrm_Error frm = new wfrm_Error(x,new System.Diagnostics.StackTrace());
  51. frm.ShowDialog(this);
  52. }
  53. }
  54. #region function Dispose
  55. /// <summary>
  56. /// Clean up any resources being used.
  57. /// </summary>
  58. protected override void Dispose( bool disposing )
  59. {
  60. if( disposing )
  61. {
  62. if(components != null)
  63. {
  64. components.Dispose();
  65. }
  66. }
  67. base.Dispose( disposing );
  68. }
  69. #endregion
  70. #region Windows Form Designer generated code
  71. /// <summary>
  72. /// Required method for Designer support - do not modify
  73. /// the contents of this method with the code editor.
  74. /// </summary>
  75. private void InitializeComponent()
  76. {
  77. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(wfrm_Users));
  78. this.grid = new LumiSoft.UI.Controls.WDataGrid();
  79. this.mt_domain = new LumiSoft.UI.Controls.WLabel();
  80. this.m_pDomains = new LumiSoft.UI.Controls.WComboBox();
  81. this.m_pNew = new LumiSoft.UI.Controls.WButton();
  82. this.m_pEdit = new LumiSoft.UI.Controls.WButton();
  83. this.m_pDelete = new LumiSoft.UI.Controls.WButton();
  84. ((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
  85. ((System.ComponentModel.ISupportInitialize)(this.m_pDomains)).BeginInit();
  86. ((System.ComponentModel.ISupportInitialize)(this.m_pNew)).BeginInit();
  87. ((System.ComponentModel.ISupportInitialize)(this.m_pEdit)).BeginInit();
  88. ((System.ComponentModel.ISupportInitialize)(this.m_pDelete)).BeginInit();
  89. this.SuspendLayout();
  90. // 
  91. // grid
  92. // 
  93. this.grid.CaptionVisible = false;
  94. this.grid.DataMember = "";
  95. this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  96. this.grid.Location = new System.Drawing.Point(14, 64);
  97. this.grid.Name = "grid";
  98. this.grid.Size = new System.Drawing.Size(600, 288);
  99. this.grid.TabIndex = 0;
  100. // 
  101. // mt_domain
  102. // 
  103. this.mt_domain.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  104. this.mt_domain.Location = new System.Drawing.Point(16, 16);
  105. this.mt_domain.Name = "mt_domain";
  106. this.mt_domain.Size = new System.Drawing.Size(80, 24);
  107. this.mt_domain.TabIndex = 5;
  108. this.mt_domain.Text = "Domain";
  109. this.mt_domain.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
  110. this.mt_domain.UseStaticViewStyle = true;
  111. // 
  112. // m_pDomains
  113. // 
  114. this.m_pDomains.AcceptsPlussKey = true;
  115. this.m_pDomains.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(228)), ((System.Byte)(224)), ((System.Byte)(220)));
  116. this.m_pDomains.ButtonIcon = ((System.Drawing.Icon)(resources.GetObject("m_pDomains.ButtonIcon")));
  117. this.m_pDomains.ButtonWidth = 18;
  118. this.m_pDomains.DropDownWidth = 168;
  119. this.m_pDomains.Location = new System.Drawing.Point(96, 16);
  120. this.m_pDomains.Mask = LumiSoft.UI.Controls.WEditBox_Mask.Text;
  121. this.m_pDomains.MaxLength = 32767;
  122. this.m_pDomains.Name = "m_pDomains";
  123. this.m_pDomains.ReadOnly = true;
  124. this.m_pDomains.SelectedIndex = -1;
  125. this.m_pDomains.Size = new System.Drawing.Size(168, 20);
  126. this.m_pDomains.TabIndex = 1;
  127. this.m_pDomains.UseStaticViewStyle = true;
  128. this.m_pDomains.VisibleItems = 5;
  129. this.m_pDomains.SelectedIndexChanged += new System.EventHandler(this.m_pDomains_SelectedIndexChanged);
  130. // 
  131. // m_pNew
  132. // 
  133. this.m_pNew.Location = new System.Drawing.Point(376, 16);
  134. this.m_pNew.Name = "m_pNew";
  135. this.m_pNew.Size = new System.Drawing.Size(64, 24);
  136. this.m_pNew.TabIndex = 2;
  137. this.m_pNew.Text = "New...";
  138. this.m_pNew.UseStaticViewStyle = true;
  139. this.m_pNew.ButtonPressed += new LumiSoft.UI.Controls.ButtonPressedEventHandler(this.m_pNew_Click);
  140. // 
  141. // m_pEdit
  142. // 
  143. this.m_pEdit.Location = new System.Drawing.Point(456, 16);
  144. this.m_pEdit.Name = "m_pEdit";
  145. this.m_pEdit.Size = new System.Drawing.Size(64, 24);
  146. this.m_pEdit.TabIndex = 3;
  147. this.m_pEdit.Text = "Edit...";
  148. this.m_pEdit.UseStaticViewStyle = true;
  149. this.m_pEdit.ButtonPressed += new LumiSoft.UI.Controls.ButtonPressedEventHandler(this.m_pEdit_Click);
  150. // 
  151. // m_pDelete
  152. // 
  153. this.m_pDelete.Location = new System.Drawing.Point(536, 16);
  154. this.m_pDelete.Name = "m_pDelete";
  155. this.m_pDelete.Size = new System.Drawing.Size(64, 24);
  156. this.m_pDelete.TabIndex = 4;
  157. this.m_pDelete.Text = "Delete";
  158. this.m_pDelete.UseStaticViewStyle = true;
  159. this.m_pDelete.ButtonPressed += new LumiSoft.UI.Controls.ButtonPressedEventHandler(this.m_pDelete_Click);
  160. // 
  161. // wfrm_Users
  162. // 
  163. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  164. this.ClientSize = new System.Drawing.Size(628, 373);
  165. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  166.   this.m_pDelete,
  167.   this.m_pEdit,
  168.   this.m_pNew,
  169.   this.m_pDomains,
  170.   this.mt_domain,
  171.   this.grid});
  172. this.Name = "wfrm_Users";
  173. this.Text = "User";
  174. ((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
  175. ((System.ComponentModel.ISupportInitialize)(this.m_pDomains)).EndInit();
  176. ((System.ComponentModel.ISupportInitialize)(this.m_pNew)).EndInit();
  177. ((System.ComponentModel.ISupportInitialize)(this.m_pEdit)).EndInit();
  178. ((System.ComponentModel.ISupportInitialize)(this.m_pDelete)).EndInit();
  179. this.ResumeLayout(false);
  180. }
  181. #endregion
  182. #region Events handling
  183. #region function m_pDomains_SelectedIndexChanged
  184. private void m_pDomains_SelectedIndexChanged(object sender, System.EventArgs e)
  185. {
  186. if(m_DvUsers != null){
  187. if(m_pDomains.SelectedItem.ToString() == "ALL"){
  188. m_DvUsers.RowFilter = "";
  189. }
  190. else{
  191. m_DvUsers.RowFilter = "DomainID='" + m_pDomains.SelectedItem.Tag.ToString() + "'";
  192. }
  193. }
  194. }
  195. #endregion
  196. #region function m_pNew_Click
  197. private void m_pNew_Click(object sender, System.EventArgs e)
  198. {
  199. if(m_pDomains.Items.Count > 1){
  200. wfrm_User frm = new wfrm_User(m_ServerAPI,m_pDomains.SelectedItem.Tag.ToString());
  201. if(frm.ShowDialog(this) == DialogResult.OK){
  202. DataRow dr = m_ServerAPI.AddUser(frm.FullName,frm.UserName,frm.Password,frm.Description,frm.Emailss,frm.DomainID,frm.MailboxSize);
  203. if(dr == null){
  204. MessageBox.Show("Error adding user.");
  205. return;
  206. }
  207. m_DvUsers.Table.ImportRow(dr);
  208. UpdateButtons();
  209. }
  210. }
  211. else{
  212. MessageBox.Show("Please open Emails domain before !");
  213. }
  214. }
  215. #endregion
  216. #region function m_pEdit_Click
  217. private void m_pEdit_Click(object sender, System.EventArgs e)
  218. {
  219. try
  220. {
  221. DataRow dr = ((DataView)(grid.DataSource))[grid.CurrentRowIndex].Row;
  222. if(dr != null){
  223. wfrm_User frm = new wfrm_User(m_ServerAPI,dr);
  224. if(frm.ShowDialog(this) == DialogResult.OK){
  225. m_ServerAPI.UpdateUser(dr["UserID"].ToString(),frm.FullName,frm.Password,frm.Description,frm.Emailss,frm.DomainID,frm.MailboxSize);
  226. dr["FULLNAME"]     = frm.FullName;
  227. dr["USERNAME"]     = frm.UserName;
  228. dr["PASSWORD"]     = frm.Password;
  229. dr["Description"]  = frm.Description;
  230. dr["Emails"]       = frm.Emailss;
  231. dr["DomainID"]     = frm.DomainID;
  232. dr["Mailbox_Size"] = frm.MailboxSize;
  233. }
  234. }
  235. }
  236. catch(Exception x)
  237. {
  238. wfrm_Error frm = new wfrm_Error(x,new System.Diagnostics.StackTrace());
  239. frm.ShowDialog(this);
  240. }
  241. }
  242. #endregion
  243. #region function m_pDelete_Click
  244. private void m_pDelete_Click(object sender, System.EventArgs e)
  245. {
  246. try
  247. {
  248. if(MessageBox.Show(this,"To delete User click Yes","Delete confirmation",MessageBoxButtons.YesNo) == DialogResult.Yes)
  249. {
  250. DataRow dr = ((DataView)(grid.DataSource))[grid.CurrentRowIndex].Row;
  251. if(dr != null){
  252. m_ServerAPI.DeleteUser(dr["UserID"].ToString());
  253. dr.Delete();
  254. }
  255. UpdateButtons();
  256. }
  257. }
  258. catch(Exception x)
  259. {
  260. wfrm_Error frm = new wfrm_Error(x,new System.Diagnostics.StackTrace());
  261. frm.ShowDialog(this);
  262. }
  263. }
  264. #endregion
  265. #endregion
  266. #region function Fill_Combo
  267. private void Fill_Combo()
  268. {
  269. m_pDomains.Items.Clear();
  270. m_pDomains.Items.Add("ALL","ALL");
  271. DataView dvDomains = m_ServerAPI.GetDomainList();
  272. foreach(DataRowView vDr in dvDomains){
  273. m_pDomains.Items.Add(vDr["DomainName"].ToString(),vDr["DomainID"].ToString());
  274. }
  275. if(m_pDomains.Items.Count > 0){
  276. m_pDomains.SelectedIndex = 0;
  277. }
  278. }
  279. #endregion
  280. #region function UpdateButtons
  281. private void UpdateButtons()
  282. {
  283. int rowCount = ((DataView)(grid.DataSource)).Count;
  284. if(rowCount > 0){
  285. m_pEdit.Enabled   = true;
  286. m_pDelete.Enabled = true;
  287. }
  288. else{
  289. m_pEdit.Enabled   = false;
  290. m_pDelete.Enabled = false;
  291. }
  292. }
  293. #endregion
  294. #region function RefreshForm
  295. public void RefreshForm()
  296. {
  297. Fill_Combo();
  298. m_DvUsers = m_ServerAPI.GetUserList("ALL");
  299. grid.DataSource = m_DvUsers;
  300. UpdateButtons();
  301. }
  302. #endregion
  303. #region Grid Init stuff
  304. private void InitGrid()
  305. {
  306. grid.ReadOnly = true;
  307. DataGridTableStyle ts1 = new DataGridTableStyle();
  308. ts1.MappingName = "Users";
  309. DataGridNoActiveCellColumn TextCol2 = new DataGridNoActiveCellColumn();
  310. TextCol2.MappingName = "UserName";
  311. TextCol2.HeaderText = "Mailbox";
  312. TextCol2.Width = 145;
  313. ts1.GridColumnStyles.Add(TextCol2);
  314.       
  315. DataGridNoActiveCellColumn TextCol = new DataGridNoActiveCellColumn();
  316. TextCol.MappingName = "Emails";
  317. TextCol.HeaderText = "E-mail Address";
  318. TextCol.Width = 400;
  319. ts1.GridColumnStyles.Add(TextCol);
  320. DataGridNoActiveCellColumn TextCol3 = new DataGridNoActiveCellColumn();
  321. TextCol3.MappingName = "Description";
  322. TextCol3.HeaderText = "Description";
  323. TextCol3.Width = 250;
  324. ts1.GridColumnStyles.Add(TextCol3);
  325. grid.TableStyles.Add(ts1);
  326. }
  327. #endregion
  328. }
  329. }