~canonical-platform-qa/ubuntu-system-settings-online-accounts/launch_fixture

« back to all changes in this revision

Viewing changes to src/notification.cpp

  • Committer: CI bot
  • Author(s): Alberto Mardegan
  • Date: 2014-06-12 13:37:46 UTC
  • mfrom: (107.1.19 master)
  • Revision ID: ps-jenkins@lists.canonical.com-20140612133746-l203pxlimfpevv6r
New version

- Use signon-ui-service, to fix co-installation with of signon-ui. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical Ltd.
 
3
 *
 
4
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 
5
 *
 
6
 * This file is part of online-accounts-ui
 
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 "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
NotificationPrivate::NotificationPrivate(const QString &summary,
 
56
                                         const QString &body,
 
57
                                         Notification *notification):
 
58
    QObject(notification),
 
59
    m_notification(0),
 
60
    q_ptr(notification)
 
61
{
 
62
    if (!notify_is_initted()) {
 
63
        QByteArray name = QCoreApplication::applicationName().toUtf8();
 
64
        notify_init(name.constData());
 
65
    }
 
66
 
 
67
    QByteArray summaryUtf8 = summary.toUtf8();
 
68
    QByteArray bodyUtf8 = body.toUtf8();
 
69
    m_notification = notify_notification_new(summaryUtf8.constData(),
 
70
                                             bodyUtf8.constData(),
 
71
                                             NULL);
 
72
    g_signal_connect_swapped(m_notification, "closed",
 
73
                             G_CALLBACK(&Notification::closed), notification);
 
74
}
 
75
 
 
76
NotificationPrivate::~NotificationPrivate()
 
77
{
 
78
    g_object_unref(m_notification);
 
79
}
 
80
 
 
81
void NotificationPrivate::actionCallback(NotifyNotification *, char *action,
 
82
                                         Notification *q)
 
83
{
 
84
    Q_EMIT q->actionInvoked(QString::fromUtf8(action));
 
85
}
 
86
 
 
87
Notification::Notification(const QString &summary,
 
88
                           const QString &body,
 
89
                           QObject *parent):
 
90
    QObject(parent),
 
91
    d_ptr(new NotificationPrivate(summary, body, this))
 
92
{
 
93
}
 
94
 
 
95
Notification::~Notification()
 
96
{
 
97
}
 
98
 
 
99
void Notification::addAction(const QString &action, const QString &label)
 
100
{
 
101
    Q_D(Notification);
 
102
    QByteArray actionUtf8 = action.toUtf8();
 
103
    QByteArray labelUtf8 = label.toUtf8();
 
104
    notify_notification_add_action(d->m_notification,
 
105
                                   actionUtf8, labelUtf8,
 
106
                                   NotifyActionCallback(
 
107
                                       &NotificationPrivate::actionCallback),
 
108
                                   this, NULL);
 
109
}
 
110
 
 
111
void Notification::show()
 
112
{
 
113
    Q_D(Notification);
 
114
    GError *error = NULL;
 
115
    if (!notify_notification_show(d->m_notification, &error)) {
 
116
        qWarning() << "Couldn't show notification:" << error->message;
 
117
        g_clear_error(&error);
 
118
    }
 
119
}
 
120
 
 
121
#include "notification.moc"