void buttonDelete_Click(object sender, EventArgs e) { try { if (listView1.SelectedItems.Count < 1) { return; } if (MessageBox.Show("Are you sure to delete the item ?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return; } string id = listView1.SelectedItems[0].Name.Replace("Item", ""); var item = (from q in Variables.xDocument.Descendants("Item") where q.Attribute("UserID").Value == Variables.CurrentUserID && q.Attribute("ID").Value == id select q).First(); item.Remove(); TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV); //Variables.xDocument.Save("debug.xml"); LoadPhoneBookItems(); int contactsNumbers = Variables.xDocument.Descendants("Item").Where(q => q.Attribute("UserID").Value == Variables.CurrentUserID).Count(); this.Text = Variables.Caption + Variables.CurrentUserName + " : " + contactsNumbers.ToString() + " Contacts"; } catch (Exception ex) { StackFrame file_info = new StackFrame(true); Messages.error(ref file_info, ex.Message, this); } }
void persianToolStripMenuItem_Click(object sender, EventArgs e) { christianToolStripMenuItem.Checked = false; persianToolStripMenuItem.Checked = true; var query = (from q in Variables.xDocument.Descendants("Setting") where q.Attribute("UserID").Value == Variables.CurrentUserID select q).First(); query.Attribute("Dates").Value = "Persian"; TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV); //Variables.xDocument.Save("debug.xml"); }
void leftToRightToolStripMenuItem_Click(object sender, EventArgs e) { try { leftToRightToolStripMenuItem.Checked = true; rightToLeftToolStripMenuItem.Checked = false; textBoxSearch.RightToLeft = RightToLeft.No; listView1.RightToLeft = RightToLeft.No; var query = (from q in Variables.xDocument.Descendants("Setting") where q.Attribute("UserID").Value == Variables.CurrentUserID select q).First(); query.Attribute("RightToLeft").Value = "NO"; TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV); } catch { } }
void toolStripMenuItemFontSize_Click(object sender, EventArgs e) { try { toolStripMenuItemFontSize8.Checked = toolStripMenuItemFontSize10.Checked = toolStripMenuItemFontSize12.Checked = toolStripMenuItemFontSize14.Checked = toolStripMenuItemFontSize16.Checked = toolStripMenuItemFontSize18.Checked = false; ToolStripMenuItem menuItem = sender as ToolStripMenuItem; menuItem.Checked = true; this.FontSize = float.Parse(menuItem.Text.Trim()); if (this.Font.Size != this.FontSize) { this.Font = new Font(this.Font.Name, this.FontSize, this.Font.Style, this.Font.Unit, this.Font.GdiCharSet, this.Font.GdiVerticalFont); var query = (from q in Variables.xDocument.Descendants("Setting") where q.Attribute("UserID").Value == Variables.CurrentUserID select q).First(); query.Attribute("FontSize").Value = this.FontSize.ToString(); TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV); //Variables.xDocument.Save("debug.xml"); } } catch { } }
private void buttonSubmit_Click(object sender, EventArgs e) { try { errorProvider1.Clear(); #region add new item if (NewItem) { if (textBoxName.Text.Trim() == "") { errorProvider1.SetError(textBoxName, "Please insert a name"); return; } int maxID = 0; try { maxID = (from q in Variables.xDocument.Descendants("Item") where q.Attribute("UserID").Value == Variables.CurrentUserID select(int) q.Attribute("ID")).Max(); } catch { } maxID++; XElement newItem = new XElement("Item", new XAttribute("ID", maxID), new XAttribute("UserID", Variables.CurrentUserID), new XAttribute("Name", textBoxName.Text.Trim()), new XAttribute("Mobile", textBoxMobile.Text.Trim()), new XAttribute("Phone", textBoxPhone.Text.Trim()), new XAttribute("Email", textBoxEMail.Text.Trim()), new XAttribute("Address", textBoxAddress.Text.Trim()), new XAttribute("RegDate", DateTime.Now.ToString())); var ItemsElement = (from q in Variables.xDocument.Descendants("Items") select q).First(); ItemsElement.Add(newItem); } #endregion #region edit item else if (EditItem) { if (textBoxName.Text.Trim() == "") { errorProvider1.SetError(textBoxName, "Please insert name"); return; } var theItem = (from q in Variables.xDocument.Descendants("Item") where q.Attribute("ID").Value == this.ItemID select q).First(); theItem.Attribute("Name").Value = textBoxName.Text.Trim(); theItem.Attribute("Mobile").Value = textBoxMobile.Text.Trim(); theItem.Attribute("Phone").Value = textBoxPhone.Text.Trim(); theItem.Attribute("Email").Value = textBoxEMail.Text.Trim(); theItem.Attribute("Address").Value = textBoxAddress.Text.Trim(); } #endregion TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV); //Variables.xDocument.Save("debug.xml"); this.Close(); } catch (Exception ex) { StackFrame file_info = new StackFrame(true); Messages.error(ref file_info, ex.Message, this); } }
private void buttonSubmit_Click(object sender, EventArgs e) { try { errorProvider1.Clear(); #region Forgets the password if (checkBoxForgetPass.Enabled && checkBoxForgetPass.Checked) { if (textBoxUsername.Text.Trim() == "") { errorProvider1.SetError(this.textBoxUsername, "Please enter your username"); textBoxUsername.Focus(); return; } errorProvider1.Clear(); var user = Variables.xDocument.Descendants("User").Where(q => q.Attribute("UserName").Value.ToLower() == textBoxUsername.Text.Trim().ToLower()); if (user.Count() < 1) { errorProvider1.SetError(this.textBoxUsername, "This username didn't exist in data base !"); return; } string password = user.First().Attribute("Password").Value; try { NetworkCredential loginInfo = new NetworkCredential("username", "password"); MailMessage msg = new MailMessage(); msg.From = new MailAddress("*****@*****.**"); msg.To.Add(new MailAddress(user.First().Attribute("Email").Value)); msg.Subject = "Phonebook Password"; msg.Body = "Yours Password = "******"smtp.gmail.com"); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = loginInfo; client.Send(msg); MessageBox.Show("Your password have sended to your email", "Email sended", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } #endregion #region add new user else if (this.NewUser) { if (textBoxUsername.Text.Trim() == "" && textBoxUsername.Enabled) { errorProvider1.SetError(this.textBoxUsername, "Please enter username"); return; } else if (textBoxPassword1.Text.Trim() == "" && textBoxPassword1.Enabled) { errorProvider1.SetError(this.textBoxPassword1, "Please enter password"); return; } else if (textBoxPassword2.Text.Trim() == "" && textBoxPassword2.Enabled) { errorProvider1.SetError(this.textBoxPassword2, "Please enter confirm password"); return; } else if (textBoxPassword2.Text.Trim() != textBoxPassword1.Text.Trim()) { errorProvider1.SetError(this.textBoxPassword1, "Your passwords must be match"); errorProvider1.SetError(this.textBoxPassword2, "Your passwords must be match"); return; } else if (textBoxEmail.Text.Trim() == "" && textBoxEmail.Enabled) { errorProvider1.SetError(this.textBoxEmail, "Please enter a valid Email"); return; } if (!File.Exists(Variables.DBFile)) { Variables.xDocument = new XDocument( new XComment("\n Don't edit manually \n"), new XElement("PhoneBook", new XElement("Users", new XElement("User", new XAttribute("ID", "01"), new XAttribute("UserName", textBoxUsername.Text.Trim()), new XAttribute("Password", textBoxPassword1.Text.Trim()), new XAttribute("Email", textBoxEmail.Text.Trim()), new XAttribute("RegDate", DateTime.Now.ToString()))), new XElement("Settings", new XElement("Setting", new XAttribute("UserID", "01"), new XAttribute("RightToLeft", "NO"), new XAttribute("Dates", "Persian"), new XAttribute("FontSize", "10"))), new XElement("Items"))); Variables.CurrentUserID = "01"; } else { Variables.xDocument = XDocument.Parse(TripleDES.DecryptFromFile(Variables.DBFile, TripleDES.ByteKey, TripleDES.IV)); var SameUserQuery = from q in Variables.xDocument.Descendants("User") where q.Attribute("UserName").Value.ToLower() == textBoxUsername.Text.Trim().ToLower() select q; if (SameUserQuery.Count() >= 1) { errorProvider1.SetError(this.textBoxUsername, "This username has been existed, please choose another one"); return; } int maxID = 0; try { maxID = (from q in Variables.xDocument.Descendants("User") select(int) q.Attribute("ID")).Max(); } catch { } maxID++; Variables.CurrentUserID = maxID.ToString(); XElement xElement = new XElement("User", new XAttribute("ID", maxID), new XAttribute("UserName", textBoxUsername.Text.Trim()), new XAttribute("Password", textBoxPassword1.Text.Trim()), new XAttribute("Email", textBoxEmail.Text.Trim()), new XAttribute("RegDate", DateTime.Now.ToString())); var usersElement = (from q in Variables.xDocument.Descendants("Users") select q).First(); usersElement.Add(xElement); xElement = new XElement("Setting", new XAttribute("UserID", maxID), new XAttribute("RightToLeft", "NO"), new XAttribute("Dates", "Persian"), new XAttribute("FontSize", "10")); var settingsElement = (from q in Variables.xDocument.Descendants("Settings") select q).First(); settingsElement.Add(xElement); } Variables.CurrentUserName = textBoxUsername.Text.Trim(); TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV); //Variables.xDocument.Save("debug.xml"); } #endregion #region change user else if (this.ChangeUser) { if (Variables.xDocument == null) { MessageBox.Show("Your Username or Password is wrong", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (textBoxUsername.Text.Trim() == "") { errorProvider1.SetError(this.textBoxUsername, "Please enter username"); return; } else if (textBoxPassword1.Text.Trim() == "" && textBoxPassword1.Enabled) { errorProvider1.SetError(this.textBoxPassword1, "Please enter password"); return; } var query = from q in Variables.xDocument.Descendants("User") where textBoxUsername.Text.Trim().ToLower() == q.Attribute("UserName").Value.ToLower() && textBoxPassword1.Text.Trim().ToLower() == q.Attribute("Password").Value.ToLower() select q; if (query.Count() == 1) { Variables.CurrentUserID = query.First().Attribute("ID").Value; Variables.CurrentUserName = textBoxUsername.Text.Trim(); } else { Variables.CurrentUserID = ""; Variables.CurrentUserName = ""; MessageBox.Show("Your Username or Password is wrong", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } #endregion #region change info else if (this.ChangeInfo) { bool changePassword = true; if (textBoxUsername.Text.Trim() == "" && textBoxUsername.Enabled) { errorProvider1.SetError(this.textBoxUsername, "Please enter the username"); return; } else if (textBoxEmail.Text.Trim() == "" && textBoxEmail.Enabled) { errorProvider1.SetError(this.textBoxEmail, "Please enter a valid Email"); return; } else if (textBoxPassword1.Text.Trim() == textBoxPassword2.Text.Trim() && textBoxPassword2.Text.Trim() == "") { changePassword = false; } else if (textBoxPassword1.Text.Trim() == "" && textBoxPassword1.Enabled) { errorProvider1.SetError(this.textBoxPassword1, "Please enter old password"); return; } else if (textBoxPassword2.Text.Trim() == "" && textBoxPassword2.Enabled) { errorProvider1.SetError(this.textBoxPassword2, "Please enter new password"); return; } errorProvider1.Clear(); var query = (from q in Variables.xDocument.Descendants("User") where q.Attribute("ID").Value == Variables.CurrentUserID select q).First(); string oldPassword = query.Attribute("Password").Value; if (oldPassword.ToLower() != textBoxPassword1.Text.Trim().ToLower() && changePassword) { errorProvider1.SetError(this.textBoxPassword1, "The old password is wrong"); return; } else if (oldPassword == textBoxPassword1.Text.Trim() && changePassword) { query.Attribute("UserName").Value = textBoxUsername.Text.Trim(); query.Attribute("Password").Value = textBoxPassword2.Text.Trim(); query.Attribute("Email").Value = textBoxEmail.Text.Trim(); } else if (!changePassword) { query.Attribute("UserName").Value = textBoxUsername.Text.Trim(); query.Attribute("Email").Value = textBoxEmail.Text.Trim(); } Variables.CurrentUserID = query.Attribute("ID").Value; Variables.CurrentUserName = textBoxUsername.Text.Trim(); TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV); //Variables.xDocument.Save("debug.xml"); } #endregion this.Close(); } catch (Exception ex) { Variables.CurrentUserID = Variables.CurrentUserName = ""; StackFrame file_info = new StackFrame(true); Messages.error(ref file_info, ex.Message, this); } }