glibmm 2.80.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Related Symbols | List of all members
Gio::ApplicationCommandLine Class Reference

ApplicationCommandLine - A command-line invocation of an application. More...

#include <giomm/applicationcommandline.h>

Inheritance diagram for Gio::ApplicationCommandLine:
Inheritance graph
[legend]

Public Member Functions

 ApplicationCommandLine (ApplicationCommandLine &&src) noexcept
 
ApplicationCommandLineoperator= (ApplicationCommandLine &&src) noexcept
 
 ~ApplicationCommandLine () noexcept override
 
GApplicationCommandLinegobj ()
 Provides access to the underlying C GObject.
 
const GApplicationCommandLinegobj () const
 Provides access to the underlying C GObject.
 
GApplicationCommandLinegobj_copy ()
 Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
 
char ** get_arguments (int &argc) const
 Gets the list of arguments that was passed on the command line.
 
Glib::RefPtr< Glib::VariantDictget_options_dict ()
 Gets the options that were passed to Glib::application_command_line().
 
Glib::RefPtr< const Glib::VariantDictget_options_dict () const
 Gets the options that were passed to Glib::application_command_line().
 
Glib::RefPtr< InputStreamget_stdin ()
 Gets the stdin of the invoking process.
 
Glib::RefPtr< const InputStreamget_stdin () const
 Gets the stdin of the invoking process.
 
std::string get_cwd () const
 Gets the working directory of the command line invocation.
 
std::vector< std::stringget_environ () const
 Gets the contents of the 'environ' variable of the command line invocation, as would be returned by Glib::get_environ(), ie as a nullptr-terminated list of strings in the form 'NAME=VALUE'.
 
std::string getenv (const Glib::ustring & name) const
 Gets the value of a particular environment variable of the command line invocation, as would be returned by Glib::getenv().
 
bool is_remote () const
 Determines if cmdline represents a remote invocation.
 
Glib::Variant< std::map< Glib::ustring, Glib::VariantBase > > get_platform_data () const
 Gets the platform data associated with the invocation of cmdline.
 
void set_exit_status (int exit_status)
 Sets the exit status that will be used when the invoking process exits.
 
int get_exit_status () const
 Gets the exit status of cmdline.
 
void print (const Glib::ustring & message)
 Prints a message using the stdout print handler in the invoking process.
 
void printerr (const Glib::ustring & message)
 Prints a message using the stderr print handler in the invoking process.
 
Glib::RefPtr< Filecreate_file_for_arg (const Glib::ustring &arg) const
 Creates a File corresponding to a filename that was given as part of the invocation of cmdline.
 
void done ()
 Signals that command line processing is completed.
 
- Public Member Functions inherited from Glib::Object
 Object (const Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&src) noexcept
 
Objectoperator= (Object &&src) noexcept
 
voidget_data (const QueryQuark & key)
 
void set_data (const Quark & key, void *data)
 
void set_data_with_c_callback (const Quark & key, void *data, GDestroyNotify notify)
 
void set_data (const Quark & key, void *data, DestroyNotify notify)
 Prefer set_data_with_c_callback() with a callback with C linkage.
 
void remove_data (const QueryQuark &quark)
 
voidsteal_data (const QueryQuark &quark)
 
- Public Member Functions inherited from Glib::ObjectBase
 ObjectBase (const ObjectBase &)=delete
 
ObjectBaseoperator= (const ObjectBase &)=delete
 
void set_property_value (const Glib::ustring & property_name, const Glib::ValueBase & value)
 You probably want to use a specific property_*() accessor method instead.
 
void get_property_value (const Glib::ustring & property_name, Glib::ValueBase & value) const
 You probably want to use a specific property_*() accessor method instead.
 
template<class PropertyType >
void set_property (const Glib::ustring & property_name, const PropertyType & value)
 You probably want to use a specific property_*() accessor method instead.
 
template<class PropertyType >
void get_property (const Glib::ustring & property_name, PropertyType & value) const
 You probably want to use a specific property_*() accessor method instead.
 
template<class PropertyType >
PropertyType get_property (const Glib::ustring & property_name) const
 You probably want to use a specific property_*() accessor method instead.
 
sigc::connection connect_property_changed (const Glib::ustring & property_name, const sigc::slot< void()> &slot)
 You can use the signal_changed() signal of the property proxy instead.
 
sigc::connection connect_property_changed (const Glib::ustring & property_name, sigc::slot< void()> &&slot)
 You can use the signal_changed() signal of the property proxy instead.
 
void freeze_notify ()
 Increases the freeze count on object.
 
void thaw_notify ()
 Reverts the effect of a previous call to freeze_notify().
 
virtual void reference () const
 Increment the reference count for this object.
 
virtual void unreference () const
 Decrement the reference count for this object.
 
GObject * gobj ()
 Provides access to the underlying C GObject.
 
const GObject * gobj () const
 Provides access to the underlying C GObject.
 
GObject * gobj_copy () const
 Give a ref-ed copy to someone. Use for direct struct access.
 

Static Public Member Functions

static GType get_type ()
 Get the GType for this class, for use with the underlying GObject type system.
 

Protected Member Functions

 ApplicationCommandLine ()
 
- Protected Member Functions inherited from Glib::Object
 Object ()
 
 Object (const Glib::ConstructParams &construct_params)
 
 Object (GObject *castitem)
 
 ~Object () noexcept override
 
- Protected Member Functions inherited from Glib::ObjectBase
 ObjectBase ()
 This default constructor is called implicitly from the constructor of user-derived classes, even if, for instance, Gtk::Button calls a different ObjectBase constructor.
 
 ObjectBase (const char *custom_type_name)
 A derived constructor always overrides this choice.
 
 ObjectBase (const std::type_info &custom_type_info)
 This constructor is a special feature to allow creation of derived types on the fly, without having to use g_object_new() manually.
 
 ObjectBase (ObjectBase &&src) noexcept
 
ObjectBaseoperator= (ObjectBase &&src) noexcept
 
virtual ~ObjectBase () noexcept=0
 
void initialize (GObject *castitem)
 
void initialize_move (GObject *castitem, Glib::ObjectBase *previous_wrapper)
 

Related Symbols

(Note that these are not member symbols.)

Glib::RefPtr< Gio::ApplicationCommandLinewrap (GApplicationCommandLine *object, bool take_copy=false)
 A Glib::wrap() method for this object.
 

Additional Inherited Members

- Public Types inherited from Glib::Object
using DestroyNotify = void(*)(gpointer data)
 

Detailed Description

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.

Since glibmm 2.32:

Constructor & Destructor Documentation

◆ ApplicationCommandLine() [1/2]

Gio::ApplicationCommandLine::ApplicationCommandLine ( ApplicationCommandLine &&  src)
noexcept

◆ ~ApplicationCommandLine()

Gio::ApplicationCommandLine::~ApplicationCommandLine ( )
overridenoexcept

◆ ApplicationCommandLine() [2/2]

Gio::ApplicationCommandLine::ApplicationCommandLine ( )
protected

Member Function Documentation

◆ create_file_for_arg()

Glib::RefPtr< File > Gio::ApplicationCommandLine::create_file_for_arg ( const Glib::ustring arg) const

Creates a File corresponding to a filename that was given as part of the invocation of cmdline.

This differs from g_file_new_for_commandline_arg() in that it resolves relative pathnames using the current working directory of the invoking process rather than the local process.

Since glibmm 2.36:
Parameters
argAn argument from cmdline.
Returns
A new File.

◆ done()

void Gio::ApplicationCommandLine::done ( )

Signals that command line processing is completed.

For remote invocation, it causes the invoking process to terminate.

For local invocation, it does nothing.

This method should be called in the Gio::Application::signal_command_line() handler, after the exit status is set and all messages are printed.

After this call, g_application_command_line_set_exit_status() has no effect. Subsequent calls to this method are no-ops.

This method is automatically called when the ApplicationCommandLine object is disposed — so you can omit the call in non-garbage collected languages.

Since glibmm 2.80:

◆ get_arguments()

char ** Gio::ApplicationCommandLine::get_arguments ( int argc) const

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().

Since glibmm 2.28:
Parameters
[out]argcThe length of the arguments array.
Returns
The string array containing the arguments (the argv).

◆ get_cwd()

std::string Gio::ApplicationCommandLine::get_cwd ( ) const

Gets the working directory of the command line invocation.

The string may contain non-utf8 data.

It is possible that the remote application did not send a working directory, so this may be nullptr.

The return value should not be modified or freed and is valid for as long as cmdline exists.

Since glibmm 2.28:
Returns
The current directory, or nullptr.

◆ get_environ()

std::vector< std::string > Gio::ApplicationCommandLine::get_environ ( ) const

Gets the contents of the 'environ' variable of the command line invocation, as would be returned by Glib::get_environ(), ie as a nullptr-terminated list of strings in the form 'NAME=VALUE'.

The strings may contain non-utf8 data.

The remote application usually does not send an environment. Use Gio::Application::Flags::SEND_ENVIRONMENT to affect that. Even with this flag set it is possible that the environment is still not available (due to invocation messages from other applications).

The return value should not be modified or freed and is valid for as long as cmdline exists.

See g_application_command_line_getenv() if you are only interested in the value of a single environment variable.

Since glibmm 2.28:
Returns
The environment strings, or nullptr if they were not sent.

◆ get_exit_status()

int Gio::ApplicationCommandLine::get_exit_status ( ) const

Gets the exit status of cmdline.

See g_application_command_line_set_exit_status() for more information.

Since glibmm 2.28:
Returns
The exit status.

◆ get_options_dict() [1/2]

Glib::RefPtr< Glib::VariantDict > Gio::ApplicationCommandLine::get_options_dict ( )

Gets the options that were passed to Glib::application_command_line().

If you did not override local_command_line() then these are the same options that were parsed according to the OptionEntrys added to the application with g_application_add_main_option_entries() and possibly modified from your GApplication::handle-local-options handler.

If no options were sent then an empty dictionary is returned so that you don't need to check for nullptr.

The data has been passed via an untrusted external process, so the types of all values must be checked before being used.

Since glibmm 2.40:
Returns
A VariantDict with the options.

◆ get_options_dict() [2/2]

Glib::RefPtr< const Glib::VariantDict > Gio::ApplicationCommandLine::get_options_dict ( ) const

Gets the options that were passed to Glib::application_command_line().

If you did not override local_command_line() then these are the same options that were parsed according to the OptionEntrys added to the application with g_application_add_main_option_entries() and possibly modified from your GApplication::handle-local-options handler.

If no options were sent then an empty dictionary is returned so that you don't need to check for nullptr.

The data has been passed via an untrusted external process, so the types of all values must be checked before being used.

Since glibmm 2.40:
Returns
A VariantDict with the options.

◆ get_platform_data()

Glib::Variant< std::map< Glib::ustring, Glib::VariantBase > > Gio::ApplicationCommandLine::get_platform_data ( ) const

Gets the platform data associated with the invocation of cmdline.

This is a Variant dictionary containing information about the context in which the invocation occurred. It typically contains information like the current working directory and the startup notification ID.

It comes from an untrusted external process and hence the types of all values must be validated before being used.

For local invocation, it will be nullptr.

Since glibmm 2.28:
Returns
The platform data, or nullptr.

◆ get_stdin() [1/2]

Glib::RefPtr< InputStream > Gio::ApplicationCommandLine::get_stdin ( )

Gets the stdin of the invoking process.

The InputStream can be used to read data passed to the standard input of the invoking process. This doesn't work on all platforms. Presently, it is only available on UNIX when using a D-Bus daemon capable of passing file descriptors. If stdin is not available then nullptr will be returned. In the future, support may be expanded to other platforms.

You must only call this function once per commandline invocation.

Since glibmm 2.34:
Returns
A InputStream for stdin.

◆ get_stdin() [2/2]

Glib::RefPtr< const InputStream > Gio::ApplicationCommandLine::get_stdin ( ) const

Gets the stdin of the invoking process.

The InputStream can be used to read data passed to the standard input of the invoking process. This doesn't work on all platforms. Presently, it is only available on UNIX when using a D-Bus daemon capable of passing file descriptors. If stdin is not available then nullptr will be returned. In the future, support may be expanded to other platforms.

You must only call this function once per commandline invocation.

Since glibmm 2.34:
Returns
A InputStream for stdin.

◆ get_type()

static GType Gio::ApplicationCommandLine::get_type ( )
static

Get the GType for this class, for use with the underlying GObject type system.

◆ getenv()

std::string Gio::ApplicationCommandLine::getenv ( const Glib::ustring name) const

Gets the value of a particular environment variable of the command line invocation, as would be returned by Glib::getenv().

The strings may contain non-utf8 data.

The remote application usually does not send an environment. Use Gio::Application::Flags::SEND_ENVIRONMENT to affect that. Even with this flag set it is possible that the environment is still not available (due to invocation messages from other applications).

The return value should not be modified or freed and is valid for as long as cmdline exists.

Since glibmm 2.28:
Parameters
nameThe environment variable to get.
Returns
The value of the variable, or nullptr if unset or unsent.

◆ gobj() [1/2]

GApplicationCommandLine * Gio::ApplicationCommandLine::gobj ( )
inline

Provides access to the underlying C GObject.

◆ gobj() [2/2]

const GApplicationCommandLine * Gio::ApplicationCommandLine::gobj ( ) const
inline

Provides access to the underlying C GObject.

◆ gobj_copy()

GApplicationCommandLine * Gio::ApplicationCommandLine::gobj_copy ( )

Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.

◆ is_remote()

bool Gio::ApplicationCommandLine::is_remote ( ) const

Determines if cmdline represents a remote invocation.

Since glibmm 2.28:
Returns
true if the invocation was remote.

◆ operator=()

ApplicationCommandLine & Gio::ApplicationCommandLine::operator= ( ApplicationCommandLine &&  src)
noexcept

◆ print()

void Gio::ApplicationCommandLine::print ( const Glib::ustring message)

Prints a message 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.

Parameters
messageThe text to print.

◆ printerr()

void Gio::ApplicationCommandLine::printerr ( const Glib::ustring message)

Prints a message 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.

Parameters
messageThe text to print.

◆ set_exit_status()

void Gio::ApplicationCommandLine::set_exit_status ( int  exit_status)

Sets the exit status that will be used when the invoking process exits.

The return value of the Application::signal_command_line() signal is passed to this function when the handler returns. This is the usual way of setting the exit status.

In the event that you want the remote invocation to continue running and want to decide on the exit status in the future, you can use this call. For the case of a remote invocation, the remote process will typically exit when the last reference is dropped on cmdline. The exit status of the remote process will be equal to the last value that was set with this function.

In the case that the commandline invocation is local, the situation is slightly more complicated. If the commandline invocation results in the mainloop running (ie: because the use-count of the application increased to a non-zero value) then the application is considered to have been 'successful' in a certain sense, and the exit status is always zero. If the application use count is zero, though, the exit status of the local ApplicationCommandLine is used.

This method is a no-op if g_application_command_line_done() has been called.

Since glibmm 2.28:
Parameters
exit_statusThe exit status.

Friends And Related Symbol Documentation

◆ wrap()

Glib::RefPtr< Gio::ApplicationCommandLine > wrap ( GApplicationCommandLine object,
bool  take_copy = false 
)
related

A Glib::wrap() method for this object.

Parameters
objectThe C instance.
take_copyFalse if the result should take ownership of the C instance. True if it should take a new copy or ref.
Returns
A C++ instance that wraps this C instance.