Ejemplo n.º 1
0
        private async void ButtonOk_Click(object sender, EventArgs e)
        {
            try
            {
                ProgressBarImport.Visible = true;
                ButtonOk.Visible          = false;
                ButtonCancel.Visible      = false;
                ProgressBarImport.Value   = 0;

                string url = TextBoxUrl.Text;

                if (url.EndsWith("/"))
                {
                    url = url.Substring(0, url.Length - 1);
                }

                if (previousUrl != url)
                {
                    previousCookieContainer = new CookieContainer();
                }

                HttpClient client = new HttpClient(new HttpClientHandler()
                {
                    CookieContainer = previousCookieContainer
                });
                HttpResponseMessage response;

                ProgressBarImport.Value = 25;

                if (!CheckBoxExistingAuth.Checked)
                {
                    HttpContent content = new StringContent(JsonConvert.SerializeObject(new LoginData()
                    {
                        Name     = TextBoxName.Text,
                        Password = TextBoxPassword.Text
                    }, serializerSettings), Encoding.UTF8, "application/json");

                    response = await client.PostAsync(url + "/mods", content);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception("Failed to import from remote: Login failed: (" + response.StatusCode + ")" + response.ReasonPhrase);
                    }
                }
                else
                {
                    await Task.Delay(100);
                }

                ProgressBarImport.Value = 50;

                response = await client.GetAsync(url + "/standings");

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception("Failed to import from remote: (" + response.StatusCode + ")" + response.ReasonPhrase);
                }
                await Task.Delay(100);

                ProgressBarImport.Value = 75;

                Scoreboard = JsonHandler.LoadFromFile(await response.Content.ReadAsStreamAsync());

                await Task.Delay(100);

                ProgressBarImport.Value = 100;

                previousUrl = url;

                await Task.Delay(100);

                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                log.Error("Failed to import from remote.", ex);
                MessageBox.Show("Failed to import from remote: " + ex.Message, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                ProgressBarImport.Visible = false;
                ButtonOk.Visible          = true;
                ButtonCancel.Visible      = true;
            }
        }
Ejemplo n.º 2
0
        private void MenuFileNewFromTemplate_Click(object sender, EventArgs e)
        {
            log.Debug("EditorForm::MenuFileNewFromTemplate_Click() {");

            if (hasUnsavedChanges)
            {
                try
                {
                    DialogResult result = MessageBox.Show("There are unsaved changes. Save?", "Information",
                                                          MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);

                    switch (result)
                    {
                    case DialogResult.Yes:
                        if (!SaveToFile())
                        {
                            log.Debug("EditorForm::MenuFileNewFromTemplate_Click() } // cancelled (save existing)");
                            return;
                        }
                        break;

                    case DialogResult.No:
                        break;

                    default:
                    case DialogResult.Cancel:
                        log.Debug("EditorForm::MenuFileNewFromTemplate_Click() } // cancelled (save existing)");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    log.Error("Failed to import template: Failed to save existing scoreboard.", ex);
                    MessageBox.Show("Failed to import template: Failed to save existing scoreboard: " + ex.Message, "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    log.Debug("EditorForm::MenuFileNewFromTemplate_Click() }");
                    return;
                }
            }

            try
            {
                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    openFileDialog.InitialDirectory = string.IsNullOrEmpty(fileName) ? Environment.SpecialFolder.MyDocuments.ToString() : fileName;
                    openFileDialog.Filter           = "CSV files (*.csv)|*.csv|JSON files (*.json)|*.json";
                    openFileDialog.FilterIndex      = 1;
                    openFileDialog.RestoreDirectory = true;

                    Scoreboard template;

                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        fileName = openFileDialog.FileName;
                        switch (Path.GetExtension(fileName))
                        {
                        case ".csv":
                            template = CsvHandler.LoadFromFile(openFileDialog.OpenFile());
                            break;

                        case ".json":
                            template = JsonHandler.LoadFromFile(openFileDialog.OpenFile());
                            break;

                        default:
                            throw new ArgumentException("Unknown file format: '" + Path.GetExtension(fileName) + "'.");
                        }
                        hasUnsavedChanges = false;

                        scoreboard = new Scoreboard(template.ExpectedNumberOfTeams, template.ExpectedNumberOfCategories);
                        foreach (string team in template.Teams)
                        {
                            scoreboard.AddTeam(team);
                        }
                        NumericInputAnimationLength.Value = GetSuggestedAnimationLength();
                        UpdateScores();
                        UpdateControls();
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Failed to import template.", ex);
                MessageBox.Show("Failed to import template: " + ex.Message, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            log.Debug("EditorForm::MenuFileNewFromTemplate_Click() }");
        }