/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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
 */

#include "exampleapplication.h"
#include "examplewindow.h"
#include "exampleoptiongroup.h"
#include <iostream>

ExampleApplication::ExampleApplication()
: Gtk::Application("org.gtkmm.examples.application",
    Gio::ApplicationFlags(Gio::APPLICATION_HANDLES_OPEN | Gio::APPLICATION_HANDLES_COMMAND_LINE))
{
  Glib::set_application_name("Gtk::Application Example");
}

Glib::RefPtr<ExampleApplication> ExampleApplication::create()
{
  return Glib::RefPtr<ExampleApplication>( new ExampleApplication() );
}

void ExampleApplication::create_window(const Glib::RefPtr<Gio::File>& file)
{
  ExampleWindow* window = new ExampleWindow();

  //Make sure that the application runs for as long this window is still open:
  add_window(*window);

  //Delete the window when it is hidden.
  //That's enough for this simple example.
  window->signal_hide().connect(sigc::bind<Gtk::Window*>(sigc::mem_fun(*this,
    &ExampleApplication::on_window_hide), window));

  window->show();

  if(!file)
  {
    //This is probably an new empty file, as a result of an activation rather
    //than an open.
    return;
  }

  const bool loaded = window->load_file(file);
  if(!loaded)
    std::cerr << "This file could not be loaded: " << file->get_path() << std::endl;
}

void ExampleApplication::on_window_hide(Gtk::Window* window)
{
  delete window;
}

void ExampleApplication::on_activate()
{
  //std::cout << "debug1: " << G_STRFUNC << std::endl;
  // The application has been started, so let's show a window.
  // A real application might want to reuse this "empty" window in on_open(),
  // when asked to open a file, if no changes have been made yet.
  create_window();
}

void ExampleApplication::on_open(const Gio::Application::type_vec_files& files,
  const Glib::ustring& hint)
{
  // The application has been asked to open some files,
  // so let's open a new window for each one.
  //std::cout << "debug: files.size()=" << files.size() << std::endl;
  for(guint i = 0; i < files.size(); i++)
  {
    Glib::RefPtr<Gio::File> file = files[0];
    if(!file)
    {
      std::cerr << G_STRFUNC << ": file is null." << std::endl;
    }
    else
      create_window(file);
  }

  //Call the base class's implementation:
  Gtk::Application::on_open(files, hint);
}

int ExampleApplication::on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>& command_line)
{
  //Parse command-line arguments that were passed either to the main (first) instance
  //or to subsequent instances.
  //Note that this parsing is happening in the main (not remote) instance.
  int argc = 0;
  char** argv =	command_line->get_arguments(argc);

  Glib::OptionContext context;
  ExampleOptionGroup group;
  context.set_main_group(group);

  try
  {
    context.parse(argc, argv);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "Exception parsing command-line: " << ex.what() << std::endl;
    std::cerr << context.get_help() << std::endl;
    return EXIT_FAILURE;
  }

  // The GOption documentation says that options without names will be returned
  // to the application as "rest arguments", meaning they will be left in the argv.
  std::string filepath;
  if(argc > 1)
  {
    const char* pch = argv[1];
    if(pch)
      filepath = pch;
  }

  if(filepath.empty())
  {
    //Open a new "document" instead:
    activate();
    return EXIT_FAILURE;
  }

  std::cout << "debug: parsed values: " << std::endl <<
    "  foo = " << group.m_arg_foo << std::endl <<
    "  goo = " << group.m_arg_goo << std::endl <<
    "  filepath = " << filepath << std::endl;

  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(filepath);
  open(file);

  //The local instance will eventually exit with this status code:
  return EXIT_SUCCESS;
}
