PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ZaaksysteemUploadConfigurator/Forms/ConfigurationListForm.cs

https://gitlab.com/martijnvds/zaaksysteemuploadservice
C# | 185 lines | 140 code | 28 blank | 17 comment | 10 complexity | 7cf34547618bf8fa13145c845a7d8817 MD5 | raw file
  1. #region Software License
  2. /*
  3. Copyright (c) 2016, Mintlab B.V. and all the persons listed in the
  4. CONTRIBUTORS file.
  5. Zaaksysteem Upload Service uses the EUPL license, for more information
  6. please have a look at the LICENSE file.
  7. */
  8. #endregion
  9. using System;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Security;
  12. using System.Windows.Forms;
  13. using ZaaksysteemUpload;
  14. namespace ZaaksysteemUploadConfigurator
  15. {
  16. public partial class ConfigurationListForm : Form
  17. {
  18. public IConfigurationService ServiceConnection;
  19. ConfigurationData ConfigData;
  20. BindingSource ConfigItemSource;
  21. public ConfigurationListForm()
  22. {
  23. InitializeComponent();
  24. }
  25. private void ConfigurationListForm_Load(object sender, EventArgs e)
  26. {
  27. try
  28. {
  29. ReloadConfiguration();
  30. }
  31. catch (EndpointNotFoundException)
  32. {
  33. MessageBox.Show(
  34. "Could not contact Zaaksysteem Upload Service. Is it running?",
  35. "Error contacting service",
  36. MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1
  37. );
  38. Application.Exit();
  39. }
  40. catch (SecurityAccessDeniedException)
  41. {
  42. MessageBox.Show(
  43. "Access to Zaaksysteem Upload Service denied.\n\nMake sure you're in the 'Administrators' group.",
  44. "Access Denied",
  45. MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1
  46. );
  47. Application.Exit();
  48. }
  49. configListBox.DisplayMember = "DisplayName";
  50. configListBox.ValueMember = "Name";
  51. // Make sure "Edit" and "Remove" buttons are disabled if no configuration is selected.
  52. toggleButtonStates();
  53. // Only add this *after* setting the initial value. Otherwise,
  54. // just starting the configuration program triggers a save/restart
  55. // on the server side.
  56. this.timeoutMinutes.ValueChanged += new System.EventHandler(this.timeoutMinutes_ValueChanged);
  57. }
  58. private void removeButton_Click(object sender, EventArgs e)
  59. {
  60. if (configListBox.SelectedIndex != -1)
  61. {
  62. // Remove configuration configListBox.SelectedIndex from the stored configuration
  63. ConfigItemSource.RemoveAt(configListBox.SelectedIndex);
  64. ServiceConnection.SetConfiguration(ConfigData);
  65. ReloadConfiguration();
  66. }
  67. }
  68. private void addButton_Click(object sender, EventArgs e)
  69. {
  70. // Show a form to create a new configuration
  71. var cdi = new ConfigurationDataItem();
  72. if (ShowConfigurationForm(cdi))
  73. {
  74. ConfigItemSource.Add(cdi);
  75. ServiceConnection.SetConfiguration(ConfigData);
  76. ReloadConfiguration();
  77. }
  78. }
  79. private void editButton_Click(object sender, EventArgs e)
  80. {
  81. var cdi = (ConfigurationDataItem)configListBox.SelectedItem;
  82. if (ShowConfigurationForm(cdi))
  83. {
  84. ServiceConnection.SetConfiguration(ConfigData);
  85. ReloadConfiguration();
  86. }
  87. }
  88. private void configListBox_DoubleClick(object sender, EventArgs e)
  89. {
  90. editButton.PerformClick();
  91. }
  92. private void configListBox_SelectedIndexChanged(object sender, EventArgs e)
  93. {
  94. toggleButtonStates();
  95. }
  96. private void toggleButtonStates()
  97. {
  98. if (configListBox.SelectedIndex == -1)
  99. {
  100. deleteButton.Enabled = false;
  101. editButton.Enabled = false;
  102. }
  103. else
  104. {
  105. deleteButton.Enabled = true;
  106. editButton.Enabled = true;
  107. }
  108. }
  109. private bool ShowConfigurationForm(ConfigurationDataItem cdi)
  110. {
  111. var cf = new ConfigurationForm {
  112. ConfigDataItem = cdi,
  113. ConfigList = ConfigData.ConfigurationItems
  114. };
  115. // Show testDialog as a modal dialog and determine if DialogResult = OK.
  116. if (cf.ShowDialog(this) == DialogResult.OK)
  117. {
  118. // Gather the new configuration bits from the UI.
  119. cdi.Name = cf.configurationNameEntry.Text;
  120. cdi.APIKey = cf.apiKeyEntry.Text;
  121. cdi.WatchPath = cf.selectedFolder.Text;
  122. cdi.PostURL = cf.zaaksysteemUrlEntry.Text;
  123. // Assume configuration is valid, until service proves otherwise.
  124. cdi.Valid = true;
  125. }
  126. else
  127. {
  128. // Form was cancelled
  129. return false;
  130. }
  131. cf.Dispose();
  132. return true;
  133. }
  134. private void ReloadConfiguration()
  135. {
  136. ConfigData = ServiceConnection.GetConfiguration();
  137. timeoutMinutes.Value = ConfigData.HTTPTimeoutMinutes;
  138. var oldConfigItemSource = ConfigItemSource;
  139. ConfigItemSource = new BindingSource { DataSource = ConfigData.ConfigurationItems };
  140. configListBox.DataSource = ConfigItemSource;
  141. if (oldConfigItemSource != null) { oldConfigItemSource.Dispose(); };
  142. }
  143. private void timeoutMinutes_ValueChanged(object sender, EventArgs e)
  144. {
  145. ConfigData.HTTPTimeoutMinutes = Convert.ToUInt32(timeoutMinutes.Value);
  146. ServiceConnection.SetConfiguration(ConfigData);
  147. }
  148. private void aboutButton_Click(object sender, EventArgs e)
  149. {
  150. using(var about = new AboutBox())
  151. {
  152. about.ShowDialog();
  153. }
  154. }
  155. }
  156. }