(root)/
glib-2.79.0/
gio/
gnotificationbackend.c
       1  /*
       2   * Copyright © 2013 Lars Uebernickel
       3   *
       4   * SPDX-License-Identifier: LGPL-2.1-or-later
       5   *
       6   * This library is free software; you can redistribute it and/or
       7   * modify it under the terms of the GNU Lesser General Public
       8   * License as published by the Free Software Foundation; either
       9   * version 2.1 of the License, or (at your option) any later version.
      10   *
      11   * This library is distributed in the hope that it will be useful,
      12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14   * Lesser General Public License for more details.
      15   *
      16   * You should have received a copy of the GNU Lesser General
      17   * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18   *
      19   * Authors: Lars Uebernickel <lars@uebernic.de>
      20   */
      21  
      22  #include "gnotificationbackend.h"
      23  
      24  #include "gnotification.h"
      25  #include "gapplication.h"
      26  #include "gactiongroup.h"
      27  #include "giomodule-priv.h"
      28  
      29  G_DEFINE_TYPE (GNotificationBackend, g_notification_backend, G_TYPE_OBJECT)
      30  
      31  static void
      32  g_notification_backend_dispose (GObject *obj)
      33  {
      34    GNotificationBackend *backend = G_NOTIFICATION_BACKEND (obj);
      35  
      36    backend->application = NULL;  /* no reference held, but clear the pointer anyway to avoid it dangling */
      37    g_clear_object (&backend->dbus_connection);
      38  
      39    G_OBJECT_CLASS (g_notification_backend_parent_class)->dispose (obj);
      40  }
      41  
      42  static void
      43  g_notification_backend_class_init (GNotificationBackendClass *class)
      44  {
      45    GObjectClass *object_class = G_OBJECT_CLASS (class);
      46  
      47    object_class->dispose = g_notification_backend_dispose;
      48  }
      49  
      50  static void
      51  g_notification_backend_init (GNotificationBackend *backend)
      52  {
      53  }
      54  
      55  GNotificationBackend *
      56  g_notification_backend_new_default (GApplication *application)
      57  {
      58    GType backend_type;
      59    GNotificationBackend *backend;
      60  
      61    g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
      62  
      63    backend_type = _g_io_module_get_default_type (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
      64                                                  "GNOTIFICATION_BACKEND",
      65                                                  G_STRUCT_OFFSET (GNotificationBackendClass, is_supported));
      66  
      67    backend = g_object_new (backend_type, NULL);
      68  
      69    /* Avoid ref cycle by not taking a ref to the application at all. The
      70     * backend only lives as long as the application does.
      71     */
      72    backend->application = application;
      73  
      74    backend->dbus_connection = g_application_get_dbus_connection (application);
      75    if (backend->dbus_connection)
      76      g_object_ref (backend->dbus_connection);
      77  
      78    return backend;
      79  }
      80  
      81  void
      82  g_notification_backend_send_notification (GNotificationBackend *backend,
      83                                            const gchar          *id,
      84                                            GNotification        *notification)
      85  {
      86    g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
      87    g_return_if_fail (G_IS_NOTIFICATION (notification));
      88  
      89    G_NOTIFICATION_BACKEND_GET_CLASS (backend)->send_notification (backend, id, notification);
      90  }
      91  
      92  void
      93  g_notification_backend_withdraw_notification (GNotificationBackend *backend,
      94                                                const gchar          *id)
      95  {
      96    g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
      97    g_return_if_fail (id != NULL);
      98  
      99    G_NOTIFICATION_BACKEND_GET_CLASS (backend)->withdraw_notification (backend, id);
     100  }