/* Copyright (C) 2010 Jonathon Jongsma <jonathon@quotidian.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <giomm/file.h>
#include <giomm/inputstream.h>
#include <glibmm/object.h>
#include <glibmm/variant.h>
#include <glibmm/variantdict.h>

_DEFS(giomm,gio)
_PINCLUDE(glibmm/private/object_p.h)

namespace Gio
{

/** ApplicationCommandLine - A command-line invocation of an application.
 * ApplicationCommandLine represents a command-line invocation of an
 * application. It is created by Application and emitted in the "command-line"
 * signal and virtual function.
 *
 * The class contains the list of arguments that the program was invoked with.
 * It is also possible to query if the commandline invocation was local (ie:
 * the current process is running in direct response to the invocation) or
 * remote (ie: some other process forwarded the commandline to this process).
 *
 * The ApplicationCommandLine object can provide the argc and argv parameters
 * for use with the Glib::OptionContext command-line parsing API, with the
 * get_arguments() method.
 *
 * The exit status of the originally-invoked process may be set and messages
 * can be printed to stdout or stderr of that process. The lifecycle of the
 * originally-invoked process is tied to the lifecycle of this object (ie: the
 * process exits when the last reference is dropped).
 *
 * The main use for ApplicationCommandline (and the "command-line" signal) is
 * 'Emacs server' like use cases: You can set the EDITOR environment variable
 * to have e.g. git use your favourite editor to edit commit messages, and if
 * you already have an instance of the editor running, the editing will happen
 * in the running instance, instead of opening a new one. An important aspect
 * of this use case is that the process that gets started by git does not
 * return until the editing is done.
 * @newin{2,32}
 */
class GIOMM_API ApplicationCommandLine : public Glib::Object
{
  _CLASS_GOBJECT(ApplicationCommandLine, GApplicationCommandLine, G_APPLICATION_COMMAND_LINE, Glib::Object, GObject, , , GIOMM_API)

protected:
  _CTOR_DEFAULT

public:

  /** Gets the list of arguments that was passed on the command line.
   *
   * The strings in the array may contain non-UTF-8 data on UNIX (such as
   * filenames or arguments given in the system locale) but are always in
   * UTF-8 on Windows.
   *
   * If you wish to use the return value with Glib::OptionContext, you must
   * use Glib::OptionContext::parse(char**& argv).
   *
   * The return value is nullptr-terminated and should be freed using
   * g_strfreev().
   *
   * @newin{2,28}
   *
   * @param[out] argc The length of the arguments array.
   * @return The string array containing the arguments (the argv).
   */
  _WRAP_METHOD(char** get_arguments(int& argc) const, g_application_command_line_get_arguments)

  //TODO: Wrap the GVariantDict*. See also Application's handle-local-options signal.
  _WRAP_METHOD(Glib::RefPtr<Glib::VariantDict> get_options_dict(), g_application_command_line_get_options_dict, refreturn)
  _WRAP_METHOD(Glib::RefPtr<const Glib::VariantDict> get_options_dict() const, g_application_command_line_get_options_dict, refreturn, constversion)

  _WRAP_METHOD(Glib::RefPtr<InputStream> get_stdin(), g_application_command_line_get_stdin, refreturn)
  _WRAP_METHOD(Glib::RefPtr<const InputStream> get_stdin() const, g_application_command_line_get_stdin, refreturn, constversion)


  //We use std::string instead of ustring because the C documentation says that it may be non-UTF-8 data:
  _WRAP_METHOD(std::string get_cwd() const, g_application_command_line_get_cwd)

  //We use std::string instead of ustring because the C documentation says that it may be non-UTF-8 data:
#m4 _CONVERSION(`const gchar* const*',`std::vector<std::string>',`Glib::ArrayHandler<std::string>::array_to_vector($3, Glib::OWNERSHIP_NONE)')
  _WRAP_METHOD(std::vector<std::string> get_environ() const, g_application_command_line_get_environ)

  //We use std::string instead of ustring because the C documentation says that it may be non-UTF-8 data:
  _WRAP_METHOD(std::string getenv(const Glib::ustring& name) const, g_application_command_line_getenv)

  _WRAP_METHOD(bool is_remote() const, g_application_command_line_get_is_remote)

#m4 _CONVERSION(`GVariant*',`Glib::Variant< std::map<Glib::ustring, Glib::VariantBase> >',`$2($3)')
  _WRAP_METHOD(Glib::Variant< std::map<Glib::ustring, Glib::VariantBase> > get_platform_data() const, g_application_command_line_get_platform_data)

  _WRAP_METHOD(void set_exit_status(int exit_status), g_application_command_line_set_exit_status)
  _WRAP_METHOD(int get_exit_status() const, g_application_command_line_get_exit_status)

  /** Formats a message and prints it using the stdout print handler in the invoking process.
   * If this is a local invocation then this is exactly equivalent to g_print().
   *  If this is remote then this is equivalent to calling g_print() in the invoking process.
   *
   * @param message The text to print.
   */
  void print(const Glib::ustring& message);
  _IGNORE(g_application_command_line_print)

  /** Formats a message and prints it using the stderr print handler in the invoking process.
   * If this is a local invocation then this is exactly equivalent to g_printerr().
   *  If this is remote then this is equivalent to calling g_printerr() in the invoking process.
   *
   * @param message The text to print.
   */
  void printerr(const Glib::ustring& message);
  _IGNORE(g_application_command_line_printerr)

  _WRAP_METHOD(Glib::RefPtr<File> create_file_for_arg(const Glib::ustring& arg) const, g_application_command_line_create_file_for_arg)
};


} // namespace Gio
