/// <summary> /// Fires when a user wants to delete some transactions /// </summary> private async void ctxDeleteTransactionConfirm_Click(object sender, EventArgs e) { gvTransactions.Enabled = false; gvAccounts.Enabled = false; btnRefreshTrans.Enabled = false; btnResetTrans.Enabled = false; await Task.Factory.StartNew(() => { int count = gvTransactions.SelectedRows.Count; for (int i = 0; i < gvTransactions.SelectedRows.Count; i++) { DataGridViewRow selectedItem = gvTransactions.SelectedRows[i]; if (selectedItem.DataBoundItem != null) { Journal.IBankAccount bankAccount; Journal.ITransaction selectedTrans = selectedItem.DataBoundItem as Journal.ITransaction; if (selectedTrans != null) { bankAccount = selectedTrans.BankAccount; //invoke GUI updates on gui thread this.MainThreadInvoke(() => { lblStatus.Text = string.Format("Deleted transaction {0} for {1}", selectedTrans.BankAccountTransactionK, ((Money)selectedTrans.Amount).ToLongString(true)); toolStripProgressBar1.Value = Convert.ToInt32(((double)i / (double)count) * 100); }); bankAccount.Transactions.Remove(selectedTrans); } } } }); this.MainThreadInvoke(async() => { AccountSummary summary = gvAccounts.SelectedRows[0].DataBoundItem as AccountSummary; Journal.IBankAccount selectedUserAccount = SEconomyPlugin.Instance.RunningJournal.BankAccounts.FirstOrDefault(i => i.BankAccountK == summary.Value); if (selectedUserAccount != null) { await selectedUserAccount.SyncBalanceAsync(); AccountSummary selectedSummary = accList.FirstOrDefault(i => i.Value == selectedUserAccount.BankAccountK); if (selectedSummary != null) { selectedSummary.Balance = selectedUserAccount.Balance; } } toolStripProgressBar1.Value = 100; gvTransactions.Enabled = true; gvAccounts.Enabled = true; btnRefreshTrans.Enabled = true; btnResetTrans.Enabled = true; btnShowFrom.Enabled = true; await LoadTransactionsForUser(summary.Value); }); }
private void gvAccounts_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { DataGridViewRow row = gvAccounts.Rows[e.RowIndex]; DataGridViewCell cell = row.Cells[0]; AccountSummary summary = row.DataBoundItem as AccountSummary; if (summary.IsSystem == true) { cell.Value = "System"; row.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(217, 237, 247); } else if (summary.IsOnline == true) { cell.Value = "Online"; row.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(223, 240, 216); } else if (summary.HasAccount == false) { row.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(242, 222, 222); cell.Value = "Orphan"; } else { row.DefaultCellStyle.ForeColor = System.Drawing.Color.DarkGray; cell.Value = "Offline"; } }
/// <summary> /// Occurs when a user clicks on the refresh transactions button on the right hand top toolbar /// </summary> private async void btnRefreshTrans_Click(object sender, EventArgs e) { if (gvAccounts.SelectedRows.Count > 0) { AccountSummary summary = gvAccounts.SelectedRows[0].DataBoundItem as AccountSummary; await LoadTransactionsForUser(summary.Value); } }
private void gvAccounts_SelectionChanged(object sender, EventArgs e) { if (gvAccounts.SelectedRows.Count > 0) { this.currentlySelectedAccount = gvAccounts.SelectedRows[0].DataBoundItem as AccountSummary; LoadTransactionsForUser(this.currentlySelectedAccount.Value); } else { this.currentlySelectedAccount = null; } }
/// <summary> /// Occurs when the user selects an item on the left hand menu /// </summary> private async void lbAccounts_SelectedIndexChanged(object sender, EventArgs e) { AccountSummary selectedSummary = gvAccounts.SelectedRows[0].DataBoundItem as AccountSummary; if (selectedSummary != null) { await LoadTransactionsForUser(selectedSummary.Value); } else { MessageBox.Show("Could not load bank account " + selectedSummary.DisplayValue, "Error"); } }
async Task LoadAccountsAsync() { List <string> tshockAccounts; List <Control> disabledControls = new List <Control>(); int i = 0; this.MainThreadInvoke(() => { lblStatus.Text = "Syncing accounts"; DisableAllControls(this, true, ref disabledControls); }); sourceAccList.Clear(); gvAccounts.DataSource = null; tshockAccounts = await Task.Run(() => TShock.Users.GetUsers().Select(acc => acc.Name).ToList()); foreach (Journal.IBankAccount account in SecInstance.RunningJournal.BankAccounts) { int p = Convert.ToInt32(((double)i / (double)SecInstance.RunningJournal.BankAccounts.Count) * 100); AccountSummary summary = new AccountSummary() { Name = account.UserAccountName, Balance = account.Balance, Value = account.BankAccountK, HasAccount = (account.IsPluginAccount == false || account.IsSystemAccount == false) && tshockAccounts.Any(x => x == account.UserAccountName), IsOnline = (account.IsPluginAccount == true || account.IsSystemAccount == true) || TShock.Players.Any(x => x != null && x.UserAccountName == account.UserAccountName), IsSystem = account.IsSystemAccount || account.IsPluginAccount }; sourceAccList.Add(summary); tshockAccounts.Remove(account.UserAccountName); this.MainThreadInvoke(() => { toolStripProgressBar1.Value = p; }); i++; } this.MainThreadInvoke(async() => { accList = new BindingList <AccountSummary>(sourceAccList.OrderByDescending(x => (long)x.Balance).ToList()); gvAccounts.DataSource = accList; await BindUserList(); EnableAllControls(disabledControls); lblStatus.Text = "Done."; }); }
private async void btnResetTrans_Click(object sender, EventArgs e) { if (MessageBox.Show("This will delete all transactions for this user. This could take a long time and SEconomy performance could be impacted. The user's balance will be reset to 0 copper.", "Delete all Transactions", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes) { Journal.IBankAccount account = SecInstance.RunningJournal.GetBankAccount(currentlySelectedAccount.Value); gvTransactions.Enabled = false; gvAccounts.Enabled = false; btnRefreshTrans.Enabled = false; btnResetTrans.Enabled = false; btnShowFrom.Enabled = false; lblStatus.Text = "Resetting all transactions for " + currentlySelectedAccount.Name; toolStripProgressBar1.Value = 0; toolStripProgressBar1.Style = ProgressBarStyle.Marquee; await Task.Factory.StartNew(() => { account.Transactions.Clear(); }); Journal.IBankAccount selectedUserAccount = SecInstance.RunningJournal.GetBankAccount(currentlySelectedAccount.Value); if (selectedUserAccount != null) { await selectedUserAccount.SyncBalanceAsync(); AccountSummary summary = accList.FirstOrDefault(i => i.Value == selectedUserAccount.BankAccountK); if (summary != null) { summary.Balance = selectedUserAccount.Balance; } this.MainThreadInvoke(async() => { toolStripProgressBar1.Value = 100; toolStripProgressBar1.Style = ProgressBarStyle.Continuous; gvTransactions.DataSource = null; gvTransactions.Enabled = true; gvAccounts.Enabled = true; btnRefreshTrans.Enabled = true; btnResetTrans.Enabled = true; //force the bindinglist to raise the reset event updating the right hand side accList.ResetBindings(); await LoadTransactionsForUser(currentlySelectedAccount.Value); }); } } }
async Task LoadTransactionsForUser(long BankAccountK) { Journal.IBankAccount selectedAccount = null; TSPlayer player; List <ITransaction> qTransactions; if (selectionEnabled == false) { return; } selectedAccount = SecInstance.RunningJournal.GetBankAccount(BankAccountK); qTransactions = selectedAccount.Transactions; gvTransactions.DataSource = null; gvTransactions.DataSource = qTransactions; tranList = SecInstance.RunningJournal.Transactions; player = TShock.Players.FirstOrDefault(i => i != null && i.UserAccountName == selectedAccount.UserAccountName); lblStatus.Text = string.Format("Loaded {0} transactions for {1}.", qTransactions.Count(), selectedAccount.UserAccountName); if (player != null) { lblOnline.Text = "Online"; lblOnline.ForeColor = System.Drawing.Color.DarkGreen; } else { lblOnline.Text = "Offline"; lblOnline.ForeColor = System.Drawing.Color.Red; } await selectedAccount.SyncBalanceAsync(); AccountSummary summary = accList.FirstOrDefault(i => i.Value == BankAccountK); if (summary != null) { summary.Balance = selectedAccount.Balance; } this.MainThreadInvoke(() => { lblBalance.Text = selectedAccount.Balance.ToLongString(true); lblName.Text = string.Format("{0}, acccount ID {1}", selectedAccount.UserAccountName, selectedAccount.BankAccountK); }); }
private async void gvAccounts_SelectionChanged(object sender, EventArgs e) { if (selectionEnabled == false) { return; } if (gvAccounts.SelectedRows.Count > 0) { this.currentlySelectedAccount = gvAccounts.SelectedRows[0].DataBoundItem as AccountSummary; await LoadTransactionsForUser(this.currentlySelectedAccount.Value); } else { this.currentlySelectedAccount = null; } }
private async void btnShowFrom_Click(object sender, EventArgs e) { var vivibleRowsCount = gvTransactions.DisplayedRowCount(true); var firstDisplayedRowIndex = gvTransactions.FirstDisplayedCell.RowIndex; var lastvibileRowIndex = (firstDisplayedRowIndex + vivibleRowsCount) - 1; for (int rowIndex = firstDisplayedRowIndex; rowIndex <= lastvibileRowIndex; rowIndex++) { var row = gvTransactions.Rows[rowIndex]; var cell = row.Cells.OfType <DataGridViewLinkCell>().FirstOrDefault(); if (string.IsNullOrEmpty(cell.Value as string)) { await Task.Factory.StartNew(() => { Journal.ITransaction trans = row.DataBoundItem as Journal.ITransaction; Journal.ITransaction oppositeTrans = tranList.FirstOrDefault(i => i.BankAccountTransactionK == trans.BankAccountTransactionFK); if (oppositeTrans != null) { AccountSummary oppositeAccount = accList.FirstOrDefault(i => i.Value == oppositeTrans.BankAccountFK); if (oppositeAccount != null) { this.MainThreadInvoke(() => { cell.Value = oppositeAccount.Name; }); } } else { this.MainThreadInvoke(() => { cell.LinkBehavior = LinkBehavior.NeverUnderline; cell.LinkColor = System.Drawing.Color.Gray; cell.Value = "{Unknown}"; }); } }); } } }
void LoadTransactionsForUser(string BankAccountK) { Journal.XBankAccount selectedAccount = Journal.TransactionJournal.GetBankAccount(BankAccountK); var qTransactions = (from i in Journal.TransactionJournal.Transactions where i.BankAccountFK == BankAccountK orderby i.TransactionDateUtc select i).ToArray(); gvTransactions.DataSource = null; gvTransactions.DataSource = qTransactions; tranList = Journal.TransactionJournal.Transactions.ToList(); lblStatus.Text = string.Format("Loaded {0} transactions for {1}.", qTransactions.Count(), BankAccountK); if (selectedAccount.Owner != null) { lblOnline.Text = "Online"; lblOnline.ForeColor = System.Drawing.Color.DarkGreen; } else { lblOnline.Text = "Offline"; lblOnline.ForeColor = System.Drawing.Color.Red; } selectedAccount.SyncBalanceAsync().ContinueWith((task) => { AccountSummary summary = accList.FirstOrDefault(i => i.Value == BankAccountK); if (summary != null) { summary.Balance = selectedAccount.Balance; } lblBalance.Text = selectedAccount.Balance.ToLongString(true); lblName.Text = string.Format("{0}, acccount ID {1}", selectedAccount.UserAccountName, selectedAccount.BankAccountK); }); }
Task LoadAccountsAsync() { return(Task.Factory.StartNew(() => { List <Journal.XBankAccount> accountList = Journal.TransactionJournal.BankAccounts.ToList(); List <Journal.XTransaction> transList = Journal.TransactionJournal.Transactions.ToList(); this.MainThreadInvoke(() => { gvTransactions.Enabled = false; gvAccounts.Enabled = false; btnRefreshTrans.Enabled = false; btnResetTrans.Enabled = false; lblStatus.Text = "Syncing accounts"; }); //Sync the account list for (int i = 0; i < accountList.Count; i++) { Journal.XBankAccount account = accountList.ElementAt(i); sourceAccList.Add(new AccountSummary() { Name = account.UserAccountName, Balance = account.Balance, Value = account.BankAccountK }); int p = Convert.ToInt32(((double)i / (double)accountList.Count) * 100); this.MainThreadInvoke(() => { toolStripProgressBar1.Value = p; }); } this.MainThreadInvoke(() => { lblStatus.Text = "Syncing transactions"; }); //Sync balances into accounts for (int i = 0; i < transList.Count; i++) { Journal.XTransaction trans = transList.ElementAt(i); if (trans != null) { AccountSummary summary = sourceAccList.FirstOrDefault(x => x.Value == trans.BankAccountFK); if (summary != null) { summary.Balance += trans.Amount; } } int p = Convert.ToInt32(((double)i / (double)transList.Count) * 100); this.MainThreadInvoke(() => { toolStripProgressBar1.Value = p; }); } this.MainThreadInvoke(() => { accList = new BindingList <AccountSummary>(sourceAccList.OrderByDescending(x => (long)x.Balance).ToList()); gvAccounts.DataSource = accList; gvTransactions.Enabled = true; gvAccounts.Enabled = true; btnRefreshTrans.Enabled = true; btnResetTrans.Enabled = true; lblStatus.Text = "Done."; }); })); }
private void btnResetTrans_Click(object sender, EventArgs e) { if (MessageBox.Show("This will delete all transactions for this user. This could take a long time and SEconomy performance could be impacted. The user's balance will be reset to 0 copper.", "Delete all Transactions", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes) { gvTransactions.Enabled = false; gvAccounts.Enabled = false; btnRefreshTrans.Enabled = false; btnResetTrans.Enabled = false; btnShowFrom.Enabled = false; lblStatus.Text = "Resetting all transactions for " + currentlySelectedAccount.Name; toolStripProgressBar1.Value = 0; toolStripProgressBar1.Style = ProgressBarStyle.Marquee; Task.Factory.StartNew(() => { Journal.TransactionJournal.XmlJournal.Element("Journal").Element("Transactions").Elements("Transaction").Where(i => i.Attribute("BankAccountFK").Value == currentlySelectedAccount.Value).Remove(); List <Journal.XTransaction> tranList = Journal.TransactionJournal.Transactions.Where(i => i.BankAccountFK == currentlySelectedAccount.Value).ToList(); int count = tranList.Count(); for (int i = 0; i < count; i++) { Journal.XTransaction selectedTrans = Journal.TransactionJournal.Transactions.FirstOrDefault(x => x.BankAccountTransactionK == tranList.ElementAt(i).BankAccountTransactionK); if (selectedTrans != null) { //invoke GUI updates on gui thread this.MainThreadInvoke(() => { lblStatus.Text = string.Format("Deleted transaction {0} for {1}", selectedTrans.BankAccountTransactionK, ((Money)selectedTrans.Amount).ToLongString(true)); toolStripProgressBar1.Value = Convert.ToInt32(((double)i / (double)count) * 100); }); ((XElement)selectedTrans).Remove(); } } }).ContinueWith((task) => { Journal.XBankAccount selectedUserAccount = Journal.TransactionJournal.GetBankAccount(currentlySelectedAccount.Value); if (selectedUserAccount != null) { selectedUserAccount.SyncBalanceAsync().ContinueWith((t) => { AccountSummary summary = accList.FirstOrDefault(i => i.Value == selectedUserAccount.BankAccountK); if (summary != null) { summary.Balance = selectedUserAccount.Balance; } this.MainThreadInvoke(() => { toolStripProgressBar1.Value = 100; toolStripProgressBar1.Style = ProgressBarStyle.Continuous; gvTransactions.Enabled = true; gvAccounts.Enabled = true; btnRefreshTrans.Enabled = true; btnResetTrans.Enabled = true; //force the bindinglist to raise the reset event updating the right hand side accList.ResetBindings(); LoadTransactionsForUser(currentlySelectedAccount.Value); }); }); } }); } }
async Task LoadAccountsAsync() { List<string> tshockAccounts; List<Control> disabledControls = new List<Control>(); int i = 0; this.MainThreadInvoke(() => { lblStatus.Text = "Syncing accounts"; DisableAllControls(this, true, ref disabledControls); }); sourceAccList.Clear(); gvAccounts.DataSource = null; tshockAccounts = await Task.Run(() => TShock.Users.GetUsers().Select(acc => acc.Name).ToList()); foreach (Journal.IBankAccount account in SecInstance.RunningJournal.BankAccounts) { int p = Convert.ToInt32(((double)i / (double)SecInstance.RunningJournal.BankAccounts.Count) * 100); AccountSummary summary = new AccountSummary() { Name = account.UserAccountName, Balance = account.Balance, Value = account.BankAccountK, HasAccount = (account.IsPluginAccount == false || account.IsSystemAccount == false) && tshockAccounts.Any(x => x == account.UserAccountName), IsOnline = (account.IsPluginAccount == true || account.IsSystemAccount == true) || TShock.Players.Any(x => x != null && x.UserAccountName == account.UserAccountName), IsSystem = account.IsSystemAccount || account.IsPluginAccount }; sourceAccList.Add(summary); tshockAccounts.Remove(account.UserAccountName); this.MainThreadInvoke(() => { toolStripProgressBar1.Value = p; }); i++; } this.MainThreadInvoke(async () => { accList = new BindingList<AccountSummary>(sourceAccList.OrderByDescending(x => (long)x.Balance).ToList()); gvAccounts.DataSource = accList; await BindUserList(); EnableAllControls(disabledControls); lblStatus.Text = "Done."; }); }