-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRedmineMySqlConfig.cs
140 lines (126 loc) · 5.68 KB
/
RedmineMySqlConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using MySql.Data.MySqlClient;
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace combit.RedmineReports
{
public partial class RedmineMySqlConfig : Form
{
private RedmineReportsForm redmineReportsForm;
public RedmineMySqlConfig(RedmineReportsForm redmineReportsForm)
{
this.redmineReportsForm = redmineReportsForm;
InitializeComponent();
CompleteBoxes();
}
private void CompleteBoxes()
{
ConfigurationManager.RefreshSection("combit.RedmineReports.Properties.Settings.RedmineConnectionString");
string convertString = ConfigurationManager.ConnectionStrings["combit.RedmineReports.Properties.Settings.RedmineConnectionString"].ConnectionString;
if(RedmineReportsConfigDataHelper.ConnectionStringEncrypted(convertString))
{
//decrypt connectionstring
convertString =RedmineReportsConfigDataHelper.DecryptData(convertString);
}
Match m = Regex.Match(convertString, "server=([^;]*);uid=([^;]*);pwd=([^;]*);database=([^;]*);port=([^;]*);");
ipAddressTextBox.Text = m.Groups[1].Value;
mySQLLogTextBox.Text = m.Groups[2].Value;
mySQLPasssTextBox.Text = m.Groups[3].Value;
dbNameTextBox.Text = m.Groups[4].Value;
mySqlPortTextBox.Text = m.Groups[5].Value;
}
private void checkConfButton_Click(object sender, EventArgs e)
{
if (checkConfButton.Text.Equals("Check configuration"))
{
checkConfButton.Enabled = false;
pictureBox1.Visible = true;
var args = Tuple.Create<string, string, string, string,string, bool>
(ipAddressTextBox.Text, mySQLLogTextBox.Text, mySQLPasssTextBox.Text,
dbNameTextBox.Text, mySqlPortTextBox.Text, encryptData.Checked);
backgroundWorker1.RunWorkerAsync(args);
}
else
{
this.backgroundWorker1.CancelAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Tuple<string, string, string, string, string, bool> args = e.Argument as Tuple<string, string, string, string, string, bool>;
// create connection
IDbConnection _connection = new MySqlConnection();
_connection.ConnectionString = string.Format("server={0};uid={1};pwd={2};database={3};port={4};",
args.Item1, args.Item2, args.Item3, args.Item4, args.Item5);
try
{
_connection.Open();
_connection.Close();
_connection = null;
MessageBox.Show("Connection succeeded.", "Test connection", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
e.Cancel = true;
}
}
// This event handler deals with the results of the
// background operation.
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
}
else
{
saveButton.Enabled = true;
}
checkConfButton.Text = "Check configuration";
checkConfButton.Enabled = true;
pictureBox1.Visible = false;
}
private void DisableSaveButton(object sender, EventArgs e)
{
saveButton.Enabled = false;
}
private void saveButton_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration("RedmineReports.exe");
ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
string convertString = connectionStringsSection.ConnectionStrings["combit.RedmineReports.Properties.Settings.RedmineConnectionString"].ConnectionString =
string.Format("server={0};uid={1};pwd={2};database={3};port={4};",
ipAddressTextBox.Text, mySQLLogTextBox.Text, mySQLPasssTextBox.Text,
dbNameTextBox.Text, mySqlPortTextBox.Text);
if (encryptData.Checked)
{
//encrpyt connectionstring
connectionStringsSection.ConnectionStrings["combit.RedmineReports.Properties.Settings.RedmineConnectionString"].ConnectionString = RedmineReportsConfigDataHelper.EncryptData(convertString);
}
else
{
connectionStringsSection.ConnectionStrings["combit.RedmineReports.Properties.Settings.RedmineConnectionString"].ConnectionString = convertString;
}
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
saveButton.Enabled = false;
redmineReportsForm.reloadCmb(string.Format("server={0};uid={1};pwd={2};database={3};port={4};",
ipAddressTextBox.Text, mySQLLogTextBox.Text, mySQLPasssTextBox.Text,
dbNameTextBox.Text, mySqlPortTextBox.Text));
this.DialogResult = System.Windows.Forms.DialogResult.OK;
Close();
}
private void cancelButton_Click_1(object sender, EventArgs e)
{
Close();
}
}
}