private void OnRowSelected(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
 {
     if (e.AddedItems != null && e.AddedItems.Count > 0)
     {
         AccountImportState item = (AccountImportState)e.AddedItems[0];
         if (Navigator != null)
         {
             Navigator.ViewTransactions(item.Changed);
         }
     }
 }
        private void ImportAccount(MyMoney newMoney, AccountImportState a, AttachmentManager newAttachments)
        {
            Account             acct         = a.Account;
            IList <Transaction> transactions = newMoney.Transactions.GetTransactionsFrom(acct);

            a.PercentComplete = 0;
            a.Loading         = true;
            List <Transaction> changed = new List <Transaction>();

            a.Changed = changed;
            this.myMoney.BeginUpdate(this);
            StringBuilder sb = new StringBuilder();

            List <Transaction> newTransactions = new List <Transaction>();

            for (int i = 0, n = transactions.Count; i < n; i++)
            {
                double percent = ((double)i * 100.0) / (double)n;
                a.PercentComplete = (int)percent;
                Transaction t = transactions[i];
                Transaction u = this.myMoney.Transactions.FindTransactionById(t.Id);

                try {
                    if (u == null)
                    {
                        newTransactions.Add(t);
                    }
                    else if (u.Merge(t))
                    {
                        a.Modified++;
                        changed.Add(u);
                        // make the amounts the same
                        u.Amount = t.Amount;
                        List <string> attachments = newAttachments.GetAttachments(t);
                        this.myAttachments.ImportAttachments(t, attachments);
                    }
                }
                catch (Exception ex)
                {
                    // todo: handle errors.
                    sb.Append(ex);
                }
            }

            // must be done last so we don't get confused over mismatching transaction ids.
            foreach (Transaction t in newTransactions)
            {
                try
                {
                    Account target = this.myMoney.Accounts.FindAccount(t.AccountName);
                    if (target == null)
                    {
                        throw new Exception("Cannot import transaction to missing account: " + t.AccountName);
                    }
                    else
                    {
                        Transaction u = this.myMoney.Transactions.NewTransaction(target);
                        u.Merge(t);
                        a.Modified++;
                        changed.Add(u);
                        this.myMoney.Transactions.Add(u);
                        // make the amounts the same
                        u.Amount = t.Amount;
                        List <string> attachments = newAttachments.GetAttachments(t);
                        this.myAttachments.ImportAttachments(t, attachments);
                    }
                }
                catch (Exception ex)
                {
                    // todo: handle errors.
                    sb.Append(ex);
                }
            }


            this.myMoney.EndUpdate();

            a.Loading = false;
            a.Done    = true;

            if (sb.Length > 0)
            {
                MessageBoxEx.Show(sb.ToString(), "Import Errors", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }