/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  by Diether Knof and Borg Enders
 *
 *   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 can find this license in the file 'gpl.txt'.
 *
 *   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
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/

#ifdef USE_UI_GTK

#include "constants.h"
#include <unistd.h>

#include "help.h"
#include "../gtk_gui.h"
#include "../../../misc/setting.h"
#include "../../../misc/language.h"

namespace GTK_DOKO {

  /**
   ** constructor
   **
   ** @param	index_location	the location of the index page
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  Help::Help(GTK_GUI* gui_p) :
    gui_p(gui_p),
  location_dir("")
  {
    DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_INIT,
		  "Help::Help(location_dir)");

    DEBUG_RETURNING(VOID,
		    INFO_GTK_HELP && INFO_GTK_INIT,
		    "Help::Help(location_dir)");
  } // Help::Help(string const& location_dir)

  /**
   ** destructor
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  Help::~Help()
  {
    DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_INIT,
		  "Help::~Help()");

    DEBUG_RETURNING(VOID,
		    INFO_GTK_HELP && INFO_GTK_INIT,
		    "Help::~Help()");
  } // Help::~Help()

  /**
   ** inits the help
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::init()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_INIT,
		    "Help::init()");

      this->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_INIT,
		      "Help::init()");
    } // void Help::init()

  /**
   ** 
   ** -> result
   **
   ** @param	-
   **
   ** @return	the gui of this help system
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  GTK_GUI&
    Help::gui()
    {
      DEBUG_CALLING(INFO_GTK_DIALOG && INFO_GTK_VALUE,
		    "Help::gui()");

      DEBUG_ASSERTION((gui_p != NULL),
		      "GTK_GUI& Help::gui():\n"
		      "  'gui_p' == NULL");

      DEBUG_RETURNING(*(this->gui_p),
		      INFO_GTK_DIALOG && INFO_GTK_VALUE,
		      "Help::gui()");
    } // GTK_GUI& Help::gui()

  /**
   ** 
   ** sets the locatino directory and load the toc-page
   **
   ** @param	location_dir	new location directory
   **
   ** @return	the new location directory
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  string const&
    Help::location_dir_set(string const& location_dir)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_VALUE,
		    "Help::location_dir_set(location_dir)");

      this->location_dir = location_dir;

      DEBUG_RETURNING(this->location_dir,
		      INFO_GTK_HELP && INFO_GTK_VALUE,
		      "Help::location_dir_set(location_dir)");
    } // string const& Help::location_dir_set(string const& location_dir)

  /**
   ** 
   ** -> result
   **
   ** @param	-
   **
   ** @return	the location of the index page
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  string
    Help::location_index() const
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_VALUE,
		    "Help::location_index()");

      DEBUG_RETURNING(this->location_dir + "/index.html",
		      INFO_GTK_HELP && INFO_GTK_VALUE,
		      "Help::location_index()");
    } // string Help::location_index() const

  /**
   ** 
   ** -> result
   **
   ** @param	-
   **
   ** @return	the location of the toc page
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  string
    Help::location_toc() const
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_VALUE,
		    "Help::location_toc()");

      DEBUG_RETURNING(this->location_dir + "/toc.html",
		      INFO_GTK_HELP && INFO_GTK_VALUE,
		      "Help::location_toc()");
    } // string Help::location_toc() const

  /**
   ** 
   ** -> result
   **
   ** @param	-
   **
   ** @return	the location of the credits page
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  string
    Help::location_credits() const
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_VALUE,
		    "Help::location_credits()");

      DEBUG_RETURNING(this->location_dir + "/credits.html",
		      INFO_GTK_HELP && INFO_GTK_VALUE,
		      "Help::location_credits()");
    } // string Help::location_credits() const

  /**
   ** 
   ** dummy function
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::show()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_SHOW,
		    "Help::show()");

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_SHOW,
		      "Help::show()");
    } // void Help::show()

  /**
   ** 
   ** dummy function
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::hide()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_SHOW,
		    "Help::hide()");

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_SHOW,
		      "Help::hide()");
    } // void Help::hide()

  /**
   ** 
   ** loads the page
   ** uses the browser from 'settings'
   **
   ** @param	location	page to load
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::load(string const& location)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_SHOW,
		    "Help::load(location)");

#ifdef OUTDATED
#if defined(LINUX) || defined(HPUX)
      ::system((::setting(Setting::BROWSER) + " file://" + location + " &").c_str());
#endif
#endif
#ifndef WINDOWS
      if (static_cast<string const&>(location)[0] == '.') {
	char* wd = new char[1000];
	if (::getcwd(wd, 1000) == NULL) {
	  cerr << "current working directory has more than 1000 char's!" << endl;
	}
	::system((::setting(Setting::BROWSER)
		  + " file://" + wd + "/" + location).c_str());
      } else {
	::system((::setting(Setting::BROWSER) + " file://" + location).c_str());
      } // if (relative path)
#endif


      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_SHOW,
		      "Help::load(location)");
    } // void Help::load(string const& location)


  /**
   ** 
   ** load the index
   **
   ** @param	help	the help of the widget
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::index_load_event(GtkWidget*, gpointer help)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::index_load_event()");

      static_cast<Help*>(help)->index_load();
      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::index_load_event()");
    } // static void Help::index_load_event(GtkWidget*, gpointer help)

  /**
   ** 
   ** load the index
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::index_load()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::index_load()");

      this->load(this->location_index());
      this->show();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::index_load()");
    } // static void Help::index_load()

  /**
   ** 
   ** load the toc
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::toc_load()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::toc_load()");

      this->load(this->location_toc());
      this->show();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::toc_load()");
    } // static void Help::toc_load()

  /**
   ** 
   ** load the credits
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::credits_load()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::credits_load()");

      this->load(this->location_credits());
      this->show();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::credits_load()");
    } // static void Help::credits_load()


  /**
   ** 
   ** dummy function
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::icons_update()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_UPDATE,
		    "Help::icons_update()");

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_UPDATE,
		      "Help::icons_update()");
    } // void Help::icons_update()

  /**
   ** 
   ** updates the location directory
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::language_update()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_UPDATE,
		    "Help::language_update()");

      { // change the location of the toc and the html page
	vector<string> directory;

	directory.push_back(::setting(Setting::PRIVATE_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back(::setting(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back("./" + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting_default(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));

	vector<string>::iterator d;
	for (d = directory.begin(); d != directory.end(); d++) {
	  ifstream istr((*d + "/toc.short.html").c_str());
	  if (istr.good()) {
	    this->location_dir_set(*d);
	    break;
	  }
	}
      } // change the location of the toc and the html page


      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_UPDATE,
		      "Help::language_update()");
    } // void Help::language_update()

} // namespace GTK_DOKO

#endif // #ifdef USE_UI_GTK
