#region Disclaimer / License
// Copyright (C) 2010, Jackie Ng
// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion Disclaimer / License
using OSGeo.MapGuide.MaestroAPI;
using OSGeo.MapGuide.MaestroAPI.Exceptions;
using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Windows.Forms;
namespace Maestro.Login
{
///
/// A dialog to prompt for information
///
public partial class LoginDialog : Form
{
private int _selectedIndex;
private readonly ILoginCtrl[] _controls;
private IServerConnection _conn;
private PreferredSiteList _siteList;
private HttpLoginCtrl _http;
private LocalNativeLoginCtrl _localNative;
private LocalLoginCtrl _local;
private readonly LocalNativeStubCtrl _localNativeStub;
private readonly LocalStubCtrl _localStub;
///
/// Initializes a new instance of the class.
///
public LoginDialog()
{
InitializeComponent();
_http = new HttpLoginCtrl() { Dock = DockStyle.Fill };
_localNative = new LocalNativeLoginCtrl() { Dock = DockStyle.Fill };
_local = new LocalLoginCtrl() { Dock = DockStyle.Fill };
_localNativeStub = new LocalNativeStubCtrl() { Dock = DockStyle.Fill };
_localStub = new LocalStubCtrl() { Dock = DockStyle.Fill };
_controls = new ILoginCtrl[]
{
_http,
_localNative,
_local,
_localNativeStub,
_localStub
};
_controls[0].EnableOk += OnEnableOk;
_controls[1].EnableOk += OnEnableOk;
_controls[2].EnableOk += OnEnableOk;
_controls[3].EnableOk += OnEnableOk;
_controls[4].EnableOk += OnEnableOk;
_controls[0].CheckSavedPassword += (sender, e) => { chkSavePassword.Checked = true; };
_controls[1].CheckSavedPassword += (sender, e) => { chkSavePassword.Checked = true; };
_controls[2].CheckSavedPassword += (sender, e) => { chkSavePassword.Checked = true; };
_controls[3].CheckSavedPassword += (sender, e) => { chkSavePassword.Checked = true; };
_controls[4].CheckSavedPassword += (sender, e) => { chkSavePassword.Checked = true; };
_controls[0].DisabledOk += OnDisableOk;
_controls[1].DisabledOk += OnDisableOk;
_controls[2].DisabledOk += OnDisableOk;
_controls[3].DisabledOk += OnDisableOk;
_controls[4].DisabledOk += OnDisableOk;
}
///
/// Gets the username.
///
/// The username.
public string Username
{
get { return _controls[_selectedIndex].Username; }
}
///
/// Gets the password.
///
/// The password.
public string Password
{
get { return _controls[_selectedIndex].Password; }
}
///
/// Raises the event.
///
/// An that contains the event data.
protected override void OnLoad(EventArgs e)
{
_siteList = PreferredSiteList.Load();
if (_siteList.Sites.Length == 0)
{
_http.Server = DefaultValues.Server;
_http.StartingPoint = DefaultValues.StartingPoint;
_http.Username = DefaultValues.Username;
_http.Password = DefaultValues.Password;
chkSavePassword.Checked = true;
//chkAutoConnect.Checked = false;
}
else
{
_http.AddSites(_siteList.Sites);
//In case the site was removed...
try { _http.SetPreferredSite(_siteList.PreferedSite); }
catch { }
//chkAutoConnect.Checked = _siteList.AutoConnect;
}
_http.SiteList = _siteList;
base.OnLoad(e);
rdHttp.Checked = true;
var isNativeApiAvailable = false;
var isLocalApiAvailable = false;
var providers = ConnectionProviderRegistry.GetProviders();
foreach (var prv in providers)
{
if (prv.Name.ToUpper().Equals("MAESTRO.LOCALNATIVE")) //NOXLATE
{
isNativeApiAvailable = true;
break;
}
else if (prv.Name.ToUpper().Equals("MAESTRO.LOCAL")) //NOXLATE
{
isLocalApiAvailable = true;
break;
}
}
//Mono = No LocalNativeConnection for you (for now...)
if (Platform.IsRunningOnMono || !isNativeApiAvailable)
{
rdTcpIp.Enabled = false;
}
if (Platform.IsRunningOnMono || !isLocalApiAvailable)
{
rdLocal.Enabled = false;
}
}
private void OnEnableOk(object sender, EventArgs e)
{
btnOK.Enabled = true;
}
private void OnDisableOk(object sender, EventArgs e)
{
btnOK.Enabled = false;
}
///
/// Gets the connection.
///
/// The connection.
public IServerConnection Connection
{
get { return _conn; }
}
///
/// Sets the login control.
///
/// The c.
public void SetLoginControl(Control c)
{
loginPanel.Controls.Clear();
loginPanel.Controls.Add(c);
}
private void rdHttp_CheckedChanged(object sender, EventArgs e)
{
UpdateLoginControl();
}
private void rdTcpIp_CheckedChanged(object sender, EventArgs e)
{
UpdateLoginControl();
}
private void rdLocal_CheckedChanged(object sender, EventArgs e)
{
UpdateLoginControl();
}
private void rdRest_CheckedChanged(object sender, EventArgs e)
{
UpdateLoginControl();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void btnOK_Click(object sender, EventArgs e)
{
using (new WaitCursor(this))
{
try
{
PreferedSite ps = null;
if (_selectedIndex == 0) //HTTP
{
var builder = new System.Data.Common.DbConnectionStringBuilder();
builder["Url"] = _http.Server; //NOXLATE
builder["Username"] = _http.Username; //NOXLATE
builder["Password"] = _http.Password; //NOXLATE
builder["Locale"] = _http.Language; //NOXLATE
builder["AllowUntestedVersion"] = true; //NOXLATE
string agent = "MapGuide Maestro v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); //NOXLATE
_conn = ConnectionProviderRegistry.CreateConnection("Maestro.Http", builder.ToString()); //NOXLATE
_conn.SetCustomProperty("UserAgent", agent); //NOXLATE
//Update preferred site entry if it exists
int index = 0;
foreach (PreferedSite s in _http.GetSites())
{
if (s.SiteURL == _http.Server)
{
ps = s;
break;
}
else
index++;
}
if (ps == null)
ps = new PreferedSite();
if (ps.ApprovedVersion == null)
ps.ApprovedVersion = new Version(0, 0, 0, 0);
if (_conn.SiteVersion > _conn.MaxTestedVersion && _conn.SiteVersion > ps.ApprovedVersion)
{
if (MessageBox.Show(this, Strings.UntestedServerVersion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) != DialogResult.Yes)
return;
}
try
{
ps.SiteURL = _http.Server;
ps.StartingPoint = _http.StartingPoint;
ps.Username = _http.Username;
ps.SavePassword = chkSavePassword.Checked;
ps.ApprovedVersion = ps.ApprovedVersion > _conn.SiteVersion ? ps.ApprovedVersion : _conn.SiteVersion;
if (ps.SavePassword)
ps.UnscrambledPassword = _http.Password;
else
ps.ScrambledPassword = string.Empty;
if (index >= _siteList.Sites.Length)
_siteList.AddSite(ps);
//_siteList.AutoConnect = chkAutoConnect.Checked;
_siteList.PreferedSite = index;
var ci = _http.SelectedCulture;
if (ci != null)
{
_siteList.GUILanguage = ci.Name;
}
_siteList.Save();
}
catch (Exception)
{
}
}
else if (_selectedIndex == 1) //Native
{
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
builder["ConfigFile"] = LocalNativeLoginCtrl.LastIniPath; //NOXLATE
builder["Username"] = _localNative.Username; //NOXLATE
builder["Password"] = _localNative.Password; //NOXLATE
builder["Locale"] = System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName; //NOXLATE
_conn = ConnectionProviderRegistry.CreateConnection("Maestro.LocalNative", builder.ToString()); //NOXLATE
}
else //Local
{
NameValueCollection param = new NameValueCollection();
param["ConfigFile"] = LocalLoginCtrl.LastIniPath; //NOXLATE
_conn = ConnectionProviderRegistry.CreateConnection("Maestro.Local", param); //NOXLATE
}
_conn.AutoRestartSession = true;
this.DialogResult = DialogResult.OK;
this.Close();
}
catch (TargetInvocationException ex)
{
//We don't care about the outer exception
string msg = ex.InnerException.Message;
MessageBox.Show(this, string.Format(Strings.ConnectionFailedError, msg), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
MessageBox.Show(this, string.Format(Strings.ConnectionFailedError, msg), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void UpdateLoginControl()
{
if (rdHttp.Checked)
{
_selectedIndex = 0;
SetLoginControl((Control)_controls[_selectedIndex]);
_controls[_selectedIndex].UpdateLoginStatus();
}
else if (rdTcpIp.Checked)
{
if (ConnectionProviderRegistry.GetInvocationCount("Maestro.LocalNative") == 0)
{
_selectedIndex = 1;
}
else
{
_selectedIndex = 3;
_localNativeStub.SetLastIniPath(LocalNativeLoginCtrl.LastIniPath);
}
SetLoginControl((Control)_controls[_selectedIndex]);
_controls[_selectedIndex].UpdateLoginStatus();
}
else
{
_selectedIndex = 2;
if (ConnectionProviderRegistry.GetInvocationCount("Maestro.Local") == 0)
{
_selectedIndex = 2;
}
else
{
_selectedIndex = 4;
_localStub.SetLastIniPath(LocalLoginCtrl.LastIniPath);
}
SetLoginControl((Control)_controls[_selectedIndex]);
_controls[_selectedIndex].UpdateLoginStatus();
}
}
}
}