-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormProfiles.cs
270 lines (225 loc) · 7.99 KB
/
FormProfiles.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BandR;
using BandR.CustObjs;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace SPFileZilla2013
{
public partial class FormProfiles : Form
{
public Form1 form1;
internal List<ProfileDetail> lstProfiles;
/// <summary>
/// </summary>
public FormProfiles()
{
InitializeComponent();
this.Closed += new EventHandler(FormProfiles_Closed);
// retrieve saved profiles from file, put in listbox
lstProfiles = new List<ProfileDetail>();
GetRecentSessionInfo();
LoadProfilesIntoListbox();
}
/// <summary>
/// </summary>
void FormProfiles_Closed(object sender, EventArgs e)
{
// save profiles to file
SaveCurrentSessionInfo();
}
/// <summary>
/// </summary>
private void SaveCurrentSessionInfo()
{
StreamWriter sw = null;
try
{
string iniPath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }) + "profiles.dat";
sw = new StreamWriter(iniPath, false);
var xml = XmlSerialization.Serialize(lstProfiles);
xml = GenUtil.Cypher(xml);
sw.Write(xml);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
if (sw != null) sw.Dispose();
}
}
/// <summary>
/// </summary>
private void GetRecentSessionInfo()
{
StreamReader sr = null;
try
{
string iniPath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }) + "profiles.dat";
var fi = new FileInfo(iniPath);
if (fi.Exists)
{
sr = new StreamReader(iniPath);
var content = sr.ReadToEnd();
content = GenUtil.Cypher(content);
lstProfiles = XmlSerialization.Deserialize<List<ProfileDetail>>(content);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
if (sr != null) sr.Dispose();
}
}
/// <summary>
/// </summary>
private void LoadProfilesIntoListbox()
{
lbProfiles.Items.Clear();
foreach (ProfileDetail profile in lstProfiles.OrderBy(x => x.profileName))
{
lbProfiles.Items.Add(profile.profileName);
}
}
/// <summary>
/// </summary>
private void lbProfiles_SelectedIndexChanged(object sender, EventArgs e)
{
// load selected profile details
if (lbProfiles.SelectedIndex < 0)
{
return;
}
var profName = lbProfiles.SelectedItem.ToString();
if (GenUtil.IsNull(profName))
{
return;
}
var prof = lstProfiles.FirstOrDefault(x => x.profileName.Trim().ToLower() == profName.Trim().ToLower());
if (prof == null)
{
return;
}
tbProfileName.Text = prof.profileName;
tbSiteUrl.Text = prof.siteUrl;
tbUsername.Text = prof.username;
tbPassword.Text = prof.password;
tbDomain.Text = prof.domain;
cbIsSharePointOnline.Checked = prof.isSpOnline;
}
/// <summary>
/// </summary>
private void btnSaveProfile_Click(object sender, EventArgs e)
{
// save profile details
var newProfile = new ProfileDetail();
newProfile.profileName = tbProfileName.Text.Trim();
newProfile.siteUrl = tbSiteUrl.Text.Trim();
newProfile.username = tbUsername.Text.Trim();
newProfile.password = tbPassword.Text.Trim();
newProfile.domain = tbDomain.Text.Trim();
newProfile.isSpOnline = cbIsSharePointOnline.Checked;
if (GenUtil.IsNull(newProfile.profileName))
{
MessageBox.Show("Profile name is required.", "Error");
return;
}
if (GenUtil.IsNull(newProfile.siteUrl))
{
MessageBox.Show("Site url is required.", "Error");
return;
}
if (GenUtil.IsNull(newProfile.username))
{
MessageBox.Show("Username is required.", "Error");
return;
}
if (GenUtil.IsNull(newProfile.password))
{
MessageBox.Show("Password is required.", "Error");
return;
}
if (!cbIsSharePointOnline.Checked && GenUtil.IsNull(newProfile.domain))
{
MessageBox.Show("Domain is required.", "Error");
return;
}
// get matching profile or create new
var existingProfile = lstProfiles.FirstOrDefault(x => x.profileName.Trim().ToLower() == newProfile.profileName.Trim().ToLower());
if (existingProfile == null)
{
lstProfiles.Add(newProfile);
LoadProfilesIntoListbox();
}
else
{
existingProfile.profileName = newProfile.profileName;
existingProfile.siteUrl = newProfile.siteUrl;
existingProfile.username = newProfile.username;
existingProfile.password = newProfile.password;
existingProfile.domain = newProfile.domain;
existingProfile.isSpOnline = newProfile.isSpOnline;
}
}
/// <summary>
/// </summary>
private void btnDeleteProfile_Click(object sender, EventArgs e)
{
DialogResult dgResult = MessageBox.Show("Are you sure?",
"Delete Profile",
MessageBoxButtons.YesNo);
if (dgResult != DialogResult.Yes)
{
return;
}
// delete profile, reload listbox
var profName = tbProfileName.Text.Trim();
lstProfiles.RemoveAll(x => x.profileName.Trim().ToLower() == profName.Trim().ToLower());
LoadProfilesIntoListbox();
btnAddProfile_Click(null, null);
}
/// <summary>
/// </summary>
private void btnAddProfile_Click(object sender, EventArgs e)
{
// create new profile and load its details
tbProfileName.Text = "";
tbSiteUrl.Text = "";
tbUsername.Text = "";
tbPassword.Text = "";
tbDomain.Text = "";
cbIsSharePointOnline.Checked = false;
lbProfiles.SelectedIndex = -1;
}
/// <summary>
/// </summary>
private void btnConnectProfile_Click(object sender, EventArgs e)
{
// save profile first
btnSaveProfile_Click(null, null);
// get saved profile
var prof = lstProfiles.FirstOrDefault(x => x.profileName.Trim().ToLower() == tbProfileName.Text.Trim().ToLower());
if (prof == null)
{
return;
}
form1.LoadProfile(prof);
this.Hide();
form1.Show();
form1.Focus();
this.Close();
}
}
}