| 1 | /* |
| 2 | * Copyright (C) 2014 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-service |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <online-accounts-common/debug.h> |
| 22 | #include "notification.h" |
| 23 | |
| 24 | #include <QByteArray> |
| 25 | #include <QCoreApplication> |
| 26 | #include <QString> |
| 27 | #include <libnotify/notification.h> |
| 28 | #include <libnotify/notify.h> |
| 29 | |
| 30 | using namespace OnlineAccountsUi; |
| 31 | |
| 32 | namespace OnlineAccountsUi { |
| 33 | |
| 34 | class NotificationPrivate: public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | Q_DECLARE_PUBLIC(Notification) |
| 38 | |
| 39 | public: |
| 40 | NotificationPrivate(const QString &summary, |
| 41 | const QString &body, |
| 42 | Notification *notification); |
| 43 | ~NotificationPrivate(); |
| 44 | |
| 45 | static void actionCallback(NotifyNotification *, char *action, |
| 46 | Notification *q); |
| 47 | |
| 48 | private: |
| 49 | NotifyNotification *m_notification; |
| 50 | mutable Notification *q_ptr; |
| 51 | }; |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | static void onNotificationClosed(Notification *notification) { |
| 56 | Q_EMIT notification->closed(); |
| 57 | } |
| 58 | |
| 59 | NotificationPrivate::NotificationPrivate(const QString &summary, |
| 60 | const QString &body, |
| 61 | Notification *notification): |
| 62 | QObject(notification), |
| 63 | m_notification(0), |
| 64 | q_ptr(notification) |
| 65 | { |
| 66 | if (!notify_is_initted()) { |
| 67 | QByteArray name = QCoreApplication::applicationName().toUtf8(); |
| 68 | notify_init(name.constData()); |
| 69 | } |
| 70 | |
| 71 | QByteArray summaryUtf8 = summary.toUtf8(); |
| 72 | QByteArray bodyUtf8 = body.toUtf8(); |
| 73 | m_notification = notify_notification_new(summaryUtf8.constData(), |
| 74 | bodyUtf8.constData(), |
| 75 | NULL); |
| 76 | g_signal_connect_swapped(m_notification, "closed", |
| 77 | G_CALLBACK(onNotificationClosed), notification); |
| 78 | } |
| 79 | |
| 80 | NotificationPrivate::~NotificationPrivate() |
| 81 | { |
| 82 | g_object_unref(m_notification); |
| 83 | } |
| 84 | |
| 85 | void NotificationPrivate::actionCallback(NotifyNotification *, char *action, |
| 86 | Notification *q) |
| 87 | { |
| 88 | Q_EMIT q->actionInvoked(QString::fromUtf8(action)); |
| 89 | } |
| 90 | |
| 91 | Notification::Notification(const QString &summary, |
| 92 | const QString &body, |
| 93 | QObject *parent): |
| 94 | QObject(parent), |
| 95 | d_ptr(new NotificationPrivate(summary, body, this)) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | Notification::~Notification() |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | void Notification::addAction(const QString &action, const QString &label) |
| 104 | { |
| 105 | Q_D(Notification); |
| 106 | QByteArray actionUtf8 = action.toUtf8(); |
| 107 | QByteArray labelUtf8 = label.toUtf8(); |
| 108 | notify_notification_add_action(d->m_notification, |
| 109 | actionUtf8, labelUtf8, |
| 110 | NotifyActionCallback( |
| 111 | &NotificationPrivate::actionCallback), |
| 112 | this, NULL); |
| 113 | } |
| 114 | |
| 115 | void Notification::setSnapDecision(bool snapDecision) |
| 116 | { |
| 117 | Q_D(Notification); |
| 118 | notify_notification_set_hint(d->m_notification, |
| 119 | "x-lomiri-snap-decisions", |
| 120 | g_variant_new_boolean(snapDecision)); |
| 121 | } |
| 122 | |
| 123 | void Notification::show() |
| 124 | { |
| 125 | Q_D(Notification); |
| 126 | GError *error = NULL; |
| 127 | if (!notify_notification_show(d->m_notification, &error)) { |
| 128 | qWarning() << "Couldn't show notification:" << error->message; |
| 129 | g_clear_error(&error); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #include "notification.moc" |