LCOV - code coverage report
Current view: top level - src/libvalent/core - valent-application-plugin.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 88.1 % 59 52
Test Date: 2024-05-05 22:34:11 Functions: 90.9 % 11 10
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 41.1 % 56 23

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       2                 :             : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
       3                 :             : 
       4                 :             : #define G_LOG_DOMAIN "valent-application-plugin"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <gio/gio.h>
       9                 :             : 
      10                 :             : #include "valent-application-plugin.h"
      11                 :             : #include "valent-debug.h"
      12                 :             : #include "valent-extension.h"
      13                 :             : 
      14                 :             : 
      15                 :             : /**
      16                 :             :  * ValentApplicationPlugin:
      17                 :             :  *
      18                 :             :  * An abstract base class for application plugins.
      19                 :             :  *
      20                 :             :  * `ValentApplicationPlugin` is a base class for plugins that operate in the
      21                 :             :  * scope of the application. This usually means integrating the application with
      22                 :             :  * the host environment (eg. XDG Autostart).
      23                 :             :  *
      24                 :             :  * ## Implementation Notes
      25                 :             :  *
      26                 :             :  * Implementations may handle application events by overriding the appropriate
      27                 :             :  * virtual function, including [vfunc@Valent.ApplicationPlugin.activate] to
      28                 :             :  * handle activation, [vfunc@Valent.ApplicationPlugin.command_line] to handle
      29                 :             :  * CLI options, or [vfunc@Valent.ApplicationPlugin.open] to handle files.
      30                 :             :  *
      31                 :             :  * For plugin preferences see [class@Valent.PreferencesPage].
      32                 :             :  *
      33                 :             :  * ## `.plugin` File
      34                 :             :  *
      35                 :             :  * Application plugins have no special fields in the `.plugin` file.
      36                 :             :  *
      37                 :             :  * Since: 1.0
      38                 :             :  */
      39                 :             : 
      40                 :             : typedef struct
      41                 :             : {
      42                 :             :   gpointer  reserved[1];
      43                 :             : } ValentApplicationPluginPrivate;
      44                 :             : 
      45   [ +  +  +  - ]:         681 : G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ValentApplicationPlugin, valent_application_plugin, VALENT_TYPE_EXTENSION)
      46                 :             : 
      47                 :             : /**
      48                 :             :  * ValentApplicationPluginClass:
      49                 :             :  * @activate: the virtual function pointer for valent_application_plugin_activate()
      50                 :             :  * @command_line: the virtual function pointer for valent_application_plugin_command_line()
      51                 :             :  * @dbus_register: the virtual function pointer for valent_application_plugin_dbus_register()
      52                 :             :  * @dbus_unregister: the virtual function pointer for valent_application_plugin_dbus_unregister()
      53                 :             :  * @open: the virtual function pointer for valent_application_plugin_open()
      54                 :             :  * @shutdown: the virtual function pointer for valent_application_plugin_shutdown()
      55                 :             :  * @startup: the virtual function pointer for valent_application_plugin_startup()
      56                 :             :  *
      57                 :             :  * The virtual function table for `ValentApplicationPlugin`.
      58                 :             :  */
      59                 :             : 
      60                 :             : 
      61                 :             : /* LCOV_EXCL_START */
      62                 :             : static gboolean
      63                 :             : valent_application_plugin_real_activate (ValentApplicationPlugin *plugin)
      64                 :             : {
      65                 :             :   g_assert (VALENT_IS_APPLICATION_PLUGIN (plugin));
      66                 :             : 
      67                 :             :   return FALSE;
      68                 :             : }
      69                 :             : 
      70                 :             : static int
      71                 :             : valent_application_plugin_real_command_line (ValentApplicationPlugin *plugin,
      72                 :             :                                              GApplicationCommandLine *command_line)
      73                 :             : {
      74                 :             :   g_assert (VALENT_IS_APPLICATION_PLUGIN (plugin));
      75                 :             :   g_assert (G_IS_APPLICATION_COMMAND_LINE (command_line));
      76                 :             : 
      77                 :             :   return 0;
      78                 :             : }
      79                 :             : 
      80                 :             : static gboolean
      81                 :             : valent_application_plugin_real_dbus_register (ValentApplicationPlugin  *plugin,
      82                 :             :                                               GDBusConnection          *connection,
      83                 :             :                                               const char               *object_path,
      84                 :             :                                               GError                  **error)
      85                 :             : {
      86                 :             :   g_assert (VALENT_IS_APPLICATION_PLUGIN (plugin));
      87                 :             :   g_assert (G_IS_DBUS_CONNECTION (connection));
      88                 :             :   g_assert (g_variant_is_object_path (object_path));
      89                 :             :   g_assert (error == NULL || *error == NULL);
      90                 :             : 
      91                 :             :   return TRUE;
      92                 :             : }
      93                 :             : 
      94                 :             : static void
      95                 :             : valent_application_plugin_real_dbus_unregister (ValentApplicationPlugin *plugin,
      96                 :             :                                                 GDBusConnection         *connection,
      97                 :             :                                                 const char              *object_path)
      98                 :             : {
      99                 :             :   g_assert (VALENT_IS_APPLICATION_PLUGIN (plugin));
     100                 :             :   g_assert (G_IS_DBUS_CONNECTION (connection));
     101                 :             :   g_assert (g_variant_is_object_path (object_path));
     102                 :             : }
     103                 :             : 
     104                 :             : static gboolean
     105                 :             : valent_application_plugin_real_open (ValentApplicationPlugin  *plugin,
     106                 :             :                                      GFile                   **files,
     107                 :             :                                      int                       n_files,
     108                 :             :                                      const char               *hint)
     109                 :             : {
     110                 :             :   g_assert (VALENT_IS_APPLICATION_PLUGIN (plugin));
     111                 :             :   g_assert (files != NULL);
     112                 :             :   g_assert (n_files > 0);
     113                 :             :   g_assert (hint != NULL);
     114                 :             : 
     115                 :             :   return FALSE;
     116                 :             : }
     117                 :             : 
     118                 :             : static void
     119                 :             : valent_application_plugin_real_shutdown (ValentApplicationPlugin *plugin)
     120                 :             : {
     121                 :             :   g_assert (VALENT_IS_APPLICATION_PLUGIN (plugin));
     122                 :             : }
     123                 :             : 
     124                 :             : static void
     125                 :             : valent_application_plugin_real_startup (ValentApplicationPlugin *plugin)
     126                 :             : {
     127                 :             :   g_assert (VALENT_IS_APPLICATION_PLUGIN (plugin));
     128                 :             : }
     129                 :             : /* LCOV_EXCL_STOP */
     130                 :             : 
     131                 :             : /*
     132                 :             :  * GObject
     133                 :             :  */
     134                 :             : static void
     135                 :          66 : valent_application_plugin_class_init (ValentApplicationPluginClass *klass)
     136                 :             : {
     137                 :          66 :   klass->activate = valent_application_plugin_real_activate;
     138                 :          66 :   klass->command_line = valent_application_plugin_real_command_line;
     139                 :          66 :   klass->dbus_register = valent_application_plugin_real_dbus_register;
     140                 :          66 :   klass->dbus_unregister = valent_application_plugin_real_dbus_unregister;
     141                 :          66 :   klass->open = valent_application_plugin_real_open;
     142                 :          66 :   klass->shutdown = valent_application_plugin_real_shutdown;
     143                 :          66 :   klass->startup = valent_application_plugin_real_startup;
     144                 :             : }
     145                 :             : 
     146                 :             : static void
     147                 :          20 : valent_application_plugin_init (ValentApplicationPlugin *adapter)
     148                 :             : {
     149                 :          20 : }
     150                 :             : 
     151                 :             : /**
     152                 :             :  * valent_application_plugin_activate: (virtual activate)
     153                 :             :  * @plugin: a `ValentApplicationPlugin`
     154                 :             :  *
     155                 :             :  * Handle activation of the application.
     156                 :             :  *
     157                 :             :  * Implementations should override this method to handle activation, as
     158                 :             :  * a result of [signal@Gio.Application::activate] being emitted on the primary
     159                 :             :  * instance of the application.
     160                 :             :  *
     161                 :             :  * Returns: %TRUE if handled, or %FALSE if not
     162                 :             :  *
     163                 :             :  * Since: 1.0
     164                 :             :  */
     165                 :             : gboolean
     166                 :           3 : valent_application_plugin_activate (ValentApplicationPlugin *plugin)
     167                 :             : {
     168                 :           3 :   gboolean ret;
     169                 :             : 
     170                 :           3 :   VALENT_ENTRY;
     171                 :             : 
     172         [ +  - ]:           3 :   g_return_val_if_fail (VALENT_IS_APPLICATION_PLUGIN (plugin), FALSE);
     173                 :             : 
     174                 :           3 :   ret = VALENT_APPLICATION_PLUGIN_GET_CLASS (plugin)->activate (plugin);
     175                 :             : 
     176                 :           3 :   VALENT_RETURN (ret);
     177                 :             : }
     178                 :             : 
     179                 :             : /**
     180                 :             :  * valent_application_plugin_command_line: (virtual command_line)
     181                 :             :  * @plugin: a `ValentApplicationPlugin`
     182                 :             :  * @command_line: a `GApplicationCommandLine`
     183                 :             :  *
     184                 :             :  * Handle the given command-line options.
     185                 :             :  *
     186                 :             :  * Implementations should override this method to handle command-line options,
     187                 :             :  * as a result of [signal@Gio.Application::command-line] being emitted on the
     188                 :             :  * primary instance of the application.
     189                 :             :  *
     190                 :             :  * Returns: an integer that is set as the exit status for the calling process
     191                 :             :  *
     192                 :             :  * Since: 1.0
     193                 :             :  */
     194                 :             : int
     195                 :           0 : valent_application_plugin_command_line (ValentApplicationPlugin *plugin,
     196                 :             :                                         GApplicationCommandLine *command_line)
     197                 :             : {
     198                 :           0 :   int ret;
     199                 :             : 
     200                 :           0 :   VALENT_ENTRY;
     201                 :             : 
     202         [ #  # ]:           0 :   g_return_val_if_fail (VALENT_IS_APPLICATION_PLUGIN (plugin), 1);
     203   [ #  #  #  #  :           0 :   g_return_val_if_fail (G_IS_APPLICATION_COMMAND_LINE (command_line), 1);
             #  #  #  # ]
     204                 :             : 
     205                 :           0 :   ret = VALENT_APPLICATION_PLUGIN_GET_CLASS (plugin)->command_line (plugin,
     206                 :             :                                                                     command_line);
     207                 :             : 
     208                 :           0 :   VALENT_RETURN (ret);
     209                 :             : }
     210                 :             : 
     211                 :             : /**
     212                 :             :  * valent_application_plugin_dbus_register: (virtual dbus_register)
     213                 :             :  * @plugin: a `ValentApplicationPlugin`
     214                 :             :  * @connection: a `Gio.DBusCOnnection`
     215                 :             :  * @object_path: a D-Bus object path
     216                 :             :  * @error: (nullable): a `GError`
     217                 :             :  *
     218                 :             :  * Handle the D-Bus registration phase of the application.
     219                 :             :  *
     220                 :             :  * Implementations may override this method to export extra objects on the
     221                 :             :  * bus, that need to exist before the application tries to own the bus name.
     222                 :             :  *
     223                 :             :  * D-Bus registration will be aborted if %FALSE is returned, so implementations
     224                 :             :  * may return %TRUE and report the error by other means if it is not intended
     225                 :             :  * to be fatal.
     226                 :             :  *
     227                 :             :  * Returns: %TRUE, or %FALSE with @error set
     228                 :             :  *
     229                 :             :  * Since: 1.0
     230                 :             :  */
     231                 :             : gboolean
     232                 :           7 : valent_application_plugin_dbus_register (ValentApplicationPlugin  *plugin,
     233                 :             :                                          GDBusConnection          *connection,
     234                 :             :                                          const char               *object_path,
     235                 :             :                                          GError                  **error)
     236                 :             : {
     237                 :           7 :   gboolean ret;
     238                 :             : 
     239                 :           7 :   VALENT_ENTRY;
     240                 :             : 
     241         [ +  - ]:           7 :   g_return_val_if_fail (VALENT_IS_APPLICATION_PLUGIN (plugin), FALSE);
     242   [ +  -  +  -  :           7 :   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
             -  +  -  - ]
     243         [ -  + ]:           7 :   g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE);
     244   [ +  +  -  + ]:           7 :   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
     245                 :             : 
     246                 :           7 :   ret = VALENT_APPLICATION_PLUGIN_GET_CLASS (plugin)->dbus_register (plugin,
     247                 :             :                                                                      connection,
     248                 :             :                                                                      object_path,
     249                 :             :                                                                      error);
     250                 :             : 
     251                 :           7 :   VALENT_RETURN (ret);
     252                 :             : }
     253                 :             : 
     254                 :             : /**
     255                 :             :  * valent_application_plugin_dbus_unregister: (virtual dbus_unregister)
     256                 :             :  * @plugin: a `ValentApplicationPlugin`
     257                 :             :  * @connection: a `Gio.DBusCOnnection`
     258                 :             :  * @object_path: a D
     259                 :             :  *
     260                 :             :  * Handle the D-Bus unregistration phase of the application.
     261                 :             :  *
     262                 :             :  * Implementations should override this method to unexport anything exported in
     263                 :             :  * [vfunc@Valent.ApplicationPlugin.dbus_register].
     264                 :             :  *
     265                 :             :  * Since: 1.0
     266                 :             :  */
     267                 :             : void
     268                 :           7 : valent_application_plugin_dbus_unregister (ValentApplicationPlugin  *plugin,
     269                 :             :                                            GDBusConnection          *connection,
     270                 :             :                                            const char               *object_path)
     271                 :             : {
     272                 :           7 :   VALENT_ENTRY;
     273                 :             : 
     274         [ +  - ]:           7 :   g_return_if_fail (VALENT_IS_APPLICATION_PLUGIN (plugin));
     275   [ +  -  +  -  :           7 :   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
             -  +  -  - ]
     276         [ -  + ]:           7 :   g_return_if_fail (g_variant_is_object_path (object_path));
     277                 :             : 
     278                 :           7 :   VALENT_APPLICATION_PLUGIN_GET_CLASS (plugin)->dbus_unregister (plugin,
     279                 :             :                                                                  connection,
     280                 :             :                                                                  object_path);
     281                 :             : 
     282                 :           7 :   VALENT_EXIT;
     283                 :             : }
     284                 :             : 
     285                 :             : /**
     286                 :             :  * valent_application_plugin_open: (virtual open)
     287                 :             :  * @plugin: a `ValentApplicationPlugin`
     288                 :             :  * @files: (array length=n_files): an array of `GFiles` to open
     289                 :             :  * @n_files: the length of the @files array
     290                 :             :  * @hint: (not nullable): a hint (or "")
     291                 :             :  *
     292                 :             :  * Open the given files.
     293                 :             :  *
     294                 :             :  * Implementations should override this method to handle files and URIs, as
     295                 :             :  * a result of [signal@Gio.Application::open] being emitted on the primary
     296                 :             :  * instance of the application.
     297                 :             :  *
     298                 :             :  * Returns: %TRUE if handled, or %FALSE if not
     299                 :             :  *
     300                 :             :  * Since: 1.0
     301                 :             :  */
     302                 :             : gboolean
     303                 :           1 : valent_application_plugin_open (ValentApplicationPlugin  *plugin,
     304                 :             :                                 GFile                   **files,
     305                 :             :                                 int                       n_files,
     306                 :             :                                 const char               *hint)
     307                 :             : {
     308                 :           1 :   gboolean ret;
     309                 :             : 
     310                 :           1 :   VALENT_ENTRY;
     311                 :             : 
     312         [ +  - ]:           1 :   g_return_val_if_fail (VALENT_IS_APPLICATION_PLUGIN (plugin), FALSE);
     313         [ -  + ]:           1 :   g_return_val_if_fail (files != NULL, FALSE);
     314         [ -  + ]:           1 :   g_return_val_if_fail (n_files > 0, FALSE);
     315         [ -  + ]:           1 :   g_return_val_if_fail (hint != NULL, FALSE);
     316                 :             : 
     317                 :           1 :   ret = VALENT_APPLICATION_PLUGIN_GET_CLASS (plugin)->open (plugin,
     318                 :             :                                                             files,
     319                 :             :                                                             n_files,
     320                 :             :                                                             hint);
     321                 :             : 
     322                 :           1 :   VALENT_RETURN (ret);
     323                 :             : }
     324                 :             : 
     325                 :             : /**
     326                 :             :  * valent_application_plugin_shutdown: (virtual shutdown)
     327                 :             :  * @plugin: a `ValentApplicationPlugin`
     328                 :             :  *
     329                 :             :  * Handle the shutdown phase of the application.
     330                 :             :  *
     331                 :             :  * Implementations should override this method to reverse anything done in
     332                 :             :  * [vfunc@Valent.ApplicationPlugin.startup].
     333                 :             :  *
     334                 :             :  * Since: 1.0
     335                 :             :  */
     336                 :             : void
     337                 :          16 : valent_application_plugin_shutdown (ValentApplicationPlugin *plugin)
     338                 :             : {
     339                 :          16 :   VALENT_ENTRY;
     340                 :             : 
     341         [ +  - ]:          16 :   g_return_if_fail (VALENT_IS_APPLICATION_PLUGIN (plugin));
     342                 :             : 
     343                 :          16 :   VALENT_APPLICATION_PLUGIN_GET_CLASS (plugin)->shutdown (plugin);
     344                 :             : 
     345                 :          16 :   VALENT_EXIT;
     346                 :             : }
     347                 :             : 
     348                 :             : /**
     349                 :             :  * valent_application_plugin_startup: (virtual startup)
     350                 :             :  * @plugin: a `ValentApplicationPlugin`
     351                 :             :  *
     352                 :             :  * Handle the startup phase of the application.
     353                 :             :  *
     354                 :             :  * Implementations may override this method to perform setup task that should
     355                 :             :  * only happen on the primary instance.
     356                 :             :  *
     357                 :             :  * Since: 1.0
     358                 :             :  */
     359                 :             : void
     360                 :          14 : valent_application_plugin_startup (ValentApplicationPlugin *plugin)
     361                 :             : {
     362                 :          14 :   VALENT_ENTRY;
     363                 :             : 
     364         [ +  - ]:          14 :   g_return_if_fail (VALENT_IS_APPLICATION_PLUGIN (plugin));
     365                 :             : 
     366                 :          14 :   VALENT_APPLICATION_PLUGIN_GET_CLASS (plugin)->startup (plugin);
     367                 :             : 
     368                 :          14 :   VALENT_EXIT;
     369                 :             : }
     370                 :             : 
        

Generated by: LCOV version 2.0-1