| 1 | /* |
| 2 | * Copyright (C) 2014 Collabora Ltd. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at 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 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "WebKitNotification.h" |
| 22 | |
| 23 | #include "WebKitNotificationPrivate.h" |
| 24 | #include "WebNotification.h" |
| 25 | #include <glib/gi18n-lib.h> |
| 26 | #include <wtf/glib/WTFGType.h> |
| 27 | #include <wtf/text/CString.h> |
| 28 | |
| 29 | /** |
| 30 | * SECTION: WebKitNotification |
| 31 | * @Short_description: Object used to hold information about a notification that should be shown to the user. |
| 32 | * @Title: WebKitNotification |
| 33 | * |
| 34 | * Since: 2.8 |
| 35 | */ |
| 36 | |
| 37 | enum { |
| 38 | PROP_0, |
| 39 | |
| 40 | PROP_ID, |
| 41 | PROP_TITLE, |
| 42 | PROP_BODY, |
| 43 | PROP_TAG |
| 44 | }; |
| 45 | |
| 46 | enum { |
| 47 | CLOSED, |
| 48 | CLICKED, |
| 49 | |
| 50 | LAST_SIGNAL |
| 51 | }; |
| 52 | |
| 53 | struct _WebKitNotificationPrivate { |
| 54 | CString title; |
| 55 | CString body; |
| 56 | CString tag; |
| 57 | guint64 id; |
| 58 | |
| 59 | WebKitWebView* webView; |
| 60 | }; |
| 61 | |
| 62 | static guint signals[LAST_SIGNAL] = { 0, }; |
| 63 | |
| 64 | WEBKIT_DEFINE_TYPE(WebKitNotification, webkit_notification, G_TYPE_OBJECT) |
| 65 | |
| 66 | static void webkitNotificationGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec) |
| 67 | { |
| 68 | WebKitNotification* notification = WEBKIT_NOTIFICATION(object); |
| 69 | |
| 70 | switch (propId) { |
| 71 | case PROP_ID: |
| 72 | g_value_set_uint64(value, webkit_notification_get_id(notification)); |
| 73 | break; |
| 74 | case PROP_TITLE: |
| 75 | g_value_set_string(value, webkit_notification_get_title(notification)); |
| 76 | break; |
| 77 | case PROP_BODY: |
| 78 | g_value_set_string(value, webkit_notification_get_body(notification)); |
| 79 | break; |
| 80 | case PROP_TAG: |
| 81 | g_value_set_string(value, webkit_notification_get_tag(notification)); |
| 82 | break; |
| 83 | default: |
| 84 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static void webkit_notification_class_init(WebKitNotificationClass* notificationClass) |
| 89 | { |
| 90 | GObjectClass* objectClass = G_OBJECT_CLASS(notificationClass); |
| 91 | objectClass->get_property = webkitNotificationGetProperty; |
| 92 | |
| 93 | /** |
| 94 | * WebKitNotification:id: |
| 95 | * |
| 96 | * The unique id for the notification. |
| 97 | * |
| 98 | * Since: 2.8 |
| 99 | */ |
| 100 | g_object_class_install_property(objectClass, |
| 101 | PROP_ID, |
| 102 | g_param_spec_uint64("id" , |
| 103 | _("ID" ), |
| 104 | _("The unique id for the notification" ), |
| 105 | 0, G_MAXUINT64, 0, |
| 106 | WEBKIT_PARAM_READABLE)); |
| 107 | |
| 108 | /** |
| 109 | * WebKitNotification:title: |
| 110 | * |
| 111 | * The title for the notification. |
| 112 | * |
| 113 | * Since: 2.8 |
| 114 | */ |
| 115 | g_object_class_install_property(objectClass, |
| 116 | PROP_TITLE, |
| 117 | g_param_spec_string("title" , |
| 118 | _("Title" ), |
| 119 | _("The title for the notification" ), |
| 120 | nullptr, |
| 121 | WEBKIT_PARAM_READABLE)); |
| 122 | |
| 123 | /** |
| 124 | * WebKitNotification:body: |
| 125 | * |
| 126 | * The body for the notification. |
| 127 | * |
| 128 | * Since: 2.8 |
| 129 | */ |
| 130 | g_object_class_install_property(objectClass, |
| 131 | PROP_BODY, |
| 132 | g_param_spec_string("body" , |
| 133 | _("Body" ), |
| 134 | _("The body for the notification" ), |
| 135 | nullptr, |
| 136 | WEBKIT_PARAM_READABLE)); |
| 137 | |
| 138 | /** |
| 139 | * WebKitNotification:tag: |
| 140 | * |
| 141 | * The tag identifier for the notification. |
| 142 | * |
| 143 | * Since: 2.16 |
| 144 | */ |
| 145 | g_object_class_install_property(objectClass, |
| 146 | PROP_TAG, |
| 147 | g_param_spec_string("tag" , |
| 148 | _("Tag" ), |
| 149 | _("The tag identifier for the notification" ), |
| 150 | nullptr, |
| 151 | WEBKIT_PARAM_READABLE)); |
| 152 | |
| 153 | /** |
| 154 | * WebKitNotification::closed: |
| 155 | * @notification: the #WebKitNotification on which the signal is emitted |
| 156 | * |
| 157 | * Emitted when a notification has been withdrawn. |
| 158 | * |
| 159 | * The default handler will close the notification using libnotify, if built with |
| 160 | * support for it. |
| 161 | * |
| 162 | * Since: 2.8 |
| 163 | */ |
| 164 | signals[CLOSED] = |
| 165 | g_signal_new( |
| 166 | "closed" , |
| 167 | G_TYPE_FROM_CLASS(notificationClass), |
| 168 | G_SIGNAL_RUN_LAST, |
| 169 | 0, 0, |
| 170 | nullptr, |
| 171 | g_cclosure_marshal_VOID__VOID, |
| 172 | G_TYPE_NONE, 0); |
| 173 | |
| 174 | /** |
| 175 | * WebKitNotification::clicked: |
| 176 | * @notification: the #WebKitNotification on which the signal is emitted |
| 177 | * |
| 178 | * Emitted when a notification has been clicked. See webkit_notification_clicked(). |
| 179 | * |
| 180 | * Since: 2.12 |
| 181 | */ |
| 182 | signals[CLICKED] = |
| 183 | g_signal_new( |
| 184 | "clicked" , |
| 185 | G_TYPE_FROM_CLASS(notificationClass), |
| 186 | G_SIGNAL_RUN_LAST, |
| 187 | 0, 0, |
| 188 | nullptr, |
| 189 | g_cclosure_marshal_VOID__VOID, |
| 190 | G_TYPE_NONE, 0); |
| 191 | } |
| 192 | |
| 193 | WebKitNotification* webkitNotificationCreate(WebKitWebView* webView, const WebKit::WebNotification& webNotification) |
| 194 | { |
| 195 | WebKitNotification* notification = WEBKIT_NOTIFICATION(g_object_new(WEBKIT_TYPE_NOTIFICATION, nullptr)); |
| 196 | notification->priv->id = webNotification.notificationID(); |
| 197 | notification->priv->title = webNotification.title().utf8(); |
| 198 | notification->priv->body = webNotification.body().utf8(); |
| 199 | notification->priv->tag = webNotification.tag().utf8(); |
| 200 | notification->priv->webView = webView; |
| 201 | return notification; |
| 202 | } |
| 203 | |
| 204 | WebKitWebView* webkitNotificationGetWebView(WebKitNotification* notification) |
| 205 | { |
| 206 | return notification->priv->webView; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * webkit_notification_get_id: |
| 211 | * @notification: a #WebKitNotification |
| 212 | * |
| 213 | * Obtains the unique id for the notification. |
| 214 | * |
| 215 | * Returns: the unique id for the notification |
| 216 | * |
| 217 | * Since: 2.8 |
| 218 | */ |
| 219 | guint64 webkit_notification_get_id(WebKitNotification* notification) |
| 220 | { |
| 221 | g_return_val_if_fail(WEBKIT_IS_NOTIFICATION(notification), 0); |
| 222 | |
| 223 | return notification->priv->id; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * webkit_notification_get_title: |
| 228 | * @notification: a #WebKitNotification |
| 229 | * |
| 230 | * Obtains the title for the notification. |
| 231 | * |
| 232 | * Returns: the title for the notification |
| 233 | * |
| 234 | * Since: 2.8 |
| 235 | */ |
| 236 | const gchar* webkit_notification_get_title(WebKitNotification* notification) |
| 237 | { |
| 238 | g_return_val_if_fail(WEBKIT_IS_NOTIFICATION(notification), nullptr); |
| 239 | |
| 240 | return notification->priv->title.data(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * webkit_notification_get_body: |
| 245 | * @notification: a #WebKitNotification |
| 246 | * |
| 247 | * Obtains the body for the notification. |
| 248 | * |
| 249 | * Returns: the body for the notification |
| 250 | * |
| 251 | * Since: 2.8 |
| 252 | */ |
| 253 | const gchar* webkit_notification_get_body(WebKitNotification* notification) |
| 254 | { |
| 255 | g_return_val_if_fail(WEBKIT_IS_NOTIFICATION(notification), nullptr); |
| 256 | |
| 257 | return notification->priv->body.data(); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * webkit_notification_get_tag: |
| 262 | * @notification: a #WebKitNotification |
| 263 | * |
| 264 | * Obtains the tag identifier for the notification. |
| 265 | * |
| 266 | * Returns: (allow-none): the tag for the notification |
| 267 | * |
| 268 | * Since: 2.16 |
| 269 | */ |
| 270 | const gchar* webkit_notification_get_tag(WebKitNotification* notification) |
| 271 | { |
| 272 | g_return_val_if_fail(WEBKIT_IS_NOTIFICATION(notification), nullptr); |
| 273 | |
| 274 | const gchar* tag = notification->priv->tag.data(); |
| 275 | return notification->priv->tag.length() ? tag : nullptr; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * webkit_notification_close: |
| 280 | * @notification: a #WebKitNotification |
| 281 | * |
| 282 | * Closes the notification. |
| 283 | * |
| 284 | * Since: 2.8 |
| 285 | */ |
| 286 | void webkit_notification_close(WebKitNotification* notification) |
| 287 | { |
| 288 | g_return_if_fail(WEBKIT_IS_NOTIFICATION(notification)); |
| 289 | |
| 290 | g_signal_emit(notification, signals[CLOSED], 0); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * webkit_notification_clicked: |
| 295 | * @notification: a #WebKitNotification |
| 296 | * |
| 297 | * Tells WebKit the notification has been clicked. This will emit the |
| 298 | * #WebKitNotification::clicked signal. |
| 299 | * |
| 300 | * Since: 2.12 |
| 301 | */ |
| 302 | void webkit_notification_clicked(WebKitNotification* notification) |
| 303 | { |
| 304 | g_return_if_fail(WEBKIT_IS_NOTIFICATION(notification)); |
| 305 | |
| 306 | g_signal_emit(notification, signals[CLICKED], 0); |
| 307 | } |
| 308 | |