# The application context

Making Advanced Gtk+ Sequencer objects reachable from different contices was mandatory as introducing AgsApplicationContext. Imagine you are within a GUI callback and want to lookup a soundcard or sequencer the application context shall provide this functionality and provide access to its objects through a well defined interface. As doing it with interfaces you are not limited to one specific implementation rather having the option to choose the appropriate one implementing the interfaces.

- AgsConcurrencyProvider

- AgsServiceProvider

- AgsSoundProvider

There are different contices available e.g. AgsThreadApplicationContext providing its functionality by AgsConcurrencyProvider, AgsAudioApplicationContext giving you the wished objects by implementing AgsConcurrencyProvider and AgsSoundProvider. For example the code below should each giving you the same meaning object but using different contices.

Since AgsApplicationContext is a singleton you create only 1 instance of your desired implementation. The application context is usually obtained by calling `AgsApplicationContext* ags_application_context_get_instance()`. Make sure to instantiate an application context before using this function.

## Implementations and their interfaces

The most basic application context implementing AgsConcurrencyProvider is AgsThreadApplicationContext. If you use your very own application context implementation make sure to set main loop, because AgsThread makes use of the appropriate getter `AgsThread* ags_concurrency_provider_get_main_loop(AgsConcurrencyProvider*)`. Further for your very own application context you should set the AgsTaskLauncher in order to take advantage of launching AgsTask implementations. The interface allows you to set a thread pool and some worker threads if needed.

This example instantiates AgsThreadApplicationContext. By instantiating the application context the global variable ags_application_context is initially set. Later you can obtain your application context by calling `AgsApplicationContext* ags_application_context_get_instance()`. Later the code connects to the AgsApplicationContext::setup() signal. The `void setup_callback(AgsApplicationContext*, gpointer)` function sets the global variable start_loader to TRUE. This causes the prior added timeout to do the actual work with AgsTaskLauncher. The task launcher is obtained by calling `AgsThread* ags_concurrency_provider_get_task_launcher(AgsConcurrencyProvider*)`.

### Thread application context

The AgsAudioApplicationContext inherites from AgsApplicationContext and implements the AgsConcurrencyProvider interface, too. So you can retrieve the task launcher the same way. But the context implements one more, the AgsSoundProvider interface. Giving you objects related to threading and audio processing.

This example does the same as the prior, but this time instantiates the AgsAudioApplicationContext. The `gboolean loader_timeout(AgsApplicationContext *application_context)`. does this time add the AgsStartSoundcard task to the task launcher.

### Audio application context

## The main loop property

AgsApplicationContext:main-loop does usually point to an AgsThread implementing AgsMainLoop interface. &libagsthreadso; provides you the AgsGenericMainLoop object or if you intend to use &libagsaudioso;, this property shall point to AgsAudioLoop.

However you should rather use `AgsThread* ags_concurrency_provider_get_main_loop(AgsConcurrencyProvider*)` to obtain the main loop instead.

## The config property

The AgsApplicationContext base class provides you an AgsConfig instance. It might load a default configuration or from current users home directory.

Since AgsConfig is a singleton you should obtain it by calling `AgsConfig* ags_config_get_instance()`.

AgsConfig stores its properties as key value pairs within appropriate group. In order to get the config instance, load default configuration and get the threading model do the following.

### Get config value

## The file property

You might want to set an AgsFile or AgsSimpleFile instance within your application context. This in view of having your application persisted.

### The application context :file property

## The application mutex

As version 2.0.x the application mutex was superseeded by the class mutices and a common field :obj-mutex used by various types. The AgsMutexManager is still around but with less importance.

## Program start and termination

The application context provides signals to make your application ready to run. You basically implement AgsApplicationContext::prepare, AgsApplicationContext::setup and AgsApplicationContext::register-types. It is upto you how the application shall behave.

Note since version 3.13.0 you call only AgsApplicationContext::prepare, which calls AgsApplicationContext::setup and then enters GLib's main loop. So the prepare signal won't return unless you terminate your application.

AgsApplicationContext::quit signal terminates your application. Feel free to provide your own implementation.

## Abstract data connection

AgsDataConnectionManager and AgsConnection are removed in 2.0.x. The object was somehow overhelming because you can have properties.

The AgsConnectable interface provides 2 new functions: `void ags_connectable_connect_connection(AgsConnectable*, GObject*)` and `void ags_connectable_disconnect_connection(AgsConnectable*, GObject*)`.

Dependencies not know an instantiation time can be later connected.

## Common interfaces

Use AgsConnectable if you intend to listen to a particular event. If you want to connect an event of an object known during instantiation time use ::connect and ::disconnect. Assumed the object needs to be resolved, you can ::connect-connection ::disconnect-connection, later.

AgsPlugin interface provides persistence to a well known abstract base type. Since it has various implementations, this interface provides `void ags_plugin_read(AgsFile*, xmlNode*, AgsPlugin*)` and `xmlNode* ags_plugin_write(AgsFile*, xmlNode*, AgsPlugin*)`

Likewise there are the interfaces intended to use with sound related objects AgsSoundcard, AgsSequencer, AgsMutable and AgsSeekable.