Add GNotification
[platform/upstream/glib.git] / gio / gnotificationbackend.c
1 /*
2  * Copyright © 2013 Lars Uebernickel
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
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_class_init (GNotificationBackendClass *class)
33 {
34 }
35
36 static void
37 g_notification_backend_init (GNotificationBackend *backend)
38 {
39 }
40
41 GNotificationBackend *
42 g_notification_backend_new_default (GApplication *application)
43 {
44   GType backend_type;
45   GNotificationBackend *backend;
46
47   g_return_val_if_fail (G_IS_APPLICATION (application), NULL);
48
49   backend_type = _g_io_module_get_default_type (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
50                                                 "GNOTIFICATION_BACKEND",
51                                                 G_STRUCT_OFFSET (GNotificationBackendClass, is_supported));
52
53   backend = g_object_new (backend_type, NULL);
54
55   /* Avoid ref cycle by not taking a ref to the application at all. The
56    * backend only lives as long as the application does.
57    */
58   backend->application = application;
59
60   backend->dbus_connection = g_object_ref (g_application_get_dbus_connection (application));
61
62   return backend;
63 }
64
65 void
66 g_notification_backend_send_notification (GNotificationBackend *backend,
67                                           const gchar          *id,
68                                           GNotification        *notification)
69 {
70   g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
71   g_return_if_fail (G_IS_NOTIFICATION (notification));
72
73   G_NOTIFICATION_BACKEND_GET_CLASS (backend)->send_notification (backend, id, notification);
74 }
75
76 void
77 g_notification_backend_withdraw_notification (GNotificationBackend *backend,
78                                               const gchar          *id)
79 {
80   g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
81   g_return_if_fail (id != NULL);
82
83   G_NOTIFICATION_BACKEND_GET_CLASS (backend)->withdraw_notification (backend, id);
84 }
85
86 GApplication *
87 g_notification_backend_get_application (GNotificationBackend *backend)
88 {
89   g_return_val_if_fail (G_IS_NOTIFICATION_BACKEND (backend), NULL);
90
91   return backend->application;
92 }
93
94 void
95 g_notification_backend_activate_action (GNotificationBackend *backend,
96                                         const gchar          *name,
97                                         GVariant             *parameter)
98 {
99   g_return_if_fail (G_IS_NOTIFICATION_BACKEND (backend));
100   g_return_if_fail (name != NULL);
101
102   if (g_str_has_prefix (name, "app."))
103     g_action_group_activate_action (G_ACTION_GROUP (backend->application), name + 4, parameter);
104 }
105
106 GDBusConnection *
107 g_notification_backend_get_dbus_connection (GNotificationBackend *backend)
108 {
109   g_return_val_if_fail (G_IS_NOTIFICATION_BACKEND (backend), NULL);
110
111   return backend->dbus_connection;
112 }