private async void btnConnect_Click(object sender, RibbonControlEventArgs e) { var mockWorksheet = Globals.ThisAddIn.Application.Worksheets.Cast <Worksheet>() .SingleOrDefault(w => string.Equals(w.Name, "Mock", StringComparison.OrdinalIgnoreCase)); IKimaiServices services; try { if (string.Equals(ConfigurationManager.AppSettings["UseMocks"], "true", StringComparison.OrdinalIgnoreCase) || mockWorksheet is Worksheet) { services = new MockKimaiServices(); } else { services = new KimaiServices(); } var version = await services.GetVersion().ConfigureAwait(false); lblVersionNo.Label = version.VersionProperty; var user = await services.GetCurrentUser().ConfigureAwait(false); Globals.ThisAddIn.CurrentUser = user; btnSync.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message); ExcelAddin.Globals.ThisAddIn.Logger.LogWarning($"Could not connect to Kimai server. {ex.Message}", ex); } }
private async void btnSave_Click(object sender, EventArgs e) { if (this.ValidateChildren()) { Globals.ThisAddIn.ApiUrl = txtAPIUrl.Text; Globals.ThisAddIn.ApiUsername = txtApiUsername.Text; Globals.ThisAddIn.ApiPassword = txtApiPassword.Text; var mockWorksheet = Globals.ThisAddIn.Application.Worksheets.Cast <Worksheet>() .SingleOrDefault(w => string.Equals(w.Name, "Mock", StringComparison.OrdinalIgnoreCase)); IKimaiServices services; if (string.Equals(ConfigurationManager.AppSettings["UseMocks"], "true", StringComparison.OrdinalIgnoreCase) || mockWorksheet is Worksheet) { services = new MockKimaiServices(); } else { services = new KimaiServices(); } try { var version = await services.GetVersion(); var user = await services.GetCurrentUser(); ExcelAddin.Globals.ThisAddIn.Logger.LogInformation("Connected to Kimai", version); Globals.Ribbons.GetRibbon <KimaiRibbon>().lblVersionNo.Label = version.VersionProperty; Globals.Ribbons.GetRibbon <KimaiRibbon>().btnSync.Enabled = true; Globals.ThisAddIn.CurrentUser = user; Do(lblConnectionStatus, rb => rb.ForeColor = Color.Green); Do(lblConnectionStatus, rb => rb.Text = $"Success"); } catch (HttpOperationException hoex) when(hoex.Response.StatusCode == System.Net.HttpStatusCode.Forbidden) { Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: Username or Password Incorrect {Environment.NewLine} {hoex.Message}"); } catch (HttpOperationException hoex) { Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: {hoex.Message}"); } catch (HttpRequestException hrex) { Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: Server name incorrect {Environment.NewLine} {hrex.Message}"); } catch (Exception ex) { // i'm using a little helper method here... // https://stackoverflow.com/questions/31007145/asynchronous-ui-updates-in-winforms Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: {ex.Message}"); } } }
private async void btnTestConnection_Click(object sender, EventArgs e) { if (this.ValidateChildren()) { var APIUrl = txtAPIUrl.Text; var userName = txtApiUsername.Text; var password = txtApiPassword.Text; var mockWorksheet = Globals.ThisAddIn.Application.Worksheets.Cast <Worksheet>() .SingleOrDefault(w => string.Equals(w.Name, "Mock", StringComparison.OrdinalIgnoreCase)); IKimaiServices services; if (string.Equals(ConfigurationManager.AppSettings["UseMocks"], "true", StringComparison.OrdinalIgnoreCase) || mockWorksheet is Worksheet) { services = new MockKimaiServices(userName, password, APIUrl); } else { services = new KimaiServices(userName, password, APIUrl); } try { _ = await services.GetPing().ConfigureAwait(false); Do(lblConnectionStatus, rb => rb.ForeColor = Color.Green); Do(lblConnectionStatus, rb => rb.Text = $"Success: API ping call "); } catch (HttpOperationException hoex) when(hoex.Response.StatusCode == System.Net.HttpStatusCode.Forbidden) { Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: Username or Password Incorrect {Environment.NewLine} {hoex.Message}"); } catch (HttpOperationException hoex) { Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: {hoex.Message}"); } catch (HttpRequestException hrex) { Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: Server name incorrect {Environment.NewLine} {hrex.Message}"); } catch (Exception ex) { // i'm using a little helper method here... // https://stackoverflow.com/questions/31007145/asynchronous-ui-updates-in-winforms Do(lblConnectionStatus, rb => rb.ForeColor = Color.Red); Do(lblConnectionStatus, rb => rb.Text = $"Failed: {ex.Message}"); } } }