/***********************************************************************
 *  File: passwordDialog-gtk.cs
 *  Author: Juan Carlos Luciani (jluciani@novell.com)
 * 
 *  Namespace: Novell.Security.ClientPasswordManager
 * 
 *  Classes implemented: PasswordDialog.
 * 
 *  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.Text.RegularExpressions;
using Gtk;

namespace Novell.Security.ClientPasswordManager
{
   /// <summary>
   /// Class implements user name dialog leveraging Gtk#.
   /// </summary>
   sealed public class passwordDialog : Dialog
   {
      #region Class Members and Defines

      private Entry m_passwordEntry;
      private Button m_okButton;
      private Button m_cancelButton;

      // The following strings need to be localized
      private static string m_passwordLabelText = "Password:";

      #endregion

      /// <summary>
      /// Constructor.
      /// </summary>
      /// <param name="title">Title to use for the dialog.</param>
      public passwordDialog(string title) : base()
      {
         this.Title = title;
         this.HasSeparator = false;
         this.Resizable = false;
         this.Modal = true;

         HBox h = new HBox();
         h.BorderWidth = 12;
         h.Spacing = 6;

         Label l = new Label(m_passwordLabelText);
         l.Xalign = 0;
         h.PackStart(l, false, false, 0);

         m_passwordEntry = new Entry();
         m_passwordEntry.ActivatesDefault = true;
         m_passwordEntry.Visibility = false;
         h.PackStart(m_passwordEntry, true, true, 0);

         h.ShowAll();
         this.VBox.Add(h);

         m_okButton = new Button(Stock.Ok);
         m_okButton.CanDefault = true;
         this.AddActionWidget(m_okButton, ResponseType.Ok);

         m_cancelButton = new Button(Stock.Cancel);
         this.AddActionWidget(m_cancelButton, ResponseType.Cancel);

         this.DefaultResponse = ResponseType.Ok;
         this.ShowAll();
      }

      /// <summary>
      /// Shows the dialog and gathers the user input.
      /// </summary>
      /// <param name="parent">Window owner.</param>
      /// <returns>True if the user input was gathered, else false.</returns>
      public bool Invoke(Window parent)
      {
         // Set focus on the entry widget, display the dialog, and
         // hide it once it is answered.
         this.Focus = m_passwordEntry;
         this.TransientFor = parent;
         ResponseType resp = (ResponseType) this.Run();
         this.Hide();

         // Proceed based on the dialog response value
         if (resp == ResponseType.Ok)
         {
            // The user provided the necessary input
            return true;
         }
         else
         {
            // The user either hit the cancel button or closed
            // the window.
            return false;
         }
      }

      /// <summary>
      /// Gets the password entered by the user.
      /// </summary>
      /// <returns>NetworkCredential object or null if not successful</returns>
      public System.String password { get {return m_passwordEntry.Text;}}
   }

   /// <summary>
   /// Class wrapper around the passwordDialog class. This wrapper is utilized
   /// to hide the use of Gtk# from its users.
   /// </summary>
   public class PasswordDialog
   {
      #region Class Members and Defines
      
      passwordDialog m_dialog;

      // The following strings need to be localized
      private static string m_titleStartText = "Login to ";
      private static string m_titleRealmText = ", Realm: ";

      #endregion
      
      /// <summary>
      /// Constructor.
      /// </summary>
      /// <param name="svcName">Name of service on whose behalf we are acquiring password.</param>
      public PasswordDialog(string svcName)
      {
         // Instantiate the dialog with the appropriate title
         m_dialog = new passwordDialog(m_titleStartText + svcName);
      }

      /// <summary>
      /// Constructor.
      /// </summary>
      /// <param name="svcName">Name of service on whose behalf we are acquiring password.</param>
      /// <param name="realm">Name of realm to which the service belongs, can be empty string or null.</param>
      public PasswordDialog(string svcName, string realm)
      {
         // Instantiate the dialog with the appropriate title
         if (realm != null && realm.Length != 0)
         {
            m_dialog = new passwordDialog(m_titleStartText + svcName + m_titleRealmText + realm);
         }
         else
         {
            m_dialog = new passwordDialog(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)
      {
         return m_dialog.Invoke((Window) o);
      }

      /// <summary>
      /// Gets the password entered by the user.
      /// </summary>
      public System.String password { get {return m_dialog.password;}}
   }
}