incomingcallview.cpp 12.8 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
3
 *  Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 5
 *  Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com>
 *  Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
6
 *  Author: Hugo Lefeuvre <hugo.lefeuvre@savoirfairelinux.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  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 "incomingcallview.h"

25 26 27 28
/* std */
#include <stdexcept>

// GTK+ related
29
#include <gtk/gtk.h>
30 31 32 33
#include <glib/gi18n.h>
#include <glib/gstdio.h>

// Qt
34 35 36
#include <QSize>

// Lrc
37
#include <api/avmodel.h>
38 39 40 41
#include <api/newcallmodel.h>
#include <api/contactmodel.h>
#include <api/conversationmodel.h>
#include <api/contact.h>
42
#include <globalinstances.h>
43 44

// Client
45
#include "chatview.h"
46
#include "messagingwidget.h"
47 48
#include "native/pixbufmanipulator.h"
#include "utils/drawing.h"
49
#include "utils/files.h"
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

struct _IncomingCallView
{
    GtkBox parent;
};

struct _IncomingCallViewClass
{
    GtkBoxClass parent_class;
};

typedef struct _IncomingCallViewPrivate IncomingCallViewPrivate;

struct _IncomingCallViewPrivate
{
65
    GtkWidget *paned_call;
66
    GtkWidget *image_incoming;
67
    GtkWidget *label_name;
68
    GtkWidget *label_bestId;
69
    GtkWidget *spinner_status;
70 71 72
    GtkWidget *label_status;
    GtkWidget *button_accept_incoming;
    GtkWidget *button_reject_incoming;
73
    GtkWidget *frame_chat;
74 75
    GtkWidget *box_messaging_widget;
    GtkWidget *messaging_widget;
76

77
    // The webkit_chat_container is created once, then reused for all chat views
aviau's avatar
aviau committed
78 79
    GtkWidget *webkit_chat_container;

80
    lrc::api::conversation::Info* conversation_;
81
    AccountInfoPointer const * accountInfo_;
82

83
    QMetaObject::Connection state_change_connection;
84 85

    GSettings *settings;
86 87

    lrc::api::AVModel* avModel_;
88 89 90 91 92 93
};

G_DEFINE_TYPE_WITH_PRIVATE(IncomingCallView, incoming_call_view, GTK_TYPE_BOX);

#define INCOMING_CALL_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), INCOMING_CALL_VIEW_TYPE, IncomingCallViewPrivate))

94
static void
95
incoming_call_view_dispose(GObject *object)
96 97 98 99 100 101 102
{
    IncomingCallView *view;
    IncomingCallViewPrivate *priv;

    view = INCOMING_CALL_VIEW(object);
    priv = INCOMING_CALL_VIEW_GET_PRIVATE(view);

103 104 105 106 107
    // navbar was hidden during init, we need to make it visible again before view destruction
    auto children = gtk_container_get_children(GTK_CONTAINER(priv->frame_chat));
    auto chat_view = children->data;
    chat_view_set_header_visible(CHAT_VIEW(chat_view), TRUE);

108 109
    QObject::disconnect(priv->state_change_connection);

110 111
    g_clear_object(&priv->settings);

112 113 114
    G_OBJECT_CLASS(incoming_call_view_parent_class)->dispose(object);
}

115 116 117
static gboolean
map_boolean_to_orientation(GValue *value, GVariant *variant, G_GNUC_UNUSED gpointer user_data)
{
118
    bool ret = FALSE;
119 120 121 122 123 124 125 126
    if (g_variant_is_of_type(variant, G_VARIANT_TYPE_BOOLEAN)) {
        if (g_variant_get_boolean(variant)) {
            // true, chat should be horizontal (to the right)
            g_value_set_enum(value, GTK_ORIENTATION_HORIZONTAL);
        } else {
            // false, chat should be vertical (at the bottom)
            g_value_set_enum(value, GTK_ORIENTATION_VERTICAL);
        }
127
        ret = TRUE;
128
    }
129
    return ret;
130 131
}

132
static void
133
reject_incoming_call(IncomingCallView *self)
134
{
135
    g_return_if_fail(IS_INCOMING_CALL_VIEW(self));
136
    auto priv = INCOMING_CALL_VIEW_GET_PRIVATE(self);
137
    (*priv->accountInfo_)->callModel->hangUp(priv->conversation_->callId);
138 139 140
}

static void
141
accept_incoming_call(IncomingCallView *self)
142
{
143
    g_return_if_fail(IS_INCOMING_CALL_VIEW(self));
144
    auto priv = INCOMING_CALL_VIEW_GET_PRIVATE(self);
145

146
    try {
147
        auto contactUri = priv->conversation_->participants.at(0);
148
        auto contact = (*priv->accountInfo_)->contactModel->getContact(contactUri);
149 150
        // If the contact is pending, we should accept its request
        if (contact.profileInfo.type == lrc::api::profile::Type::PENDING)
151
            (*priv->accountInfo_)->conversationModel->makePermanent(priv->conversation_->uid);
152
        // Accept call
153
        (*priv->accountInfo_)->callModel->accept(priv->conversation_->callId);
154 155 156
    } catch (const std::out_of_range&) {
        // ContactModel::getContact() exception
    }
157 158
}

159 160 161 162 163 164 165 166
static void
on_leave_action(IncomingCallView *view)
{
    g_return_if_fail(IS_INCOMING_CALL_VIEW(view));
    auto priv = INCOMING_CALL_VIEW_GET_PRIVATE(view);
    (*priv->accountInfo_)->conversationModel->selectConversation(priv->conversation_->uid);
}

167 168 169 170
static void
incoming_call_view_init(IncomingCallView *view)
{
    gtk_widget_init_template(GTK_WIDGET(view));
171

172 173 174 175 176 177 178 179 180
    auto provider = gtk_css_provider_new();
    gtk_css_provider_load_from_data(provider,
        ".flat-button { border: 0; border-radius: 50%; transition: all 0.3s ease; } \
        .red-button { background: #dc3a37; } \
        .green-button { background: #27ae60; } \
        .red-button:hover { background: #dc2719; } \
        .green-button:hover { background: #219d55; }",
        -1, nullptr
    );
181

182 183 184 185
    gtk_style_context_add_provider_for_screen(gdk_display_get_default_screen(gdk_display_get_default()),
                                              GTK_STYLE_PROVIDER(provider),
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

186 187 188
    auto priv = INCOMING_CALL_VIEW_GET_PRIVATE(view);

    /* bind the chat orientation to the gsetting */
189
    priv->settings = g_settings_new_full(get_settings_schema(), NULL, NULL);
190 191 192 193 194
    g_settings_bind_with_mapping(priv->settings, "chat-pane-horizontal",
                                 priv->paned_call, "orientation",
                                 G_SETTINGS_BIND_GET,
                                 map_boolean_to_orientation,
                                 nullptr, nullptr, nullptr);
195

196 197
    g_signal_connect_swapped(priv->button_reject_incoming, "clicked", G_CALLBACK(reject_incoming_call), view);
    g_signal_connect_swapped(priv->button_accept_incoming, "clicked", G_CALLBACK(accept_incoming_call), view);
198 199 200 201 202
}

static void
incoming_call_view_class_init(IncomingCallViewClass *klass)
{
203
    G_OBJECT_CLASS(klass)->dispose = incoming_call_view_dispose;
204

205
    gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
206
                                                "/net/jami/JamiGnome/incomingcallview.ui");
207

208
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, paned_call);
209
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, image_incoming);
210
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, label_name);
211
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, label_bestId);
212
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, spinner_status);
213 214
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, label_status);
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, button_reject_incoming);
215
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, button_accept_incoming);
216
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, frame_chat);
217
    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), IncomingCallView, box_messaging_widget);
218 219
}

220
static void
221
update_state(IncomingCallView *view)
222
{
223
    g_return_if_fail(IS_INCOMING_CALL_VIEW(view));
224 225
    IncomingCallViewPrivate *priv = INCOMING_CALL_VIEW_GET_PRIVATE(view);

226 227
    // change state label
    auto callId = priv->conversation_->callId;
228 229
    if (!(*priv->accountInfo_)->callModel->hasCall(callId)) return;
    auto call = (*priv->accountInfo_)->callModel->getCall(callId);
230

231
    gtk_label_set_text(GTK_LABEL(priv->label_status), lrc::api::call::to_string(call.status).c_str());
232

233 234 235 236
    if (call.status == lrc::api::call::Status::INCOMING_RINGING)
        gtk_widget_show(priv->button_accept_incoming);
    else
        gtk_widget_hide(priv->button_accept_incoming);
237

238 239 240 241 242 243 244 245
    if (call.status != lrc::api::call::Status::PEER_BUSY &&
             call.status != lrc::api::call::Status::ENDED) {
        gtk_widget_hide(priv->messaging_widget);
        gtk_widget_show(priv->label_status);
        gtk_widget_show(priv->spinner_status);
        gtk_widget_show(priv->button_reject_incoming);
        gtk_widget_show(priv->spinner_status);
    }
246 247
}

248
static void
249 250
update_name_and_photo(IncomingCallView *view)
{
251
    g_return_if_fail(IS_INCOMING_CALL_VIEW(view));
252
    auto priv = INCOMING_CALL_VIEW_GET_PRIVATE(view);
253

254 255
    QVariant var_i = GlobalInstances::pixmapManipulator().conversationPhoto(
        *priv->conversation_,
256
        **(priv->accountInfo_),
257 258 259
        QSize(110, 110),
        false
    );
260 261
    std::shared_ptr<GdkPixbuf> image = var_i.value<std::shared_ptr<GdkPixbuf>>();
    gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_incoming), image.get());
262

263
    try {
264
        auto contactInfo = (*priv->accountInfo_)->contactModel->getContact(priv->conversation_->participants.front());
265

266 267
        auto name = contactInfo.profileInfo.alias;
        gtk_label_set_text(GTK_LABEL(priv->label_name), name.c_str());
268

269 270 271 272 273 274 275
        auto bestId = contactInfo.registeredName;
        if (name != bestId) {
            gtk_label_set_text(GTK_LABEL(priv->label_bestId), bestId.c_str());
            gtk_widget_show(priv->label_bestId);
        }
    } catch (const std::out_of_range&) {
        // ContactModel::getContact() exception
276
    }
277 278 279
}

static void
280
set_call_info(IncomingCallView *view) {
281
    g_return_if_fail(IS_INCOMING_CALL_VIEW(view));
282 283
    IncomingCallViewPrivate *priv = INCOMING_CALL_VIEW_GET_PRIVATE(view);

284 285
    update_state(view);
    update_name_and_photo(view);
286

287
    // Update view if call state changes
288
    priv->state_change_connection = QObject::connect(
289
    &*(*priv->accountInfo_)->callModel,
290 291 292 293 294 295 296
    &lrc::api::NewCallModel::callStatusChanged,
    [view, priv] (const std::string& callId) {
        if (callId == priv->conversation_->callId) {
            update_state(view);
            update_name_and_photo(view);
        }
    });
297

298
    auto chat_view = chat_view_new(WEBKIT_CHAT_CONTAINER(priv->webkit_chat_container),
299
                                                         *priv->accountInfo_,
300 301
                                                         priv->conversation_,
                                                         *priv->avModel_);
302 303
    gtk_widget_show(chat_view);
    chat_view_set_header_visible(CHAT_VIEW(chat_view), FALSE);
304
    chat_view_set_record_visible(CHAT_VIEW(chat_view), FALSE);
305
    gtk_container_add(GTK_CONTAINER(priv->frame_chat), chat_view);
306
}
307 308

GtkWidget *
309
incoming_call_view_new(WebKitChatContainer* view,
310
                       lrc::api::AVModel& avModel,
311
                       AccountInfoPointer const & accountInfo,
312
                       lrc::api::conversation::Info* conversation)
313 314
{
    auto self = g_object_new(INCOMING_CALL_VIEW_TYPE, NULL);
aviau's avatar
aviau committed
315 316

    IncomingCallViewPrivate *priv = INCOMING_CALL_VIEW_GET_PRIVATE(self);
317 318
    priv->webkit_chat_container = GTK_WIDGET(view);
    priv->conversation_ = conversation;
319
    priv->accountInfo_ = &accountInfo;
320
    priv->avModel_ = &avModel;
aviau's avatar
aviau committed
321

322 323 324 325
    priv->messaging_widget = messaging_widget_new(avModel, conversation, accountInfo);
    gtk_box_pack_start(GTK_BOX(priv->box_messaging_widget), priv->messaging_widget, TRUE, TRUE, 0);
    g_signal_connect_swapped(priv->messaging_widget, "leave-action", G_CALLBACK(on_leave_action), self);

326
    set_call_info(INCOMING_CALL_VIEW(self));
327 328 329 330

    return GTK_WIDGET(self);
}

331 332
lrc::api::conversation::Info
incoming_call_view_get_conversation(IncomingCallView *self)
333
{
334
    g_return_val_if_fail(IS_INCOMING_CALL_VIEW(self), lrc::api::conversation::Info());
335 336
    auto priv = INCOMING_CALL_VIEW_GET_PRIVATE(self);

337
    return *priv->conversation_;
338
}
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

void
incoming_call_view_let_a_message(IncomingCallView* view, lrc::api::conversation::Info conv)
{
    g_return_if_fail(IS_INCOMING_CALL_VIEW(view));
    auto priv = INCOMING_CALL_VIEW_GET_PRIVATE(view);
    g_return_if_fail(priv->conversation_->uid == conv.uid);

    gtk_widget_hide(priv->label_status);
    gtk_widget_hide(priv->spinner_status);
    gtk_widget_hide(priv->button_accept_incoming);
    gtk_widget_hide(priv->button_reject_incoming);

    gtk_widget_show(priv->messaging_widget);
}