Exemple #1
0
        private void CmExportCustom(object sender, RoutedEventArgs e)
        {
            if (AccountsDataGrid.SelectedItems.Count == 0)
            {
                return;
            }
            ExportWindow window = new ExportWindow(AccountsDataGrid.SelectedItems.Cast <Account>());

            window.ShowDialog();
        }
        private void CmExportCustom(object sender, RoutedEventArgs e)
        {
            if (AccountsDataGrid.SelectedItem == null)
            {
                return;
            }

            var accounts = AccountsDataGrid.SelectedItems.Cast <Account>();
            var window   = new ExportWindow(accounts);

            window.ShowDialog();
        }
        public ExportWindow(IEnumerable <Account> accounts)
        {
            InitializeComponent();

            Instance = this;

            _accounts = accounts;

            if (File.Exists("ExportFormat.txt"))
            {
                using (var sr = new StreamReader("ExportFormat.txt"))
                {
                    FormatBox.Text = sr.ReadToEnd();
                }
            }
        }
        public ExportWindow(IEnumerable<Account> accounts)
        {
            InitializeComponent();

            Instance = this;

            _accounts = accounts;

            if (File.Exists("ExportFormat.txt"))
            {
                using (var sr = new StreamReader("ExportFormat.txt"))
                {
                    FormatBox.Text = sr.ReadToEnd();
                }
            }
        }
        private void CmExportCustom(object sender, RoutedEventArgs e)
        {
            if (AccountsDataGrid.SelectedItem == null)
            {
                return;
            }

            var accounts = AccountsDataGrid.SelectedItems.Cast<Account>();
            var window = new ExportWindow(accounts);
            window.ShowDialog();
        }
        private async void ButtonClick(object sender, RoutedEventArgs e)
        {
            if (!(sender is Button))
            {
                return;
            }

            Button s = (Button)sender;

            if (s.Name == "HelpButton")
            {
                ExportWindow w = new ExportWindow("CustomExport.nfo");
                w.Show();
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog
            {
                InitialDirectory = Directory.GetCurrentDirectory()
            };

            if (s.Name == "OpenButton")
            {
                OpenFileDialog ofd = new OpenFileDialog
                {
                    InitialDirectory = Directory.GetCurrentDirectory(),
                    Filter           = "Text File(*.txt) | *.txt",
                    Multiselect      = false,
                    CheckFileExists  = true
                };
                if (ofd.ShowDialog() == true)
                {
                    if (LoadFile(ofd.FileName))
                    {
                        Settings.Config.DefaultCustomExportFilename = ofd.SafeFileName;
                    }
                }
            }
            else if (s.Name == "SaveButton")
            {
                sfd.Filter = "Text File (*.txt)|*.txt";

                if (sfd.ShowDialog() == true && !string.IsNullOrEmpty(sfd.FileName))
                {
                    using (var sw = new StreamWriter(sfd.FileName))
                    {
                        sw.Write(FormatBox.Text);
                    }
                    Settings.Config.DefaultCustomExportFilename = sfd.SafeFileName;
                    await this.ShowMessageAsync("Export Template", "Template has been successfully saved.");
                }
            }
            else if (s.Name == "ExportButton")
            {
                Regex accountBlockRegex = new Regex("\\[Account](.+?)\\[\\/Account]", RegexOptions.Singleline);
                Match accountBlock      = accountBlockRegex.Match(FormatBox.Text);

                if (string.IsNullOrEmpty(FormatBox.Text) || !accountBlock.Success)
                {
                    return;
                }

                sfd.FileName = "output";
                sfd.Filter   = "Text File (*.txt)|*.txt|Html Document (*.htm)|*.htm";

                if (new Regex(@"<[^>]+>").IsMatch(FormatBox.Text))
                {
                    sfd.DefaultExt = ".htm";
                    Utils.UseDefaultExtAsFilterIndex(sfd);
                }

                if (sfd.ShowDialog() != true || string.IsNullOrEmpty(sfd.FileName))
                {
                    return;
                }

                StringBuilder sb = new StringBuilder();

                try
                {
                    foreach (Account account in _accounts)
                    {
                        string accountTemplate = accountBlock.Groups[1].Value;
                        accountTemplate = PopulateLists(accountTemplate, account);
                        accountTemplate = accountTemplate.HenriFormat(account);

                        sb.Append(accountTemplate);
                    }

                    string output = sb.ToString().Trim(Environment.NewLine.ToCharArray());
                    output = FormatBox.Text.Replace(accountBlock.Groups[0].Value, output);

                    Regex           indexRegex   = new Regex("%index=?(\\d+)?:?(\\d+)?%");
                    MatchCollection indexMatches = indexRegex.Matches(output);

                    if (indexMatches.Count > 0)
                    {
                        int index  = 1;
                        int digits = 1;

                        string indexStartValue = indexMatches[0].Groups[1].Value;
                        if (!string.IsNullOrEmpty(indexStartValue))
                        {
                            index = int.Parse(indexStartValue);
                        }

                        string indexDigitsValue = indexMatches[0].Groups[2].Value;
                        if (!string.IsNullOrEmpty(indexDigitsValue))
                        {
                            int d = int.Parse(indexDigitsValue);
                            if (d > 0)
                            {
                                digits = d;
                            }
                        }

                        output = indexRegex.Replace(output, m => index++.ToString(new string('0', digits)));
                    }

                    using (var sw = new StreamWriter(sfd.FileName))
                    {
                        sw.Write(output);
                    }

                    await this.ShowMessageAsync("Export", $"Accounts Exported: {_accounts.Count()}");

                    Close();
                }
                catch (Exception ex)
                {
                    Utils.ExportException(ex);
                    await this.ShowMessageAsync("Export", "An error occured. Check the logs for more information.");
                }
            }
        }
        private async void ButtonClick(object sender, RoutedEventArgs e)
        {
            if (!(sender is Button))
            {
                return;
            }

            Button s = (Button) sender;

            if (s.Name == "HelpButton")
            {
                ExportWindow w = new ExportWindow("CustomExport.nfo");
                w.Show();
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog
            {
                InitialDirectory = Directory.GetCurrentDirectory()
            };

            if (s.Name == "OpenButton")
            {
                OpenFileDialog ofd = new OpenFileDialog
                {
                    InitialDirectory = Directory.GetCurrentDirectory(),
                    Filter = "Text File(*.txt) | *.txt",
                    Multiselect = false,
                    CheckFileExists = true
                };
                if (ofd.ShowDialog() == true)
                {
                    if (LoadFile(ofd.FileName))
                    {
                        Settings.Config.DefaultCustomExportFilename = ofd.SafeFileName;
                    }
                }
            }
            else if (s.Name == "SaveButton")
            {
                sfd.Filter = "Text File (*.txt)|*.txt";

                if (sfd.ShowDialog() == true && !string.IsNullOrEmpty(sfd.FileName))
                {
                    using (var sw = new StreamWriter(sfd.FileName))
                    {
                        sw.Write(FormatBox.Text);
                    }
                    Settings.Config.DefaultCustomExportFilename = sfd.SafeFileName;
                    await this.ShowMessageAsync("Export Template", "Template has been successfully saved.");
                }
            }
            else if (s.Name == "ExportButton")
            {
                Regex accountBlockRegex = new Regex("\\[Account](.+?)\\[\\/Account]", RegexOptions.Singleline);
                Match accountBlock = accountBlockRegex.Match(FormatBox.Text);

                if (string.IsNullOrEmpty(FormatBox.Text) || !accountBlock.Success)
                {
                    return;
                }

                sfd.FileName = "output";
                sfd.Filter = "Text File (*.txt)|*.txt|Html Document (*.htm)|*.htm";

                if (new Regex(@"<[^>]+>").IsMatch(FormatBox.Text))
                {
                    sfd.DefaultExt = ".htm";
                    Utils.UseDefaultExtAsFilterIndex(sfd);
                }

                if (sfd.ShowDialog() != true || string.IsNullOrEmpty(sfd.FileName))
                {
                    return;
                }

                StringBuilder sb = new StringBuilder();

                try
                {
                    foreach (Account account in _accounts)
                    {
                        string accountTemplate = accountBlock.Groups[1].Value;
                        accountTemplate = PopulateLists(accountTemplate, account);
                        accountTemplate = accountTemplate.HenriFormat(account);

                        sb.Append(accountTemplate);
                    }

                    string output = sb.ToString().Trim(Environment.NewLine.ToCharArray());
                    output = FormatBox.Text.Replace(accountBlock.Groups[0].Value, output);

                    Regex indexRegex = new Regex("%index=?(\\d+)?:?(\\d+)?%");
                    MatchCollection indexMatches = indexRegex.Matches(output);

                    if (indexMatches.Count > 0)
                    {
                        int index = 1;
                        int digits = 1;

                        string indexStartValue = indexMatches[0].Groups[1].Value;
                        if (!string.IsNullOrEmpty(indexStartValue))
                        {
                            index = int.Parse(indexStartValue);
                        }

                        string indexDigitsValue = indexMatches[0].Groups[2].Value;
                        if (!string.IsNullOrEmpty(indexDigitsValue))
                        {
                            int d = int.Parse(indexDigitsValue);
                            if (d > 0)
                            {
                                digits = d;
                            }
                        }

                        output = indexRegex.Replace(output, m => index++.ToString(new string('0', digits)));
                    }

                    using (var sw = new StreamWriter(sfd.FileName))
                    {
                        sw.Write(output);
                    }

                    await this.ShowMessageAsync("Export", $"Accounts Exported: {_accounts.Count()}");

                    Close();
                }
                catch (Exception ex)
                {
                    Utils.ExportException(ex);
                    await this.ShowMessageAsync("Export", "An error occured. Check the logs for more information.");
                }
            }
        }
 private void CmExportCustom(object sender, RoutedEventArgs e)
 {
     if (AccountsDataGrid.SelectedItems.Count == 0)
     {
         return;
     }
     ExportWindow window = new ExportWindow(AccountsDataGrid.SelectedItems.Cast<Account>());
     window.ShowDialog();
 }