00001 using System;
00002 using System.IO;
00003 using System.Windows;
00004 using System.Windows.Controls;
00005 using CodePlex.MicrosoftProject.mpFx;
00006 using Microsoft.MCS.Projects.MopsBrowser;
00007
00008 namespace Mcs.Epm.MpFx.Browser.Controls.Login
00009 {
00010
00011
00012
00013 public partial class LoginControl
00014 {
00015 private ProjectProfiles _ProjectProfiles;
00016
00017 private ProjectProfiles.ProjectProfile _Profile;
00018
00019 public event EventHandler OnLoginButtonClick;
00020
00021 public event EventHandler OnCancelButtonClick;
00022
00023 public LoginControl()
00024 {
00025 InitializeComponent();
00026 }
00027
00028 public ProjectProfiles.ProjectProfile Profile
00029 {
00030 get
00031 {
00032 return _Profile;
00033 }
00034 }
00035
00036 public bool UseDefaultCredentials { get; private set; }
00037
00038 private void DoOnCancelButtonClick()
00039 {
00040 if (OnCancelButtonClick != null)
00041 {
00042 OnCancelButtonClick(this, null);
00043 }
00044 }
00045
00046 public void DoOnLoginButtonClick()
00047 {
00048 if (OnLoginButtonClick != null)
00049 {
00050 UseDefaultCredentials = !useDefaultCredentialsCheckBox.IsChecked.HasValue ||
00051 (bool) useDefaultCredentialsCheckBox.IsChecked;
00052
00053 _Profile = _ProjectProfiles.FindByName(profileList.SelectedValue.ToString());
00054
00055 _Profile.Url = urlTextBox.Text;
00056 _Profile.UserName = userNameTextBox.Text;
00057 _Profile.Password = passwordBox.Password;
00058
00059 _ProjectProfiles.Replace(_Profile.Name, _Profile);
00060 _ProjectProfiles.Save();
00061
00062 if (_Profile == null)
00063 {
00064 Global.ShowMessageBox(System.Windows.Window.GetWindow(Parent),
00065 "invalidProfile",
00066 MessageBoxButton.OK,
00067 MessageBoxImage.Error,
00068 MessageBoxOptions.None);
00069
00070 return;
00071 }
00072
00073 OnLoginButtonClick(this, null);
00074 }
00075 }
00076
00077 private void loginButton_Click(object sender, RoutedEventArgs e)
00078 {
00079 DoOnLoginButtonClick();
00080 }
00081
00082 private void cancelButton_Click(object sender, RoutedEventArgs e)
00083 {
00084 DoOnCancelButtonClick();
00085 }
00086
00087 private void UserControl_Initialized(object sender, EventArgs e)
00088 {
00089 LoadProfiles();
00090 SelectLastProfile();
00091 }
00092
00093 private void LoadProfiles()
00094 {
00095 profileList.Items.Clear();
00096
00097 _ProjectProfiles = new ProjectProfiles(true);
00098 _ProjectProfiles.LoadUserProfiles(
00099 Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
00100 Application.Current.FindResource("profilesFile").ToString()));
00101
00102 profileList.Items.Add("<Manage Profiles...>");
00103
00104 foreach (ProjectProfiles.ProjectProfile profile in _ProjectProfiles.Profiles)
00105 {
00106 profileList.Items.Add(profile.Name);
00107 }
00108
00109 }
00110
00111 private void SelectLastProfile()
00112 {
00113 int lastProfileIndex = profileList.Items.IndexOf(Settings.Default.LastProfileName);
00114
00115 if (lastProfileIndex != -1)
00116 {
00117 profileList.SelectedIndex = lastProfileIndex;
00118 }
00119 else
00120 {
00121 profileList.SelectedIndex = profileList.Items.Count > 1 ? 1 : 0;
00122 }
00123 }
00124
00125 private void profileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
00126 {
00127 if (profileList.SelectedValue == null)
00128 {
00129 return;
00130 }
00131
00132 SelectProfile(profileList.SelectedValue.ToString());
00133 }
00134
00135 private void SelectProfile(string currentProfileName)
00136 {
00137 ProjectProfiles.ProjectProfile currentProfile = _ProjectProfiles.FindByName(currentProfileName);
00138
00139 if (currentProfile != null)
00140 {
00141 userNameTextBox.Text = currentProfile.UserName;
00142 urlTextBox.Text = currentProfile.Url;
00143
00144 if (userNameTextBox.Text.Length > 0)
00145 {
00146 passwordBox.Focus();
00147 passwordBox.SelectAll();
00148 }
00149 else
00150 {
00151 userNameTextBox.Focus();
00152 }
00153
00154 _Profile = currentProfile;
00155 }
00156 }
00157
00158 private void manageProfilesButton_Click(object sender, RoutedEventArgs e)
00159 {
00160 ProfileManagerWindow profileManagerWindow = new ProfileManagerWindow(_ProjectProfiles, profileList.SelectedValue as string)
00161 {
00162 Owner = System.Windows.Window.GetWindow(Parent),
00163 ShowInTaskbar = false,
00164 WindowStartupLocation =
00165 WindowStartupLocation.CenterOwner
00166 };
00167
00168 profileManagerWindow.ShowDialog();
00169
00170 if (!profileManagerWindow.DialogResult.HasValue || !profileManagerWindow.DialogResult.Value)
00171 {
00172 return;
00173 }
00174
00175 LoadProfiles();
00176 SelectProfile(profileManagerWindow.ProfileName);
00177 }
00178 }
00179 }