00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.IO;
00014 using System.Windows.Forms;
00015
00016 namespace Mcs.Epm.MicrosoftProject.mpFx.WinForms
00017 {
00018
00019
00020
00021 public partial class CredentialsForm : Form
00022 {
00023 #region Instance Data
00024
00025 private readonly string _ProfilesPath;
00026 private readonly bool _ImportProjectProfiles;
00027 private ProjectProfiles _ProjectProfiles;
00028 private ProjectProfiles.ProjectProfile _LastProfile;
00029
00030 #endregion
00031
00032 #region Public Properties
00033
00034 public ProjectProfiles.ProjectProfile ProjectProfile { get; private set; }
00035
00036 public string ProfileName { get; private set; }
00037 public string ProfileUrl { get; private set; }
00038 public string ProfileUserName { get; private set; }
00039 public string ProfilePassword { get; private set; }
00040 public bool UseDefaultCredentials { get; private set; }
00041
00042 #endregion
00043
00044 #region Constructors
00045
00046 public CredentialsForm(string profilesPath, bool importProjectProfiles, ProjectProfiles.ProjectProfile lastProfile)
00047 {
00048 InitializeComponent();
00049
00050 _ProfilesPath = profilesPath;
00051 _ImportProjectProfiles = importProjectProfiles;
00052 _LastProfile = lastProfile;
00053 }
00054
00055 #endregion
00056
00057 #region Public Methods
00058
00059 public void LoadUserProfiles(string path)
00060 {
00061 if (File.Exists(path))
00062 {
00063 using (StreamReader textReader = File.OpenText(path))
00064 {
00065 string buffer;
00066
00067 while ((buffer = textReader.ReadLine()) != null)
00068 {
00069 string[] profileComponents = buffer.Split('~');
00070
00071 if (profileComponents.Length == 4)
00072 {
00073 if (_ProjectProfiles.FindByName(profileComponents[0]) == null)
00074 {
00075 _ProjectProfiles.Profiles.Add(new ProjectProfiles.ProjectProfile(profileComponents[0],
00076 profileComponents[1],
00077 profileComponents[2],
00078 false));
00079 profileList.Items.Add(profileComponents[0]);
00080 }
00081 }
00082 }
00083 }
00084 }
00085 else
00086 {
00087 File.Create(path).Dispose();
00088 }
00089 }
00090 #endregion
00091
00092 #region Private Methods
00093
00094 private void SaveProfiles()
00095 {
00096 _ProjectProfiles.Save(_ProfilesPath);
00097 }
00098
00099 #endregion
00100
00101 #region Event Handlers
00102
00103 private void CredentialsForm_Load(object sender, EventArgs e)
00104 {
00105 if (_ImportProjectProfiles)
00106 {
00107 _ProjectProfiles = ProjectServer.ImportProjectProfiles();
00108
00109 foreach (ProjectProfiles.ProjectProfile profile in _ProjectProfiles.Profiles)
00110 {
00111 profileList.Items.Add(profile.Name);
00112 }
00113 }
00114 else
00115 {
00116 _ProjectProfiles = new ProjectProfiles(false);
00117 }
00118
00119 LoadUserProfiles(_ProfilesPath);
00120
00121 if (_LastProfile != null)
00122 {
00123 int profileIndex = profileList.FindString(_LastProfile.Name);
00124 if (profileIndex != -1)
00125 {
00126 profileList.SelectedIndex = profileIndex;
00127 return;
00128 }
00129 }
00130
00131 if (profileList.Items.Count > 0)
00132 {
00133 profileList.SelectedIndex = 0;
00134 }
00135 }
00136
00137 private void okButton_Click(object sender, EventArgs e)
00138 {
00139 if (useDefaultCredentialsCheckBox.Checked)
00140 {
00141 UseDefaultCredentials = true;
00142 ProfilePassword = string.Empty;
00143 }
00144 else
00145 {
00146 if (userNameTextBox.Text == string.Empty)
00147 {
00148 MessageBox.Show(this,
00149 @"Please provide a domain and user name (DOMAIN\UserName) or just a user name if connecting to a machine not belonging to a domain.",
00150 Text,
00151 MessageBoxButtons.OK,
00152 MessageBoxIcon.Information);
00153
00154 userNameTextBox.Select();
00155 DialogResult = DialogResult.None;
00156 return;
00157 }
00158
00159 UseDefaultCredentials = false;
00160 ProfilePassword = passwordTextBox.Text;
00161 }
00162
00163 if (profileList.SelectedIndex == -1)
00164 {
00165 MessageBox.Show(this,
00166 "Please choose a profile.",
00167 Text,
00168 MessageBoxButtons.OK,
00169 MessageBoxIcon.Information);
00170
00171 DialogResult = DialogResult.None;
00172 return;
00173 }
00174
00175 ProjectProfiles.ProjectProfile profile = _ProjectProfiles.FindByName(profileList.SelectedItem.ToString());
00176
00177 if (profile == null)
00178 {
00179 MessageBox.Show(this,
00180 "Invalid profile.",
00181 Text,
00182 MessageBoxButtons.OK,
00183 MessageBoxIcon.Information);
00184
00185 DialogResult = DialogResult.None;
00186 return;
00187 }
00188
00189 ProfileName = profile.Name;
00190 ProfileUrl = profile.Url;
00191 ProfileUserName = userNameTextBox.Text;
00192 profile.UserName = userNameTextBox.Text;
00193
00194 _ProjectProfiles.Replace(profile.Name, profile);
00195
00196 ProjectProfile = profile;
00197
00198 SaveProfiles();
00199 }
00200
00201 private void profileList_SelectedIndexChanged(object sender, EventArgs e)
00202 {
00203 if (profileList.SelectedIndex != -1)
00204 {
00205 ProjectProfiles.ProjectProfile profile = _ProjectProfiles.FindByName(profileList.SelectedItem.ToString());
00206
00207 if (profile != null)
00208 {
00209 userNameTextBox.Text = profile.UserName;
00210
00211 editButton.Enabled = !profile.IsWinProjProfile;
00212 deleteButton.Enabled = !profile.IsWinProjProfile;
00213
00214 return;
00215 }
00216 }
00217 editButton.Enabled = false;
00218 deleteButton.Enabled = false;
00219 }
00220
00221 private void addButton_Click(object sender, EventArgs e)
00222 {
00223 using (ProfileForm profileForm = new ProfileForm(new ProjectProfiles.ProjectProfile(string.Empty, string.Empty, string.Empty, false)))
00224 {
00225 if (profileForm.ShowDialog(this) == DialogResult.Cancel)
00226 {
00227 return;
00228 }
00229
00230 if (_ProjectProfiles.FindByName(profileForm.Profile.Name) != null)
00231 {
00232 MessageBox.Show(this,
00233 string.Format("A profile with name '{0}' already exists.", profileForm.Profile.Name),
00234 Text,
00235 MessageBoxButtons.OK,
00236 MessageBoxIcon.Information);
00237
00238 return;
00239 }
00240
00241 profileForm.Profile.UserName = Environment.UserDomainName + @"\" + Environment.UserName;
00242
00243 _ProjectProfiles.Profiles.Add(profileForm.Profile);
00244
00245 profileList.SelectedIndex = profileList.Items.Add(profileForm.Profile.Name);
00246 }
00247 }
00248
00249 private void editButton_Click(object sender, EventArgs e)
00250 {
00251 ProjectProfiles.ProjectProfile profile = _ProjectProfiles.FindByName(profileList.SelectedItem.ToString());
00252
00253 if (profile == null)
00254 {
00255 return;
00256 }
00257 using (ProfileForm profileForm = new ProfileForm(profile))
00258 {
00259 if (profileForm.ShowDialog(this) == DialogResult.Cancel)
00260 {
00261 return;
00262 }
00263
00264 _ProjectProfiles.Replace(profileList.SelectedItem.ToString(), profileForm.Profile);
00265
00266 profileList.Items.RemoveAt(profileList.SelectedIndex);
00267 profileList.SelectedIndex = profileList.Items.Add(profileForm.Profile.Name);
00268 userNameTextBox.Text = profileForm.Profile.UserName;
00269
00270 }
00271 }
00272
00273 private void deleteButton_Click(object sender, EventArgs e)
00274 {
00275 _ProjectProfiles.Profiles.Remove(_ProjectProfiles.FindByName(profileList.SelectedItem.ToString()));
00276
00277 profileList.Items.RemoveAt(profileList.SelectedIndex);
00278
00279 if (profileList.Items.Count > 0)
00280 {
00281 profileList.SelectedIndex = 0;
00282 }
00283 else
00284 {
00285 deleteButton.Enabled = false;
00286 editButton.Enabled = false;
00287 }
00288 }
00289
00290 private void useDefaultCredentialsCheckBox_CheckedChanged(object sender, EventArgs e)
00291 {
00292 credentialsGroupBox.Enabled = !useDefaultCredentialsCheckBox.Checked;
00293 }
00294
00295 #endregion
00296 }
00297 }