/***********************************************************************
 *  File: usernameDialog.cs
 *  Author: Juan Carlos Luciani (jluciani@novell.com)
 * 
 *  Namespace: Novell.Security.ClientPasswordManager
 * 
 *  Classes implemented: UsernameDialog.
 * 
 *  Copyright (C) 2004 Novell, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ***********************************************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Novell.Security.ClientPasswordManager
{
   /// <summary>
   /// Provides a dialog for the acquisition of user names.
   /// </summary>
   sealed public class usernameDialog : System.Windows.Forms.Form
   {
      private System.Windows.Forms.TextBox usernameField;
      private System.Windows.Forms.Button okBtn;
      private System.Windows.Forms.Button cancelBtn;
      private System.Windows.Forms.Label label1;

      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      /// <summary>
      /// Constructor.
      /// </summary>
      /// <param name="title">Title to use with dialog.</param>
      public usernameDialog(string title)
      {
         // Required for Windows Form Designer support
         InitializeComponent();

         // Update the form title
         Text = title;
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose(bool disposing)
      {
         if (disposing)
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose(disposing);
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         this.usernameField = new System.Windows.Forms.TextBox();
         this.okBtn = new System.Windows.Forms.Button();
         this.cancelBtn = new System.Windows.Forms.Button();
         this.label1 = new System.Windows.Forms.Label();
         this.SuspendLayout();
         // 
         // usernameField
         // 
         this.usernameField.AcceptsReturn = true;
         this.usernameField.Location = new System.Drawing.Point(96, 24);
         this.usernameField.MaxLength = 64;
         this.usernameField.Name = "usernameField";
         this.usernameField.Size = new System.Drawing.Size(224, 20);
         this.usernameField.TabIndex = 5;
         this.usernameField.Text = "";
         // 
         // okBtn
         // 
         this.okBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
         this.okBtn.Location = new System.Drawing.Point(152, 64);
         this.okBtn.Name = "okBtn";
         this.okBtn.TabIndex = 7;
         this.okBtn.Text = "Ok";
         this.okBtn.Click += new System.EventHandler(this.okBtn_Click);
         // 
         // cancelBtn
         // 
         this.cancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel;
         this.cancelBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
         this.cancelBtn.Location = new System.Drawing.Point(240, 64);
         this.cancelBtn.Name = "cancelBtn";
         this.cancelBtn.TabIndex = 8;
         this.cancelBtn.Text = "Cancel";
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(8, 24);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(72, 16);
         this.label1.TabIndex = 9;
         this.label1.Text = "User name:";
         // 
         // usernameDialog
         // 
         this.AcceptButton = this.okBtn;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.CancelButton = this.cancelBtn;
         this.ClientSize = new System.Drawing.Size(338, 96);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.cancelBtn);
         this.Controls.Add(this.okBtn);
         this.Controls.Add(this.usernameField);
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "usernameDialog";
         this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Load += new System.EventHandler(this.usernameDialog_Load);
         this.Activated += new System.EventHandler(this.usernameDialog_Activated);
         this.ResumeLayout(false);

      }
      #endregion

      /// <summary>
      /// Handler for form activation event.
      /// </summary>
      private void usernameDialog_Activated(object sender, System.EventArgs e)
      {
         usernameField.Focus();
      }

      /// <summary>
      /// Handler for Ok button click event.
      /// </summary>
      private void okBtn_Click(object sender, System.EventArgs e)
      {
         // Dispose of the dialog and set DialogResult to OK
         DialogResult = DialogResult.OK;
         Close();
      }

      private void usernameDialog_Load(object sender, System.EventArgs e)
      {
      
      }

      /// <summary>
      /// Returns the username entered by the user.
      /// </summary>
      public System.String userName { get {return usernameField.Text;}}
   }

   /// <summary>
   /// Class wrapper around the usernameDialog class. This wrapper is utilized
   /// to hide the use of Gtk# from its users.
   /// </summary>
   public class UsernameDialog
   {
      #region Class Members and Defines
      
      // The following strings need to be localized
      private static string m_titleStartText = "Login to ";
      private static string m_titleRealmText = ", Realm: ";
      private static string m_inputRequiredLabelText = "User name required";

      // Title that will be used with the dialog
      private string m_dialogTitle;

      // Input gathered from the user
      private string m_userName;

      #endregion

      /// <summary>
      /// Constructor.
      /// </summary>
      /// <param name="svcName">Name of service on whose behalf we are acquiring user name.</param>
      public UsernameDialog(string svcName)
      {
         // Setup the title that will be used with the dialog
         m_dialogTitle = m_titleStartText + svcName;
      }

      /// <summary>
      /// Constructor.
      /// </summary>
      /// <param name="svcName">Name of service on whose behalf we are acquiring user name.</param>
      /// <param name="realm">Name of realm to which the service belongs, can be empty string or null.</param>
      public UsernameDialog(string svcName, string realm)
      {
         // Setup the appropriate title that will be used with the dialog
         if (realm != null && realm.Length != 0)
         {
            m_dialogTitle = m_titleStartText + svcName + m_titleRealmText + realm;
         }
         else
         {
            m_dialogTitle = m_titleStartText + svcName;
         }
      }

      /// <summary>
      /// Shows the dialog and gathers the user input.
      /// </summary>
      /// <param name="o">Window owner.</param>
      /// <returns>True if the user input was gathered, else false.</returns>
      public bool Invoke(System.Object o)
      {
         bool retVal;

         while (true)
         {
            // Instantiate and show usernameDialog
            usernameDialog dialog = new usernameDialog(m_dialogTitle);
            if (dialog.ShowDialog((IWin32Window) o) == DialogResult.OK)
            {
               // Save the user name entered
               m_userName = dialog.userName;

               // Verify that the user entered the desired information
               if (m_userName.Length != 0)
               {
                  // The user name was entered, indicate success and
                  // get ready to exit.
                  retVal = true;

                  // Dispose of the dialog and exit the loop
                  dialog.Dispose();
                  break;
               }
               else
               {
                  // Inform the user that it must enter a name
                  MessageBox.Show((IWin32Window) o, 
                                  m_inputRequiredLabelText,
                                  "", 
                                  MessageBoxButtons.OK,
                                  MessageBoxIcon.Error);

                  // Dispose of the dialog and continue to prompt the user
                  dialog.Dispose();
                  continue;
               }
            }
            else
            {
               // The user either hit the cancel button or closed
               // the window.
               retVal = false;

               // Dispose of the dialog and exit the loop
               dialog.Dispose();
               break;
            }
         }

         return retVal;
      }

      /// <summary>
      /// Gets the name entered by the user.
      /// </summary>
      public System.String userName { get {return m_userName;}}
   }
}