00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Diagnostics;
00014 using System.Drawing;
00015 using System.IO;
00016 using System.Security.Principal;
00017 using System.Windows.Forms;
00018 using Mcs.Epm.MicrosoftProject.mpFx.Client.Properties;
00019 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared;
00020 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared.Forms;
00021 using Mcs.Epm.MicrosoftProject.mpFx.ProjectsWebService;
00022 using Mcs.Epm.MicrosoftProject.mpFx.WinForms;
00023
00024 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Forms
00025 {
00026
00027
00028
00029 public partial class MainForm : Form
00030 {
00031 #region Constants
00032
00033 private const string PLUGIN_PUBLISHER_PLUGIN_GUID = "{2EB58C93-D11E-47c1-BB16-4C865D527A10}";
00034
00035 #endregion
00036
00037 #region Instance Data
00038
00039 private ProjectServer _ProjectServer;
00040
00041 private WindowsIdentity _Identity;
00042 private WindowsImpersonationContext _ImpersonationContext;
00043 private IntPtr _UserToken;
00044
00045 private PluginHost _PluginHost;
00046 private PluginGalleryBrowserForm _PluginGalleryBrowserForm;
00047
00048 #endregion
00049
00050 #region Constructors
00051
00052
00053
00054
00055 public MainForm()
00056 {
00057 InitializeComponent();
00058
00059 MdiClient mdiClient = null;
00060
00061 foreach (Control control in Controls)
00062 {
00063 mdiClient = control as MdiClient;
00064
00065 if (mdiClient != null)
00066 {
00067 break;
00068 }
00069 }
00070
00071 if (mdiClient != null)
00072 {
00073 mdiClient.BackColor = Color.White;
00074 }
00075 }
00076
00077 #endregion
00078
00079 #region Private Methods
00080
00081
00082
00083
00084 private void UpdateLoginUserInterface()
00085 {
00086 if (formsAuthenticationRadioButton.Checked)
00087 {
00088 formsCredentialsPanel.Enabled = true;
00089 loginButton.Enabled = (_ProjectServer == null ||
00090 _ProjectServer.AuthenticationType == AuthenticationType.NotAuthenticated) &&
00091 projectServerUrlTextBox.Text != string.Empty &&
00092 userNameTextBox.Text != string.Empty;
00093 }
00094 else
00095 {
00096 formsCredentialsPanel.Enabled = !useDefaultCredentialsCheckBox.Checked;
00097 loginButton.Enabled = (_ProjectServer == null ||
00098 _ProjectServer.AuthenticationType == AuthenticationType.NotAuthenticated) &&
00099 projectServerUrlTextBox.Text != string.Empty;
00100 }
00101 }
00102
00103
00104
00105
00106 private void Logon()
00107 {
00108 SetStatusText("Logging in...");
00109
00110 Cursor = Cursors.WaitCursor;
00111 mainToolStrip.Enabled = false;
00112 loginButton.Enabled = false;
00113 logoffButton.Enabled = false;
00114
00115 CleanUp();
00116
00117 try
00118 {
00119 if (formsAuthenticationRadioButton.Checked)
00120 {
00121 _ProjectServer = new ProjectServer(projectServerUrlTextBox.Text,
00122 userNameTextBox.Text,
00123 passwordTextBox.Text,
00124 DataStoreEnum.WorkingStore,
00125 null);
00126 }
00127 else
00128 {
00129 if (!useDefaultCredentialsCheckBox.Checked)
00130 {
00131 if (!Impersonate())
00132 {
00133 throw new OperationCanceledException();
00134 }
00135 }
00136
00137 _ProjectServer = new ProjectServer(projectServerUrlTextBox.Text, DataStoreEnum.WorkingStore, null);
00138 }
00139
00140 SaveLoginSettings();
00141
00142 if (hideLoginPanelOnSuccessCheckbox.Checked)
00143 {
00144 ToggleLoginPanel();
00145 }
00146
00147 string loggedIn;
00148
00149 if (useDefaultCredentialsCheckBox.Checked)
00150 {
00151 loggedIn = string.Format("{0} - Logged onto {1}", Environment.UserName, _ProjectServer.Site);
00152 }
00153 else
00154 {
00155 loggedIn = string.Format("{0} - Logged onto {1}", userNameTextBox.Text, _ProjectServer.Site);
00156 }
00157
00158 loginInfToolStripStatusLabel.Text = loggedIn;
00159 informationToolStripDropDownButton.Visible = true;
00160
00161 LoadPlugins();
00162
00163 }
00164 catch (OperationCanceledException)
00165 {
00166 CleanUp();
00167 }
00168 catch (Exception exception)
00169 {
00170 CleanUp();
00171
00172 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00173 Debug.Print(exception.GetBaseException().GetType().Name);
00174 }
00175 finally
00176 {
00177 SetStatusText(string.Empty);
00178 Cursor = Cursors.Default;
00179 mainToolStrip.Enabled = true;
00180 loginButton.Enabled = _ProjectServer == null;
00181 logoffButton.Enabled = !loginButton.Enabled;
00182 }
00183 }
00184
00185
00186
00187
00188 private void LoadPlugins()
00189 {
00190 SetStatusText("Loading plugins");
00191
00192 _PluginHost = new PluginHost(_ProjectServer, this, mainToolStrip, Application.StartupPath);
00193 _PluginHost.OnStatusChanged += _PluginHost_OnStatusChanged;
00194
00195 try
00196 {
00197 _PluginHost.Load();
00198 }
00199 catch (MpFxException exception)
00200 {
00201 MessageBox.Show(this, exception.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00202 }
00203
00204 if (_PluginHost.Plugins.Count == 1)
00205 {
00206
00207 if (_PluginHost.Plugins.ContainsKey(new Guid(PLUGIN_PUBLISHER_PLUGIN_GUID)))
00208 {
00209 ShowPluginGalleryBrowser();
00210 }
00211 }
00212 }
00213
00214
00215
00216
00217 private void SaveLoginSettings()
00218 {
00219 Settings.Default.LastAuthenticationType = _ProjectServer.AuthenticationType;
00220 Settings.Default.LastProjectServerUrl = projectServerUrlTextBox.Text;
00221 Settings.Default.LastUserName = userNameTextBox.Text;
00222 Settings.Default.UseDefaultCredentials = useDefaultCredentialsCheckBox.Checked;
00223 Settings.Default.Save();
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 private bool Impersonate()
00235 {
00236 int index = userNameTextBox.Text.IndexOf('\\');
00237
00238 string domain;
00239 string userName;
00240
00241 if (index == -1)
00242 {
00243 domain = ".";
00244
00245 userName = userNameTextBox.Text;
00246 }
00247 else
00248 {
00249 domain = userNameTextBox.Text.Substring(0, index);
00250 userName = userNameTextBox.Text.Substring(index + 1);
00251 }
00252
00253 if (Win32Impersonation.LogonUser(userName,
00254 domain,
00255 passwordTextBox.Text,
00256 Win32Impersonation.LogonSessionType.NewCredentials,
00257 out _UserToken))
00258 {
00259 _Identity = new WindowsIdentity(_UserToken);
00260 _ImpersonationContext = _Identity.Impersonate();
00261 return true;
00262 }
00263
00264 MessageBox.Show(this,
00265 Resources.LoginFailed,
00266 Resources.AppTitle,
00267 MessageBoxButtons.OK,
00268 MessageBoxIcon.Warning);
00269
00270 return false;
00271 }
00272
00273 private void Logoff()
00274 {
00275 SetStatusText("Logging off...");
00276 Cursor = Cursors.WaitCursor;
00277
00278 loginButton.Enabled = false;
00279 logoffButton.Enabled = false;
00280
00281 CleanUp();
00282
00283 loginInfToolStripStatusLabel.Text = "Not logged in.";
00284
00285 loginButton.Enabled = true;
00286 informationToolStripDropDownButton.Visible = false;
00287
00288 SetStatusText(string.Empty);
00289 Cursor = Cursors.Default;
00290 }
00291
00292
00293
00294
00295 private void ToggleLoginPanel()
00296 {
00297 if (loginPanel.Height == mainToolStrip.Height)
00298 {
00299 loginPanel.Height = mainToolStrip.Height + 155;
00300 toogleLoginViewToolStripButton.Image = imageList.Images["arrowUp"];
00301
00302 projectServerUrlTextBox.Focus();
00303 }
00304 else
00305 {
00306 loginPanel.Height = mainToolStrip.Height;
00307 toogleLoginViewToolStripButton.Image = imageList.Images["arrowDown"];
00308 }
00309 }
00310
00311
00312
00313
00314 private void ShowPluginGalleryBrowser()
00315 {
00316 if (_PluginGalleryBrowserForm == null || _PluginGalleryBrowserForm.IsDisposed)
00317 {
00318 _PluginGalleryBrowserForm = new PluginGalleryBrowserForm(_PluginHost);
00319 _PluginGalleryBrowserForm.MdiParent = this;
00320 _PluginGalleryBrowserForm.WindowState = FormWindowState.Normal;
00321 }
00322
00323 _PluginGalleryBrowserForm.Show();
00324 }
00325
00326
00327
00328
00329 private void SetStatusText(string status)
00330 {
00331 statusToolStripStatusLabel.Text = status;
00332 statusToolStripStatusLabel.Invalidate();
00333 Update();
00334 }
00335
00336
00337
00338
00339 private void CleanUp()
00340 {
00341 UndoImpersonation();
00342
00343 if (_ProjectServer != null)
00344 {
00345 _ProjectServer.Dispose();
00346 _ProjectServer = null;
00347 }
00348
00349 if (_PluginHost != null)
00350 {
00351 _PluginHost.Dispose();
00352 }
00353
00354 Tools.DisposeForm(_PluginGalleryBrowserForm);
00355 }
00356
00357
00358
00359
00360 private void UndoImpersonation()
00361 {
00362 if (_ImpersonationContext != null)
00363 {
00364 _ImpersonationContext.Undo();
00365 _ImpersonationContext.Dispose();
00366 }
00367
00368 if (_Identity != null)
00369 {
00370 _Identity.Dispose();
00371 }
00372
00373 if (_UserToken != IntPtr.Zero)
00374 {
00375 Win32Impersonation.CloseHandle(_UserToken);
00376 }
00377 }
00378
00379 #endregion
00380
00381 #region Event Handlers
00382
00383 private void mainForm_Load(object sender, EventArgs e)
00384 {
00385 toogleLoginViewToolStripButton.Image = imageList.Images["arrowDown"];
00386
00387 loginPanel.Height = mainToolStrip.Height;
00388
00389 passwordTextBox.UseSystemPasswordChar = true;
00390
00391 projectServerUrlTextBox.Text = Settings.Default.LastProjectServerUrl;
00392 userNameTextBox.Text = Settings.Default.LastUserName;
00393
00394 useDefaultCredentialsCheckBox.Checked = Settings.Default.UseDefaultCredentials;
00395
00396 switch (Settings.Default.LastAuthenticationType)
00397 {
00398 case AuthenticationType.NotAuthenticated:
00399 windowsAuthenticationRadioButton.Checked = true;
00400 break;
00401 case AuthenticationType.Windows:
00402 windowsAuthenticationRadioButton.Checked = true;
00403 break;
00404 case AuthenticationType.Forms:
00405 formsAuthenticationRadioButton.Checked = true;
00406 break;
00407 default:
00408 break;
00409 }
00410
00411 hideLoginPanelOnSuccessCheckbox.Checked = Settings.Default.HideLoginPanelOnSuccess;
00412
00413 UpdateLoginUserInterface();
00414
00415 ToggleLoginPanel();
00416 }
00417
00418 private void UpdateLoginUserInterface(object sender, EventArgs e)
00419 {
00420 UpdateLoginUserInterface();
00421 }
00422
00423 private void toogleLoginViewToolStripButton_Click(object sender, EventArgs e)
00424 {
00425 ToggleLoginPanel();
00426 }
00427
00428 private void loginButton_Click(object sender, EventArgs e)
00429 {
00430 Logon();
00431 }
00432
00433 private void logoffButton_Click(object sender, EventArgs e)
00434 {
00435 Logoff();
00436 }
00437
00438 private void Test_FormClosed(object sender, FormClosedEventArgs e)
00439 {
00440 CleanUp();
00441 }
00442
00443 private void Test_FormClosing(object sender, FormClosingEventArgs e)
00444 {
00445 logoffButton.Enabled = false;
00446 loginButton.Enabled = false;
00447 Visible = false;
00448 }
00449
00450 private void hideLoginPanelOnSuccessCheckbox_CheckedChanged(object sender, EventArgs e)
00451 {
00452 Settings.Default.HideLoginPanelOnSuccess = hideLoginPanelOnSuccessCheckbox.Checked;
00453 Settings.Default.Save();
00454 }
00455
00456 private void useDefaultCredentialsCheckBox_CheckedChanged(object sender, EventArgs e)
00457 {
00458 UpdateLoginUserInterface();
00459
00460 if (!useDefaultCredentialsCheckBox.Checked)
00461 {
00462 if (string.IsNullOrEmpty(userNameTextBox.Text))
00463 {
00464 userNameTextBox.Select();
00465 }
00466 else
00467 {
00468 passwordTextBox.Select();
00469 }
00470 }
00471 }
00472
00473 private void profilesButton_Click(object sender, EventArgs e)
00474 {
00475 ProjectProfiles profiles = new ProjectProfiles(true);
00476 ProjectProfiles.ProjectProfile lastProfile = profiles.FindByName(Settings.Default.LastProfileName);
00477
00478 using (CredentialsForm credentialsForm = new CredentialsForm(Path.Combine(Application.StartupPath, Settings.Default.ProfilesFileName),
00479 true,
00480 lastProfile))
00481 {
00482
00483 if (credentialsForm.ShowDialog(this) == DialogResult.Cancel)
00484 {
00485 return;
00486 }
00487
00488 projectServerUrlTextBox.Text = credentialsForm.ProfileUrl;
00489 useDefaultCredentialsCheckBox.Checked = credentialsForm.UseDefaultCredentials;
00490
00491 if (!useDefaultCredentialsCheckBox.Checked)
00492 {
00493 userNameTextBox.Text = credentialsForm.ProfileUserName;
00494 passwordTextBox.Text = credentialsForm.ProfilePassword;
00495 }
00496
00497 Settings.Default.LastProfileName = credentialsForm.ProfileName;
00498
00499 Logon();
00500 }
00501 }
00502
00503 private void credentials_KeyDown(object sender, KeyEventArgs e)
00504 {
00505 if (e.KeyCode == Keys.Enter)
00506 {
00507 Logon();
00508 }
00509 }
00510
00511 private void visitPluginGalleryToolStripMenuItem_Click(object sender, EventArgs e)
00512 {
00513 ShowPluginGalleryBrowser();
00514 }
00515
00516 void _PluginHost_OnStatusChanged(object sender, OnStatusChangedArgs args)
00517 {
00518 if (args.IsFatalError)
00519 {
00520 MessageBox.Show(this, args.Message, Resources.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
00521 Close();
00522 return;
00523 }
00524 SetStatusText(args.Message);
00525 }
00526
00527 #endregion
00528 }
00529 }