/* 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_connectdialog.cc

#include "cardwords_gtk_connectdialog.hh"
#include "cardwords_gettext.hh"
#include "cardwords_readfile.hh"
#include <gtk--/label.h>
#include <gtk--/spinbutton.h>
#include <gtk--/button.h>

CardWords_Gtk_ConnectDialog::CardWords_Gtk_ConnectDialog(CardWords_Options *opt)
  : Gtk::Table (3,2, false),
    port_adjustment(DEFAULT_PORT, 0, 65535, 1, 20, 20),
    options(opt)
{
  hostname_label = new Gtk::Label(gettext("Remote host name"));
  attach(*hostname_label,0,1,0,1, 0);
  hostname_label->show();

  hostname_entry = new Gtk::Entry;
  attach(*hostname_entry,1,2,0,1);
  switch(options->get_type("remote_hostname")) {
  case STRING_TYPE:
    {
      string remote_hostname;
      options->get("remote_hostname", &remote_hostname);
      hostname_entry->set_text(remote_hostname);
    }
    break;
  case UNKNOWN_TYPE:
    hostname_entry->set_text("localhost");
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("remote_hostname", "string");
    // This exits the program.
  }
  hostname_entry->show();

  port_label = new Gtk::Label(gettext("Remote port number"));
  attach(*port_label,0,1,1,2, 0);
  port_label->show();

  port_entry = new Gtk::SpinButton(port_adjustment, 1, 0);
  attach(*port_entry,1,2,1,2);
  switch(options->get_type("remote_port_number")) {
  case UNSIGNED_TYPE:
    {
      unsigned remote_port_number;
      options->get("remote_port_number", &remote_port_number);
      port_entry->set_value(remote_port_number);
    }
    break;
  case UNKNOWN_TYPE:
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("remote_port_number", "unsigned");
    // This exits the program.
  }
  port_entry->show();

  help_button = new Gtk::Button(gettext("Help"));
  attach(*help_button, 0,2, 2,3, GTK_EXPAND | GTK_FILL, 0);
  help_button->show();

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

// Put the entries' contents into the options object.
void
CardWords_Gtk_ConnectDialog::store_options(void)
{
  options->set("remote_hostname", hostname_entry->get_text());
  options->set("remote_port_number",
               unsigned(port_entry->get_value_as_int()));
}

CardWords_Gtk_ConnectDialog::~CardWords_Gtk_ConnectDialog()
{
  hostname_label->hide();
  remove (*hostname_label);
  delete hostname_label;

  hostname_entry->hide();
  remove (*hostname_entry);
  delete hostname_entry;

  port_label->hide();
  remove (*port_label);
  delete port_label;

  port_entry->hide();
  remove (*port_entry);
  delete port_entry;

  help_button->hide();
  remove(*help_button);
  delete help_button;
}

void
CardWords_Gtk_ConnectDialog::emit_help_signal(void)
{
  CardWords_ReadFile help_file(gettext("/gtk_connectdialog.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_connectdialog.help_text")
         << endl;
    return;
  }

  help_signal(help_file.str());
}
