Application


Object Hierarchy:

GLib.Application GLib.Application GLib.Application GLib.Object GLib.Object GLib.Object->GLib.Application GLib.ActionGroup GLib.ActionGroup GLib.ActionGroup->GLib.Application GLib.ActionMap GLib.ActionMap GLib.ActionMap->GLib.Application

Description:

[ CCode ( type_id = "g_application_get_type ()" ) ]
[ Version ( since = "2.28" ) ]
public class Application : Object, ActionGroup, ActionMap

Example: Opening files with a GLib.Application::

public class MyApplication : Application {
private MyApplication () {
Object (application_id: "org.example.application", flags: ApplicationFlags.HANDLES_OPEN);
set_inactivity_timeout (10000);
}

public override void activate () {
// NOTE: when doing a longer-lasting action here that returns
// to the mainloop, you should use g_application_hold() and
// g_application_release() to keep the application alive until
// the action is completed.
print ("activated\n");
}

public override void open (File[] files, string hint) {
// NOTE: when doing a longer-lasting action here that returns
// to the mainloop, you should use g_application_hold() and
// g_application_release() to keep the application alive until
// the action is completed.

foreach (File file in files) {
string uri = file.get_uri ();
print (@"$uri\n");
}
}

public static int main (string[] args) {
MyApplication app = new MyApplication ();
int status = app.run (args);
return status;
}
}

valac --pkg gio-2.0 GLib.Application.0.vala

Example: Application with actions::

public class MyApplication : Application {
private MyApplication () {
Object (application_id: "org.example.application", flags: 0);
set_inactivity_timeout (10000);
add_actions ();
}

private void add_actions () {
SimpleAction simple_action = new SimpleAction ("simple-action", null);
simple_action.activate.connect (() => {
this.hold ();
print ("Simple action %s activated\n", simple_action.get_name ());
this.release ();
});
this.add_action (simple_action);

SimpleAction stateful_action = new SimpleAction.stateful ("toggle-action", null, new Variant.boolean (false));
stateful_action.activate.connect (() => {
print ("Action %s activated\n", stateful_action.get_name ());

this.hold ();
Variant state = stateful_action.get_state ();
bool b = state.get_boolean ();
stateful_action.set_state (new Variant.boolean (!b));
print (@"State change $b -> $(!b)\n");
this.release ();
});
this.add_action (stateful_action);
}

public override void activate () {
this.hold ();
print ("Activated\n");
this.release ();
}

private void describe_and_activate_action (string name) {
VariantType param_type = this.get_action_parameter_type (name);
Variant state = this.get_action_state (name);
bool enabled = this.get_action_enabled (name);

print (@"Action name: $name\n");
string? type = (param_type != null) ? param_type.dup_string () : "<none>";
print (@"Parameter type: $type\n");

print ("State type: %s\n", (state != null) ? state.get_type_string () : "<none>");
string state_val = (state != null) ? state.print (false) : "<none>";
print (@"State: $state_val\n");
print (@"Enabled: $enabled\n");

this.activate_action (name, null);
}

public static int main (string[] args) {
try {
MyApplication app = new MyApplication ();

if (args.length > 1) {
if (args[1] == "--simple-action") {
app.register (null);
app.describe_and_activate_action ("simple-action");
Process.exit (0);
} else if (args[1] == "--toggle-action") {
app.register (null);
app.describe_and_activate_action ("toggle-action");
Process.exit (0);
}
}

int status = app.run (args);
return status;
} catch (Error e) {
print ("Error: %s\n", e.message);
return 0;
}
}
}

valac --pkg gio-2.0 GLib.Application.1.vala

Example: Application with actions::

public class MyApplication : Application {
private MyApplication () {
Object (application_id: "org.example.application", flags: 0);
set_inactivity_timeout (10000);
add_actions ();
}

private void add_actions () {
SimpleAction simple_action = new SimpleAction ("simple-action", null);
simple_action.activate.connect (() => {
this.hold ();
print ("Simple action %s activated\n", simple_action.get_name ());
this.release ();
});
this.add_action (simple_action);

SimpleAction stateful_action = new SimpleAction.stateful ("toggle-action", null, new Variant.boolean (false));
stateful_action.activate.connect (() => {
print ("Action %s activated\n", stateful_action.get_name ());

this.hold ();
Variant state = stateful_action.get_state ();
bool b = state.get_boolean ();
stateful_action.set_state (new Variant.boolean (!b));
print (@"State change $b -> $(!b)\n");
this.release ();
});
this.add_action (stateful_action);
}

public override void activate () {
this.hold ();
print ("Activated\n");
this.release ();
}

private void describe_and_activate_action (string name) {
VariantType param_type = this.get_action_parameter_type (name);
Variant state = this.get_action_state (name);
bool enabled = this.get_action_enabled (name);

print (@"Action name: $name\n");
string? type = (param_type != null) ? param_type.dup_string () : "<none>";
print (@"Parameter type: $type\n");

print ("State type: %s\n", (state != null) ? state.get_type_string () : "<none>");
string state_val = (state != null) ? state.print (false) : "<none>";
print (@"State: $state_val\n");
print (@"Enabled: $enabled\n");

this.activate_action (name, null);
}

public static int main (string[] args) {
try {
MyApplication app = new MyApplication ();

if (args.length > 1) {
if (args[1] == "--simple-action") {
app.register (null);
app.describe_and_activate_action ("simple-action");
Process.exit (0);
} else if (args[1] == "--toggle-action") {
app.register (null);
app.describe_and_activate_action ("toggle-action");
Process.exit (0);
}
}

int status = app.run (args);
return status;
} catch (Error e) {
print ("Error: %s\n", e.message);
return 0;
}
}
}

valac --pkg gio-2.0 GLib.Application.1.vala

Example: Using extra D-Bus hooks with a GLib.Application::

public class MyApplication : Application {
private MyApplication () {
Object (application_id: "org.example.application", flags: 0);
set_inactivity_timeout (10000);
}

public override void activate () {
// NOTE: when doing a longer-lasting action here that returns
// to the mainloop, you should use g_application_hold() and
// g_application_release() to keep the application alive until
// the action is completed.
print ("Activated\n");
}

public override bool dbus_register (DBusConnection connection, string object_path) throws Error {
// We must chain up to the parent class:
base.dbus_register (connection, object_path);

// Now we can do our own stuff here. For example, we could export some D-Bus objects
return true;
}

public override void dbus_unregister (DBusConnection connection, string object_path) {
// Do our own stuff here, e.g. unexport any D-Bus objects we exported in the dbus_register
// hook above. Be sure to check that we actually did export them, since the hook
// above might have returned early due to the parent class' hook returning false!

base.dbus_unregister (connection, object_path);
}

public static int main (string[] args) {
MyApplication app = new MyApplication ();
int status = app.run (args);
return status;
}
}

valac --pkg gio-2.0 GLib.Application.3.vala

Example: Commandline-handling and GLib.Applications:

public class MyApplication : Application {
private int counter = 0;

private MyApplication () {
Object (application_id: "org.example.application", flags: ApplicationFlags.HANDLES_COMMAND_LINE);
set_inactivity_timeout (10000);
}

public override void activate () {
this.hold ();
print ("Activated\n");
this.release ();
}


private int _command_line (ApplicationCommandLine command_line) {
bool version = false;
bool count = false;

OptionEntry[] options = new OptionEntry[2];
options[0] = { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null };
options[1] = { "count", 0, 0, OptionArg.NONE, ref count, "Display version number", null };


// We have to make an extra copy of the array, since .parse assumes
// that it can remove strings from the array without freeing them.
string[] args = command_line.get_arguments ();
string*[] _args = new string[args.length];
for (int i = 0; i < args.length; i++) {
_args[i] = args[i];
}

try {
var opt_context = new OptionContext ("- OptionContext example");
opt_context.set_help_enabled (true);
opt_context.add_main_entries (options, null);
unowned string[] tmp = _args;
opt_context.parse (ref tmp);
} catch (OptionError e) {
command_line.print ("error: %s\n", e.message);
command_line.print ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
return 0;
}

if (version) {
command_line.print ("Test 0.1\n");
return 0;
}

if (count) {
command_line.print ("%d\n", ++this.counter);
return 0;
}


return 0;
}

public override int command_line (ApplicationCommandLine command_line) {
// keep the application running until we are done with this commandline
this.hold ();
int res = _command_line (command_line);
this.release ();
return res;
}

public static int main (string[] args) {
MyApplication app = new MyApplication ();
int status = app.run (args);
return status;
}
}

valac --pkg gio-2.0 GLib.Application.4.vala


Namespace: GLib
Package: gio-2.0

Content:

Properties:

Static methods:

Creation methods:

Methods:

Signals:

Inherited Members:

All known members inherited from interface GLib.ActionMap