ringnotify.cpp 11.9 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
3
 *  Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4
 *  Author: Sebastien Blin <sebastien.blin@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#include "ringnotify.h"
#include "config.h"
23
#include "ring_client.h"
24

25 26 27 28
#if USE_CANBERRA
#include <canberra-gtk.h>
#endif // USE_CANBERRA

29
#if USE_LIBNOTIFY
30
#include <glib/gi18n.h>
31 32
#include <libnotify/notify.h>
#include <memory>
33 34
#include <globalinstances.h>
#include "native/pixbufmanipulator.h"
35 36 37
#include <QtCore/QSize>
#endif

38 39

static constexpr const char* SERVER_NOTIFY_OSD = "notify-osd";
40 41
static constexpr const char* NOTIFICATION_FILE = SOUNDSDIR "/ringtone_notify.wav";

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
namespace details
{
class CppImpl;
}

struct _RingNotifier
{
    GtkBox parent;
};

struct _RingNotifierClass
{
    GtkBoxClass parent_class;
};

typedef struct _RingNotifierPrivate RingNotifierPrivate;

struct _RingNotifierPrivate
{
    details::CppImpl* cpp; ///< Non-UI and C++ only code0
};

G_DEFINE_TYPE_WITH_PRIVATE(RingNotifier, ring_notifier, GTK_TYPE_BOX);

#define RING_NOTIFIER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RING_NOTIFIER_TYPE, RingNotifierPrivate))

/* signals */
enum {
    SHOW_CHAT,
    ACCEPT_PENDING,
    REFUSE_PENDING,
    ACCEPT_CALL,
    DECLINE_CALL,
    LAST_SIGNAL
};

static guint ring_notifier_signals[LAST_SIGNAL] = { 0 };

namespace details
{

class CppImpl
{
public:
    explicit CppImpl(RingNotifier& widget);
    ~CppImpl();

    RingNotifier* self = nullptr; // The GTK widget itself
    RingNotifierPrivate* priv = nullptr;

92 93 94 95 96 97 98 99
    /* server info and capabilities */
    char *name = nullptr;
    char *vendor = nullptr;
    char *version = nullptr;
    char *spec = nullptr;
    gboolean append;
    gboolean actions;

100 101 102 103 104 105 106 107
    std::map<std::string, std::shared_ptr<NotifyNotification>> notifications_;
private:
    CppImpl() = delete;
    CppImpl(const CppImpl&) = delete;
    CppImpl& operator=(const CppImpl&) = delete;
};

CppImpl::CppImpl(RingNotifier& widget)
108 109 110
: self {&widget}
{
}
111 112

CppImpl::~CppImpl()
113 114 115 116 117 118 119 120 121
{
    if (name)
        g_free(name);
    if (vendor)
        g_free(vendor);
    if (version)
        g_free(version);
    if (spec)
        g_free(spec);
122 123
}

124 125
} // namespace details

126 127 128 129 130 131 132 133 134 135 136 137
static void
ring_notifier_dispose(GObject *object)
{
    auto* self = RING_NOTIFIER(object);
    auto* priv = RING_NOTIFIER_GET_PRIVATE(self);

    delete priv->cpp;
    priv->cpp = nullptr;

#if USE_LIBNOTIFY
    if (notify_is_initted())
        notify_uninit();
138 139
#endif

140 141 142 143 144
    G_OBJECT_CLASS(ring_notifier_parent_class)->dispose(object);
}

static void
ring_notifier_init(RingNotifier *view)
145
{
146 147 148
    RingNotifierPrivate *priv = RING_NOTIFIER_GET_PRIVATE(view);
    priv->cpp = new details::CppImpl {*view};

149 150
#if USE_LIBNOTIFY
    notify_init("Ring");
151 152

    /* get notify server info */
153 154 155 156
    if (notify_get_server_info(&priv->cpp->name,
                               &priv->cpp->vendor,
                               &priv->cpp->version,
                               &priv->cpp->spec)) {
157
        g_debug("notify server name: %s, vendor: %s, version: %s, spec: %s",
158
                priv->cpp->name, priv->cpp->vendor, priv->cpp->version, priv->cpp->spec);
159 160 161 162 163 164 165
    }

    /* check  notify server capabilities */
    auto list = notify_get_server_caps();
    while (list) {
        if (g_strcmp0((const char *)list->data, "append") == 0 ||
            g_strcmp0((const char *)list->data, "x-canonical-append") == 0) {
166
            priv->cpp->append = TRUE;
167 168
        }
        if (g_strcmp0((const char *)list->data, "actions") == 0) {
169
            priv->cpp->actions = TRUE;
170 171 172 173 174 175
        }

        list = g_list_next(list);
    }

    g_list_free_full(list, g_free);
176 177 178
#endif
}

179 180
static void
ring_notifier_class_init(RingNotifierClass *klass)
181
{
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    G_OBJECT_CLASS(klass)->dispose = ring_notifier_dispose;

    ring_notifier_signals[SHOW_CHAT] = g_signal_new(
        "showChatView",
        G_TYPE_FROM_CLASS(klass),
        (GSignalFlags) (G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED),
        0,
        nullptr,
        nullptr,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1, G_TYPE_STRING);

    ring_notifier_signals[ACCEPT_PENDING] = g_signal_new(
        "acceptPending",
        G_TYPE_FROM_CLASS(klass),
        (GSignalFlags) (G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED),
        0,
        nullptr,
        nullptr,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1, G_TYPE_STRING);

    ring_notifier_signals[REFUSE_PENDING] = g_signal_new(
        "refusePending",
        G_TYPE_FROM_CLASS(klass),
        (GSignalFlags) (G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED),
        0,
        nullptr,
        nullptr,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1, G_TYPE_STRING);

    ring_notifier_signals[ACCEPT_CALL] = g_signal_new(
        "acceptCall",
        G_TYPE_FROM_CLASS(klass),
        (GSignalFlags) (G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED),
        0,
        nullptr,
        nullptr,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1, G_TYPE_STRING);

    ring_notifier_signals[DECLINE_CALL] = g_signal_new(
        "declineCall",
        G_TYPE_FROM_CLASS(klass),
        (GSignalFlags) (G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED),
        0,
        nullptr,
        nullptr,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1, G_TYPE_STRING);
233 234
}

235 236
GtkWidget *
ring_notifier_new()
237
{
238 239
    gpointer view = g_object_new(RING_NOTIFIER_TYPE, NULL);
    return (GtkWidget *)view;
240 241
}

242
#if USE_LIBNOTIFY
243
static void
244
show_chat_view(NotifyNotification*, char* id, RingNotifier* view)
245
{
246
    g_signal_emit(G_OBJECT(view), ring_notifier_signals[SHOW_CHAT], 0, id);
247 248
}

249 250
static void
accept_pending(NotifyNotification*, char* id, RingNotifier* view)
251
{
252 253
    std::string newId = id;
    g_signal_emit(G_OBJECT(view), ring_notifier_signals[ACCEPT_PENDING], 0, newId.substr(std::string("add:").length()).c_str());
254
}
255

256
static void
257
refuse_pending(NotifyNotification*, char* id, RingNotifier* view)
258
{
259 260
    std::string newId = id;
    g_signal_emit(G_OBJECT(view), ring_notifier_signals[REFUSE_PENDING], 0, newId.substr(std::string("rm:").length()).c_str());
261 262 263
}

static void
264
accept_call(NotifyNotification*, char* id, RingNotifier* view)
265
{
266 267
    std::string newId = id;
    g_signal_emit(G_OBJECT(view), ring_notifier_signals[ACCEPT_CALL], 0, newId.substr(std::string("accept:").length()).c_str());
268 269 270
}

static void
271
decline_call(NotifyNotification*, char* id, RingNotifier* view)
272
{
273 274
    std::string newId = id;
    g_signal_emit(G_OBJECT(view), ring_notifier_signals[DECLINE_CALL], 0, newId.substr(std::string("decline:").length()).c_str());
275 276
}

277 278 279 280 281 282 283
#endif

gboolean
ring_show_notification(RingNotifier* view, const std::string& icon,
                       const std::string& uri, const std::string& name,
                       const std::string& id, const std::string& title,
                       const std::string& body, NotificationType type)
284
{
285
    g_return_val_if_fail(IS_RING_NOTIFIER(view), false);
286
    gboolean success = FALSE;
287
    RingNotifierPrivate *priv = RING_NOTIFIER_GET_PRIVATE(view);
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
#if USE_LIBNOTIFY
    std::shared_ptr<NotifyNotification> notification(
        notify_notification_new(title.c_str(), body.c_str(), nullptr), g_object_unref);
    priv->cpp->notifications_.emplace(id, notification);

    // Draw icon
    auto firstLetter = name.empty() ? "" : QString(QString(name.c_str()).at(0)).toStdString();  // NOTE best way to be compatible with UTF-8
    auto default_avatar = Interfaces::PixbufManipulator().generateAvatar(firstLetter, uri);
    auto photo = Interfaces::PixbufManipulator().scaleAndFrame(default_avatar.get(), QSize(50, 50));
    if (!icon.empty()) {
        QByteArray byteArray(icon.c_str(), icon.length());
        QVariant avatar = Interfaces::PixbufManipulator().personPhoto(byteArray);
        auto pixbuf_photo = Interfaces::PixbufManipulator().scaleAndFrame(avatar.value<std::shared_ptr<GdkPixbuf>>().get(), QSize(50, 50));
        if (avatar.isValid()) {
            photo = pixbuf_photo;
304
        }
305 306
    }
    notify_notification_set_image_from_pixbuf(notification.get(), photo.get());
307

308 309 310
    if (type != NotificationType::CHAT) {
        notify_notification_set_urgency(notification.get(), NOTIFY_URGENCY_CRITICAL);
        notify_notification_set_timeout(notification.get(), NOTIFY_EXPIRES_DEFAULT);
311
    } else {
312
        notify_notification_set_urgency(notification.get(), NOTIFY_URGENCY_NORMAL);
313 314
    }

315
#if USE_CANBERRA
316 317 318 319 320 321 322 323
    if (type != NotificationType::CALL) {
        auto status = ca_context_play(ca_gtk_context_get(),
                                      0,
                                      CA_PROP_MEDIA_FILENAME,
                                      NOTIFICATION_FILE,
                                      nullptr);
        if (status != 0)
            g_warning("ca_context_play: %s", ca_strerror(status));
324
    }
325
#endif // USE_CANBERRA
326

327
    // if the notification server supports actions, make the default action to show the chat view
328
    if (priv->cpp->actions) {
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        if (type != NotificationType::CALL) {
            notify_notification_add_action(notification.get(),
                id.c_str(),
                C_("", "Open conversation"),
                (NotifyActionCallback)show_chat_view,
                view,
                nullptr);
            if (type != NotificationType::CHAT) {
                auto addId = "add:" + id;
                notify_notification_add_action(notification.get(),
                    addId.c_str(),
                    C_("", "Accept"),
                    (NotifyActionCallback)accept_pending,
                    view,
                    nullptr);
                auto rmId = "rm:" + id;
                notify_notification_add_action(notification.get(),
                    rmId.c_str(),
                    C_("", "Refuse"),
                    (NotifyActionCallback)refuse_pending,
                    view,
                    nullptr);
            }
        } else {
            auto acceptId = "accept:" + id;
            notify_notification_add_action(notification.get(),
                acceptId.c_str(),
                C_("", "Accept"),
                (NotifyActionCallback)accept_call,
                view,
                nullptr);
            auto declineId = "decline:" + id;
            notify_notification_add_action(notification.get(),
                declineId.c_str(),
                C_("", "Decline"),
                (NotifyActionCallback)decline_call,
                view,
                nullptr);
        }
368 369
    }

370 371
    GError *error = nullptr;
    success = notify_notification_show(notification.get(), &error);
372

373 374 375
    if (error) {
        g_warning("failed to show notification: %s", error->message);
        g_clear_error(&error);
Nicolas Jager's avatar
Nicolas Jager committed
376
    }
377
#endif
378
    return success;
379
}
380 381

gboolean
382
ring_hide_notification(RingNotifier* view, const std::string& id)
383
{
384 385
    g_return_val_if_fail(IS_RING_NOTIFIER(view), false);
    RingNotifierPrivate *priv = RING_NOTIFIER_GET_PRIVATE(view);
386 387

#if USE_LIBNOTIFY
388 389 390
    // Search
    auto notification = priv->cpp->notifications_.find(id);
    if (notification == priv->cpp->notifications_.end()) {
391
        return FALSE;
392
    }
393

394 395 396 397 398
    // Close
    GError *error = nullptr;
    if (!notify_notification_close(notification->second.get(), &error)) {
        g_warning("could not close notification: %s", error->message);
        g_clear_error(&error);
399
        return FALSE;
400
    }
401

402 403
    // Erase
    priv->cpp->notifications_.erase(id);
404
#endif
405 406

    return TRUE;
407
}