/* This file is part of cardwords
   (c) 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   This program 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 program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_gtk_identitydialog.cc

#include "cardwords_gtk_identitydialog.hh"
#include "cardwords_gettext.hh"
#include <stdlib.h>
#include "cardwords_readfile.hh"
#include <gtk--/button.h>
#include <gtk--/label.h>
#include <gtk--/entry.h>

CardWords_Gtk_IdentityDialog::
CardWords_Gtk_IdentityDialog(CardWords_Options * opt)
  : Gtk::Table(3,2, false),
    options(opt)
{
  help_button = new Gtk::Button(gettext("Help"));

  name_label = new Gtk::Label(gettext("Your name:"));
  name_entry = new Gtk::Entry();
  switch(options->get_type("name")) {
  case UNKNOWN_TYPE:
    name_entry->set_text(getenv("LOGNAME"));
    break;
  case STRING_TYPE:
    {
      string name;
      options->get("name", &name);
      name_entry->set_text(name);
    }
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("name", "string");
    // This exits the program.
  }
   
  alt_name_label = new Gtk::Label(gettext("Alternative name:"));
  alt_name_entry = new Gtk::Entry();
  switch(options->get_type("name")) {
  case UNKNOWN_TYPE:
    break;
  case STRING_TYPE:
    {
      string alt_name;
      options->get("alt_name", &alt_name);
      alt_name_entry->set_text(alt_name);
    }
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("alt_name", "string");
    // This exits the program.
  }

  attach(*help_button, 0,2, 2,3, GTK_FILL | GTK_EXPAND, 0);

  attach(*name_label, 0,1, 0,1, 0);
  attach(*name_entry, 1,2, 0,1);
  attach(*alt_name_label, 0,1, 1,2, 0);
  attach(*alt_name_entry, 1,2, 1,2);

  help_button->show();
  name_label->show();
  name_entry->show();
  alt_name_label->show();
  alt_name_entry->show();

  help_button->clicked.
    connect(SigC::slot(this, &CardWords_Gtk_IdentityDialog::emit_help_signal));

  name_entry->changed.
    connect(SigC::slot(this, &CardWords_Gtk_IdentityDialog::store_options));

  alt_name_entry->changed.
    connect(SigC::slot(this, &CardWords_Gtk_IdentityDialog::store_options));
}

void CardWords_Gtk_IdentityDialog::store_options(void)
{
  options->set("name", name_entry->get_text());
  options->set("alt_name", alt_name_entry->get_text());
}

CardWords_Gtk_IdentityDialog::~CardWords_Gtk_IdentityDialog()
{
  help_button->hide();
  name_label->hide();
  name_entry->hide();
  alt_name_label->hide();
  alt_name_entry->hide();

  remove(*help_button);
  remove(*name_label);
  remove(*name_entry);
  remove(*alt_name_label);
  remove(*alt_name_entry);

  delete help_button;
  delete name_label;
  delete name_entry;
  delete alt_name_label;
  delete alt_name_entry;
}

void
CardWords_Gtk_IdentityDialog::emit_help_signal(void)
{
  CardWords_ReadFile help_file(gettext("/gtk_identitydialog.help_text"));
  // Note to *translators*: this is the name of a file containing a help text.
  // The help text is generated from the README.<locale>.sgml.m4 file.
  // The filename above is then to be translated to
  // "/gtk_clientoptionsdialog.<locale>.help_text".
  // But leave this untranslated as long as there is no translated version
  // of the README. If you want to translate the README, please contact me at
  // cardwords@crosswinds.net . Tobias.
  
  if (help_file == false) {
    // Help text file not found
    cerr << gettext("WARNING: Cannot open help text file ")
         << LIBDIR_STR
         << gettext("/gtk_identitydialog.help_text")
         << endl;
    return;
  }

  help_signal(help_file.str());
}
