currentcallview.cpp 72.6 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *  Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
 *
 *  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 "currentcallview.h"

22 23 24
 // Client
#include "chatview.h"
#include "native/pixbufmanipulator.h"
25
#include "notifier.h"
26 27 28
#include "utils/drawing.h"
#include "utils/files.h"
#include "video/video_widget.h"
29 30

// Lrc
31
#include <api/avmodel.h>
32
#include <api/newaccountmodel.h>
33 34 35 36
#include <api/conversationmodel.h>
#include <api/contact.h>
#include <api/contactmodel.h>
#include <api/newcallmodel.h>
37
#include <api/newcodecmodel.h>
38
#include <globalinstances.h>
39
#include <smartinfohub.h>
40 41 42 43 44 45

// Gtk
#include <clutter-gtk/clutter-gtk.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>

46
#include <QSize>
47

48 49 50 51 52 53 54 55
#include <set>

enum class RowType {
    CONTACT,
    CALL,
    CONFERENCE,
    TITLE
};
56

57 58 59 60
namespace { namespace details
{
class CppImpl;
}}
61

62 63 64 65 66 67 68 69 70 71
struct _CurrentCallView
{
    GtkBox parent;
};

struct _CurrentCallViewClass
{
    GtkBoxClass parent_class;
};

72
struct CurrentCallViewPrivate
73
{
74
    GtkWidget *hbox_call_info;
75
    GtkWidget *hbox_call_status;
76
    GtkWidget *hbox_call_controls;
Olivier Gregoire's avatar
Olivier Gregoire committed
77
    GtkWidget *vbox_call_smartInfo;
78
    GtkWidget *vbox_peer_identity;
79
    GtkWidget *image_peer;
80
    GtkWidget *label_name;
81
    GtkWidget *label_bestId;
82 83
    GtkWidget *label_status;
    GtkWidget *label_duration;
Olivier Gregoire's avatar
Olivier Gregoire committed
84 85 86
    GtkWidget *label_smartinfo_description;
    GtkWidget *label_smartinfo_value;
    GtkWidget *label_smartinfo_general_information;
87
    GtkWidget *paned_call;
88 89
    GtkWidget *frame_video;
    GtkWidget *video_widget;
90
    GtkWidget *frame_chat;
91
    GtkWidget *togglebutton_chat;
92 93
    GtkWidget *togglebutton_muteaudio;
    GtkWidget *togglebutton_mutevideo;
94
    GtkWidget *togglebutton_add_participant;
95
    GtkWidget *togglebutton_transfer;
96 97 98 99 100 101
    GtkWidget *siptransfer_popover;
    GtkWidget *siptransfer_filter_entry;
    GtkWidget *list_conversations;
    GtkWidget *add_participant_popover;
    GtkWidget *conversation_filter_entry;
    GtkWidget *list_conversations_invite;
102
    GtkWidget *togglebutton_hold;
103
    GtkWidget *togglebutton_record;
104
    GtkWidget *button_hangup;
105 106
    GtkWidget *scalebutton_quality;
    GtkWidget *checkbutton_autoquality;
107
    GtkWidget *chat_view;
108
    GtkWidget *webkit_chat_container; // The webkit_chat_container is created once, then reused for all chat views
109 110

    GSettings *settings;
111

112
    details::CppImpl* cpp; ///< Non-UI and C++ only code
113 114 115 116 117 118
};

G_DEFINE_TYPE_WITH_PRIVATE(CurrentCallView, current_call_view, GTK_TYPE_BOX);

#define CURRENT_CALL_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CURRENT_CALL_VIEW_TYPE, CurrentCallViewPrivate))

119 120 121 122 123
enum {
    VIDEO_DOUBLE_CLICKED,
    LAST_SIGNAL
};

124
//==============================================================================
125

126
namespace { namespace details
127 128
{

129 130 131
static constexpr int CONTROLS_FADE_TIMEOUT = 3000000; /* microseconds */
static constexpr int FADE_DURATION = 500; /* miliseconds */
static guint current_call_view_signals[LAST_SIGNAL] = { 0 };
132

133
namespace // Helpers
134 135
{

136
static gboolean
137
map_boolean_to_orientation(GValue* value, GVariant* variant, G_GNUC_UNUSED gpointer user_data)
138 139 140 141 142 143 144 145 146 147 148 149 150 151
{
    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);
        }
        return TRUE;
    }
    return FALSE;
}

152
static ClutterTransition*
153 154 155 156 157 158 159 160 161 162 163
create_fade_out_transition()
{
    auto transition  = clutter_property_transition_new("opacity");
    clutter_transition_set_from(transition, G_TYPE_UINT, 255);
    clutter_transition_set_to(transition, G_TYPE_UINT, 0);
    clutter_timeline_set_duration(CLUTTER_TIMELINE(transition), FADE_DURATION);
    clutter_timeline_set_repeat_count(CLUTTER_TIMELINE(transition), 0);
    clutter_timeline_set_progress_mode(CLUTTER_TIMELINE(transition), CLUTTER_EASE_IN_OUT_CUBIC);
    return transition;
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static GtkBox *
gtk_scale_button_get_box(GtkScaleButton *button)
{
    GtkWidget *box = NULL;
    if (auto dock = gtk_scale_button_get_popup(button)) {
        // the dock is a popover which contains the box
        box = gtk_bin_get_child(GTK_BIN(dock));
        if (box) {
            if (GTK_IS_FRAME(box)) {
                // support older versions of gtk; the box used to be in a frame
                box = gtk_bin_get_child(GTK_BIN(box));
            }
        }
    }

    return GTK_BOX(box);
}

/**
 * This gets the GtkScaleButtonScale widget (which is a GtkScale) from the
 * given GtkScaleButton in order to be able to modify its properties and connect
 * to its signals
 */
static GtkScale *
188
gtk_scale_button_get_scale(GtkScaleButton* button)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
    GtkScale *scale = NULL;

    if (auto box = gtk_scale_button_get_box(button)) {
        GList *children = gtk_container_get_children(GTK_CONTAINER(box));
        for (GList *c = children; c && !scale; c = c->next) {
            if (GTK_IS_SCALE(c->data))
                scale = GTK_SCALE(c->data);
        }
        g_list_free(children);
    }

    return scale;
}

204 205 206
} // namespace

class CppImpl
207
{
208
public:
209
    explicit CppImpl(CurrentCallView& widget, const lrc::api::Lrc& lrc);
210
    ~CppImpl();
211

212 213
    void init();
    void setup(WebKitChatContainer* chat_widget,
214
               AccountInfoPointer const & account_info,
215 216
               lrc::api::conversation::Info* conversation,
               lrc::api::AVModel& avModel);
217
    void add_transfer_contact(const std::string& uri);
218 219 220
    void add_title(const std::string& title);
    void add_present_contact(const std::string& uri, const std::string& custom_data, RowType custom_type, const std::string& accountId);
    void add_conference(const std::vector<std::string>& uris, const std::string& custom_data, const std::string& accountId);
221

222 223
    void insertControls();
    void checkControlsFading();
224

225 226
    CurrentCallView* self = nullptr; // The GTK widget itself
    CurrentCallViewPrivate* widgets = nullptr;
227

228
    lrc::api::conversation::Info* conversation = nullptr;
229
    AccountInfoPointer const *accountInfo = nullptr;
230
    lrc::api::AVModel* avModel_;
231

232
    QMetaObject::Connection state_change_connection;
233
    QMetaObject::Connection update_vcard_connection;
234
    QMetaObject::Connection renderer_connection;
235
    QMetaObject::Connection smartinfo_refresh_connection;
236

237 238 239 240 241
    // for clutter animations and to know when to fade in/out the overlays
    ClutterTransition* fade_info = nullptr;
    ClutterTransition* fade_controls = nullptr;
    gint64 time_last_mouse_motion = 0;
    guint timer_fade = 0;
242

243 244 245 246 247 248 249
    /* flag used to keep track of the video quality scale pressed state;
     * we do not want to update the codec bitrate until the user releases the
     * scale button */
    gboolean quality_scale_pressed = FALSE;
    gulong insert_controls_id = 0;
    guint smartinfo_action = 0;

250 251 252 253
    const lrc::api::Lrc& lrc_;

    std::vector<std::string> titles_;
    std::set<std::string> hiddenTitles_;
254 255

    std::string currentCall_ {};
256 257 258 259 260 261 262 263 264 265 266
private:
    CppImpl() = delete;
    CppImpl(const CppImpl&) = delete;
    CppImpl& operator=(const CppImpl&) = delete;

    void setCallInfo();
    void updateDetails();
    void updateState();
    void updateNameAndPhoto();
    void updateSmartInfo();
};
267

268 269 270
inline namespace gtk_callbacks
{

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
static void
set_call_quality(CurrentCallView* view, bool auto_quality_on, double desired_quality)
{
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);

    auto videoCodecs = (*priv->cpp->accountInfo)->codecModel->getVideoCodecs();
    for (const auto& codec : videoCodecs) {
        if (auto_quality_on) {
            (*priv->cpp->accountInfo)->codecModel->autoQuality(codec.id, true);
        } else {
            (*priv->cpp->accountInfo)->codecModel->autoQuality(codec.id, false);
            double min_bitrate = 0., max_bitrate = 0., min_quality = 0., max_quality = 0.;
            try {
                min_bitrate = std::stoi(codec.min_bitrate);
                max_bitrate = std::stoi(codec.max_bitrate);
                min_quality = std::stoi(codec.min_quality);
                max_quality = std::stoi(codec.max_quality);
            } catch (...) {
                g_error("Cannot convert a codec value to an int, abort");
                break;
            }

            double bitrate = min_bitrate + (max_bitrate - min_bitrate)*(desired_quality/100.0);
            if (bitrate < 0) bitrate = 0;
            (*priv->cpp->accountInfo)->codecModel->bitrate(codec.id, bitrate);

            // note: a lower value means higher quality
            double quality = min_quality - (min_quality - max_quality)*(desired_quality/100.0);
            if (quality < 0) quality = 0;
            (*priv->cpp->accountInfo)->codecModel->quality(codec.id, quality);
        }
    }
}

305 306 307 308 309 310
static void
set_record_animation(CurrentCallViewPrivate* priv)
{
    auto callToRender = priv->cpp->conversation->callId;
    if (!priv->cpp->conversation->confId.empty())
        callToRender = priv->cpp->conversation->confId;
311 312 313 314 315 316
    bool nextStatus = (*priv->cpp->accountInfo)->callModel->isRecording(callToRender);
    bool currentStatus = (*priv->cpp->accountInfo)->callModel->isRecording(callToRender);
    if (nextStatus != currentStatus) {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->togglebutton_record),
            (*priv->cpp->accountInfo)->callModel->isRecording(callToRender));
    }
317 318
}

319 320
static void
on_togglebutton_chat_toggled(GtkToggleButton* widget, CurrentCallView* view)
321
{
322 323
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
324

325 326 327 328 329 330
    if (gtk_toggle_button_get_active(widget)) {
        gtk_widget_show_all(priv->frame_chat);
        gtk_widget_grab_focus(priv->frame_chat);
    } else {
        gtk_widget_hide(priv->frame_chat);
    }
331 332 333
}

static gboolean
334
on_timer_fade_timeout(CurrentCallView* view)
335
{
336 337 338 339 340
    g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), G_SOURCE_REMOVE);
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
    priv->cpp->checkControlsFading();
    return G_SOURCE_CONTINUE;
}
341

342 343 344 345 346
static void
on_size_allocate(CurrentCallView* view)
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
347

348
    priv->cpp->insertControls();
349 350
}

351
static void
352
on_button_hangup_clicked(CurrentCallView* view)
353
{
354 355
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
356
    (*priv->cpp->accountInfo)->callModel->hangUp(priv->cpp->conversation->callId);
357 358 359
}

static void
360
on_togglebutton_hold_clicked(CurrentCallView* view)
361
{
362 363 364 365 366 367
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);

    auto callToHold = priv->cpp->conversation->callId;
    if (!priv->cpp->conversation->confId.empty())
        callToHold = priv->cpp->conversation->confId;
368
    (*priv->cpp->accountInfo)->callModel->togglePause(callToHold);
369 370 371
}

static void
372
on_togglebutton_record_clicked(CurrentCallView* view)
373
{
374 375 376 377 378 379
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);

    auto callToRecord = priv->cpp->conversation->callId;
    if (!priv->cpp->conversation->confId.empty())
        callToRecord = priv->cpp->conversation->confId;
380
    (*priv->cpp->accountInfo)->callModel->toggleAudioRecord(callToRecord);
381 382

    set_record_animation(priv);
383 384 385
}

static void
386
on_togglebutton_muteaudio_clicked(CurrentCallView* view)
387 388
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
389 390 391 392 393 394
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);

    auto callToMute = priv->cpp->conversation->callId;
    if (!priv->cpp->conversation->confId.empty())
        callToMute = priv->cpp->conversation->confId;
    //auto muteAudioBtn = GTK_TOGGLE_BUTTON(priv->togglebutton_muteaudio);
395
    (*priv->cpp->accountInfo)->callModel->toggleMedia(callToMute,
396
        lrc::api::NewCallModel::Media::AUDIO);
397 398

    auto togglebutton = GTK_TOGGLE_BUTTON(priv->togglebutton_muteaudio);
399
    auto image = gtk_image_new_from_resource ("/net/jami/JamiGnome/mute_audio");
400
    if (gtk_toggle_button_get_active(togglebutton))
401
        image = gtk_image_new_from_resource ("/net/jami/JamiGnome/unmute_audio");
402 403 404 405
    gtk_button_set_image(GTK_BUTTON(togglebutton), image);
}

static void
406
on_togglebutton_mutevideo_clicked(CurrentCallView* view)
407 408
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
409 410 411 412 413 414
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);

    auto callToMute = priv->cpp->conversation->callId;
    if (!priv->cpp->conversation->confId.empty())
        callToMute = priv->cpp->conversation->confId;
    //auto muteVideoBtn = GTK_TOGGLE_BUTTON(priv->togglebutton_mutevideo);
415
    (*priv->cpp->accountInfo)->callModel->toggleMedia(callToMute,
416
        lrc::api::NewCallModel::Media::VIDEO);
417 418

    auto togglebutton = GTK_TOGGLE_BUTTON(priv->togglebutton_mutevideo);
419
    auto image = gtk_image_new_from_resource ("/net/jami/JamiGnome/mute_video");
420
    if (gtk_toggle_button_get_active(togglebutton))
421
        image = gtk_image_new_from_resource ("/net/jami/JamiGnome/unmute_video");
422 423 424
    gtk_button_set_image(GTK_BUTTON(togglebutton), image);
}

425 426
static gboolean
on_mouse_moved(CurrentCallView* view)
427
{
428
    g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);
429
    auto priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
430

431
    priv->cpp->time_last_mouse_motion = g_get_monotonic_time();
432

433 434 435 436 437 438 439 440 441 442 443
    // since the mouse moved, make sure the controls are shown
    if (clutter_timeline_get_direction(CLUTTER_TIMELINE(priv->cpp->fade_info)) == CLUTTER_TIMELINE_FORWARD) {
        clutter_timeline_set_direction(CLUTTER_TIMELINE(priv->cpp->fade_info), CLUTTER_TIMELINE_BACKWARD);
        clutter_timeline_set_direction(CLUTTER_TIMELINE(priv->cpp->fade_controls), CLUTTER_TIMELINE_BACKWARD);
        if (!clutter_timeline_is_playing(CLUTTER_TIMELINE(priv->cpp->fade_info))) {
            clutter_timeline_rewind(CLUTTER_TIMELINE(priv->cpp->fade_info));
            clutter_timeline_rewind(CLUTTER_TIMELINE(priv->cpp->fade_controls));
            clutter_timeline_start(CLUTTER_TIMELINE(priv->cpp->fade_info));
            clutter_timeline_start(CLUTTER_TIMELINE(priv->cpp->fade_controls));
        }
    }
444

Hugo Lefeuvre's avatar
Hugo Lefeuvre committed
445
    return FALSE; // propagate event
446
}
Olivier Gregoire's avatar
Olivier Gregoire committed
447

448 449 450 451 452
static void
on_autoquality_toggled(GtkToggleButton* button, CurrentCallView* view)
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
    auto priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
453

454
    gboolean auto_quality_on = gtk_toggle_button_get_active(button);
455

456 457 458
    auto scale = gtk_scale_button_get_scale(GTK_SCALE_BUTTON(priv->scalebutton_quality));
    auto plus_button = gtk_scale_button_get_plus_button(GTK_SCALE_BUTTON(priv->scalebutton_quality));
    auto minus_button = gtk_scale_button_get_minus_button(GTK_SCALE_BUTTON(priv->scalebutton_quality));
459

460 461 462
    gtk_widget_set_sensitive(GTK_WIDGET(scale), !auto_quality_on);
    gtk_widget_set_sensitive(plus_button, !auto_quality_on);
    gtk_widget_set_sensitive(minus_button, !auto_quality_on);
463

464
    double desired_quality = gtk_scale_button_get_value(GTK_SCALE_BUTTON(priv->scalebutton_quality));
465

466
    set_call_quality(view, auto_quality_on, desired_quality);
467
}
468

469 470 471 472 473 474
static void
on_quality_changed(G_GNUC_UNUSED GtkScaleButton *button, G_GNUC_UNUSED gdouble value,
                   CurrentCallView* view)
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
    auto priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
475

476
    /* no need to update quality if auto quality is enabled */
477
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbutton_autoquality))) return;
478

479
    /* update only if the scale button is released (reduces the number of updates) */
480 481
    if (priv->cpp->quality_scale_pressed) return;

482
    set_call_quality(view, false, gtk_scale_button_get_value(button));
483
}
484

485 486 487 488 489 490
static gboolean
on_quality_button_pressed(G_GNUC_UNUSED GtkWidget *widget, G_GNUC_UNUSED GdkEvent *event,
                          CurrentCallView* view)
{
    g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);
    auto priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
491

492
    priv->cpp->quality_scale_pressed = TRUE;
493

494
    return GDK_EVENT_PROPAGATE;
495 496
}

497 498 499
static gboolean
on_quality_button_released(G_GNUC_UNUSED GtkWidget *widget, G_GNUC_UNUSED GdkEvent *event,
                           CurrentCallView* view)
500
{
501 502
    g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);
    auto priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
503

504
    priv->cpp->quality_scale_pressed = FALSE;
505

506
    // make sure the quality gets updated
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    on_quality_changed(GTK_SCALE_BUTTON(priv->scalebutton_quality), 0, view);

    return GDK_EVENT_PROPAGATE;
}

static gboolean
on_video_widget_focus(GtkWidget* widget, GtkDirectionType direction, CurrentCallView* view)
{
    g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);
    auto priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);

    // if this widget already has focus, we want the focus to move to the next widget, otherwise we
    // will get stuck in a focus loop on the buttons
    if (gtk_widget_has_focus(widget))
        return FALSE;

    // otherwise we want the focus to go to and change between the call control buttons
    if (gtk_widget_child_focus(GTK_WIDGET(priv->hbox_call_controls), direction)) {
        // selected a child, make sure call controls are shown
        on_mouse_moved(view);
        return TRUE;
    }

Hugo Lefeuvre's avatar
Hugo Lefeuvre committed
530
    // did not select the next child, propagate the event
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    return FALSE;
}

static gboolean
on_button_press_in_video_event(GtkWidget* widget, GdkEventButton *event, CurrentCallView* view)
{
    g_return_val_if_fail(IS_VIDEO_WIDGET(widget), FALSE);
    g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);

    // on double click
    if (event->type == GDK_2BUTTON_PRESS) {
        g_signal_emit(G_OBJECT(view), current_call_view_signals[VIDEO_DOUBLE_CLICKED], 0);
    }

    return GDK_EVENT_PROPAGATE;
}

static void
on_toggle_smartinfo(GSimpleAction* action, G_GNUC_UNUSED GVariant* state, GtkWidget* vbox_call_smartInfo)
{
    if (g_variant_get_boolean(g_action_get_state(G_ACTION(action)))) {
        gtk_widget_show(vbox_call_smartInfo);
    } else {
        gtk_widget_hide(vbox_call_smartInfo);
    }
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
static void
transfer_to_peer(CurrentCallViewPrivate* priv, const std::string& peerUri)
{
    if (peerUri == priv->cpp->conversation->participants.front()) {
        g_warning("avoid to transfer to the same call, abort.");
#if GTK_CHECK_VERSION(3,22,0)
        gtk_popover_popdown(GTK_POPOVER(priv->siptransfer_popover));
#else
        gtk_widget_hide(GTK_WIDGET(priv->siptransfer_popover));
#endif
        return;
    }
    try {
        // If a call is already present with a peer, try an attended transfer.
        auto callInfo = (*priv->cpp->accountInfo)->callModel->getCallFromURI(peerUri, true);
        (*priv->cpp->accountInfo)->callModel->transferToCall(
            priv->cpp->conversation->callId, callInfo.id);
    } catch (std::out_of_range&) {
        // No current call found with this URI, perform a blind transfer
        (*priv->cpp->accountInfo)->callModel->transfer(
            priv->cpp->conversation->callId, peerUri);
    }
#if GTK_CHECK_VERSION(3,22,0)
    gtk_popover_popdown(GTK_POPOVER(priv->siptransfer_popover));
#else
    gtk_widget_hide(GTK_WIDGET(priv->siptransfer_popover));
#endif
}

static void
on_siptransfer_filter_activated(CurrentCallView* self)
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);

    transfer_to_peer(priv, gtk_entry_get_text(GTK_ENTRY(priv->siptransfer_filter_entry)));
}

static GtkLabel*
597
get_address_label(GtkListBoxRow* row)
598 599 600 601 602 603 604
{
    auto* row_children = gtk_container_get_children(GTK_CONTAINER(row));
    auto* box_infos = g_list_first(row_children)->data;
    auto* children = gtk_container_get_children(GTK_CONTAINER(box_infos));
    return GTK_LABEL(g_list_last(children)->data);
}

605 606 607 608 609 610 611 612 613
static GtkImage*
get_image(GtkListBoxRow* row)
{
    auto* row_children = gtk_container_get_children(GTK_CONTAINER(row));
    auto* box_infos = g_list_first(row_children)->data;
    auto* children = gtk_container_get_children(GTK_CONTAINER(box_infos));
    return GTK_IMAGE(g_list_first(children)->data);
}

614 615 616 617 618
static void
transfer_to_conversation(GtkListBox*, GtkListBoxRow* row, CurrentCallView* self)
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
619
    auto* sip_address = get_address_label(row);
620 621 622
    transfer_to_peer(priv, gtk_label_get_text(GTK_LABEL(sip_address)));
}

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
static void
on_search_participant(GtkSearchEntry* search_entry, CurrentCallView* self)
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);

    std::string search_text = gtk_entry_get_text(GTK_ENTRY(search_entry));
    std::transform(search_text.begin(), search_text.end(), search_text.begin(), ::tolower);

    auto row = 0, lastTitleRow = -1;
    auto hideTitle = true;
    while (GtkWidget* children = GTK_WIDGET(gtk_list_box_get_row_at_index(
            GTK_LIST_BOX(priv->list_conversations_invite), row))) {
        auto* addr_label = get_address_label(GTK_LIST_BOX_ROW(children));
        std::string content = gtk_label_get_text(addr_label);
        std::transform(content.begin(), content.end(), content.begin(), ::tolower);
        if (content.find(search_text) == std::string::npos) {
            bool hide = true;
            for (auto title: priv->cpp->titles_) {
                std::transform(title.begin(), title.end(), title.begin(), ::tolower);
                if (title == content) {
                    hide = false;
                    // Hide last title if needed
                    if (lastTitleRow != -1 && hideTitle) {
                        auto* lastTitle = GTK_WIDGET(gtk_list_box_get_row_at_index(GTK_LIST_BOX(priv->list_conversations_invite), lastTitleRow));
                        gtk_widget_hide(lastTitle);
                    }
                    lastTitleRow = row;
                    hideTitle = true;
                }
            }
            if (hide) gtk_widget_hide(children);
        } else {
            if (lastTitleRow != -1 && hideTitle) {
                auto* lastTitle = GTK_WIDGET(gtk_list_box_get_row_at_index(GTK_LIST_BOX(priv->list_conversations_invite), lastTitleRow));
                gtk_widget_show(lastTitle);
            }
            hideTitle = false;
            gtk_widget_show(children);
        }
        row++;
    }

    // Hide last title if needed
    if (lastTitleRow != -1 && hideTitle) {
        auto* lastTitle = GTK_WIDGET(gtk_list_box_get_row_at_index(GTK_LIST_BOX(priv->list_conversations_invite), lastTitleRow));
        gtk_widget_hide(lastTitle);
    }
}

static void
invite_to_conversation(GtkListBox*, GtkListBoxRow* row, CurrentCallView* self)
{
    auto priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);

    auto* label = get_address_label(GTK_LIST_BOX_ROW(row));
    std::string content = gtk_label_get_text(label);
    for (auto title: priv->cpp->titles_) {
        if (content != title) continue;
        bool isHiddenTitle = priv->cpp->hiddenTitles_.find(content) != priv->cpp->hiddenTitles_.end();
        auto* image = get_image(row);
        if (!isHiddenTitle) {
            priv->cpp->hiddenTitles_.insert(content);
            gtk_image_set_from_icon_name(image, "pan-up-symbolic", GTK_ICON_SIZE_MENU);
        } else {
            priv->cpp->hiddenTitles_.erase(content);
            gtk_image_set_from_icon_name(image, "pan-down-symbolic", GTK_ICON_SIZE_MENU);
        }
        auto changeState = false;
        auto rowIdx = 0;
        while (auto* children = gtk_list_box_get_row_at_index(GTK_LIST_BOX(priv->list_conversations_invite), rowIdx)) {
            rowIdx++;
            if (children == row) {
                changeState = true;
                continue;
            }
            if (!changeState) continue;
            auto* addr_label = get_address_label(GTK_LIST_BOX_ROW(children));
            std::string content2 = gtk_label_get_text(addr_label);
            for (auto title: priv->cpp->titles_) {
                if (content2 == title) return; // Other title, stop here.
            }
            if (!isHiddenTitle)
                gtk_widget_hide(GTK_WIDGET(children));
            else {
                gtk_widget_show(GTK_WIDGET(children));
                // refilter if needed
                std::string currentFilter = gtk_entry_get_text(GTK_ENTRY(priv->conversation_filter_entry));
                if (!currentFilter.empty())
                    on_search_participant(GTK_SEARCH_ENTRY(priv->conversation_filter_entry), self);
            }
        }
        return;
    }

    auto callToRender = priv->cpp->conversation->callId;
    if (!priv->cpp->conversation->confId.empty())
        callToRender = priv->cpp->conversation->confId;

    auto rowIdx = 0;
    while (auto* children = gtk_list_box_get_row_at_index(GTK_LIST_BOX(priv->list_conversations_invite), rowIdx)) {
        if (children == row) {
            auto* custom_type = g_object_get_data(G_OBJECT(label), "custom_type");
            std::string custom_data = (gchar*)g_object_get_data(G_OBJECT(label), "custom_data");
            if (GPOINTER_TO_INT(custom_type) == (int)RowType::CONTACT) {
                try {
                    const auto& call = (*priv->cpp->accountInfo)->callModel->getCall(callToRender);
                    (*priv->cpp->accountInfo)->callModel->callAndAddParticipant(custom_data, callToRender, call.isAudioOnly);
                } catch (...) {
                    g_warning("Can't add participant to inexistant call");
                }
            } else if (GPOINTER_TO_INT(custom_type)  == (int)RowType::CALL
                    || GPOINTER_TO_INT(custom_type)  == (int)RowType::CONFERENCE) {
                (*priv->cpp->accountInfo)->callModel->joinCalls(custom_data, callToRender);
            }
            break;
        }
        ++rowIdx;
    }


#if GTK_CHECK_VERSION(3,22,0)
    gtk_popover_popdown(GTK_POPOVER(priv->add_participant_popover));
#else
    gtk_widget_hide(GTK_WIDGET(priv->add_participant_popover));
#endif
}

751 752 753 754 755 756 757 758 759 760
static void
filter_transfer_list(CurrentCallView *self)
{
    g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);

    std::string currentFilter = gtk_entry_get_text(GTK_ENTRY(priv->siptransfer_filter_entry));

    auto row = 0;
    while (GtkWidget* children = GTK_WIDGET(gtk_list_box_get_row_at_index(GTK_LIST_BOX(priv->list_conversations), row))) {
761
        auto* sip_address = get_address_label(GTK_LIST_BOX_ROW(children));
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
        if (row == 0) {
            // Update searching item
            if (currentFilter.empty() || currentFilter == priv->cpp->conversation->participants.front()) {
                // Hide temporary item if filter is empty or same number
                gtk_widget_hide(children);
            } else {
                // Else, show the temporary item (and select it)
                gtk_label_set_text(GTK_LABEL(sip_address), currentFilter.c_str());
                gtk_widget_show_all(children);
                gtk_list_box_select_row(GTK_LIST_BOX(priv->list_conversations), GTK_LIST_BOX_ROW(children));
            }
        } else {
            // It's a contact
            std::string item_address = gtk_label_get_text(GTK_LABEL(sip_address));

            if (item_address == priv->cpp->conversation->participants.front())
                // if item is the current conversation, hide it
                gtk_widget_hide(children);
            else if (currentFilter.empty())
                // filter is empty, show all items
                gtk_widget_show_all(children);
            else if (item_address.find(currentFilter) == std::string::npos || item_address == currentFilter)
                // avoid duplicates and unwanted numbers
                gtk_widget_hide(children);
            else
                // Item is filtered
                gtk_widget_show_all(children);
        }
        ++row;
    }
}

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
static void
on_button_add_participant_clicked(CurrentCallView *self)
{
    // Show and init list
    g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
    gtk_popover_set_relative_to(GTK_POPOVER(priv->add_participant_popover), GTK_WIDGET(priv->togglebutton_add_participant));
#if GTK_CHECK_VERSION(3,22,0)
    gtk_popover_popdown(GTK_POPOVER(priv->add_participant_popover));
#else
    gtk_widget_show_all(GTK_WIDGET(priv->add_participant_popover));
#endif
    gtk_widget_show_all(priv->add_participant_popover);
    filter_transfer_list(self);
}

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
static void
on_button_transfer_clicked(CurrentCallView *self)
{
    // Show and init list
    g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
    auto* priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
    gtk_popover_set_relative_to(GTK_POPOVER(priv->siptransfer_popover), GTK_WIDGET(priv->togglebutton_transfer));
#if GTK_CHECK_VERSION(3,22,0)
    gtk_popover_popdown(GTK_POPOVER(priv->siptransfer_popover));
#else
    gtk_widget_show_all(GTK_WIDGET(priv->siptransfer_popover));
#endif
    gtk_widget_show_all(priv->siptransfer_popover);
    filter_transfer_list(self);
}

static void
on_siptransfer_text_changed(GtkSearchEntry*, CurrentCallView* self)
{
    filter_transfer_list(self);
}

832 833
} // namespace gtk_callbacks

834
CppImpl::CppImpl(CurrentCallView& widget, const lrc::api::Lrc& lrc)
835
    : self {&widget}
836
    , lrc_ {lrc}
837 838 839 840 841 842
    , widgets {CURRENT_CALL_VIEW_GET_PRIVATE(&widget)}
{}

CppImpl::~CppImpl()
{
    QObject::disconnect(state_change_connection);
843
    QObject::disconnect(update_vcard_connection);
844
    QObject::disconnect(renderer_connection);
845 846 847
    QObject::disconnect(smartinfo_refresh_connection);
    g_clear_object(&widgets->settings);

848
    if (timer_fade) g_source_remove(timer_fade);
849 850 851 852 853 854 855 856 857 858 859 860

    auto* display_smartinfo = g_action_map_lookup_action(G_ACTION_MAP(g_application_get_default()),
                                                        "display-smartinfo");
    g_signal_handler_disconnect(display_smartinfo, smartinfo_action);
}

void
CppImpl::init()
{
    // CSS styles
    auto provider = gtk_css_provider_new();
    gtk_css_provider_load_from_data(provider,
861 862
        ".search-entry-style { border: 0; border-radius: 0; } \
        .smartinfo-block-style { color: #8ae234; background-color: rgba(1, 1, 1, 0.33); } \
863 864
        @keyframes blink { 0% {opacity: 1;} 49% {opacity: 1;} 50% {opacity: 0;} 100% {opacity: 0;} } \
        .record-button { background: rgba(0, 0, 0, 1); border-radius: 50%; border: 0; transition: all 0.3s ease; } \
865
        .record-button:checked { animation: blink 1s; animation-iteration-count: infinite; } \
866 867
        .call-button { background: rgba(0, 0, 0, 0.35); border-radius: 50%; border: 0; transition: all 0.3s ease; } \
        .call-button:hover { background: rgba(0, 0, 0, 0.2); } \
868 869 870 871
        .call-button:disabled { opacity: 0.2; } \
        .can-be-disabled:checked { background: rgba(219, 58, 55, 1); } \
        .hangup-button-style { background: rgba(219, 58, 55, 1); border-radius: 50%; border: 0; transition: all 0.3s ease; } \
        .hangup-button-style:hover { background: rgba(219, 39, 25, 1); }",
872 873 874 875 876 877
        -1, nullptr
    );
    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);

878 879 880
    widgets->video_widget = video_widget_new();
    gtk_container_add(GTK_CONTAINER(widgets->frame_video), widgets->video_widget);
    gtk_widget_show_all(widgets->frame_video);
881

882 883
    // add the overlay controls only once the view has been allocated a size to prevent size
    // allocation warnings in the log
884
    insert_controls_id = g_signal_connect(self, "size-allocate", G_CALLBACK(on_size_allocate), nullptr);
885 886
}

887 888
void
CppImpl::setup(WebKitChatContainer* chat_widget,
889
               AccountInfoPointer const & account_info,
890 891
               lrc::api::conversation::Info* conv_info,
               lrc::api::AVModel& avModel)
892
{
893 894
    widgets->webkit_chat_container = GTK_WIDGET(chat_widget);
    conversation = conv_info;
895
    accountInfo = &account_info;
896
    avModel_ = &avModel;
897
    setCallInfo();
898

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
    gtk_widget_hide(widgets->togglebutton_transfer);

    auto callToRender = conversation->callId;
    if (!conversation->confId.empty())
        callToRender = conversation->confId;

    std::vector<std::string> callsId;
    std::vector<std::string> uris;
    bool first = true;
    for (const auto& c : lrc_.getConferences()) {
        // Get subcalls
        auto cid = lrc_.getConferenceSubcalls(c);
        callsId.insert(callsId.end(), cid.begin(), cid.end());
        // Get participants
        std::string uri, accountId;
        std::vector<std::string> curis;
        for (const auto& callId: cid) {
916 917 918
            for (const auto &account_id : lrc_.getAccountModel().getAccountList()) {
                try {
                    auto &accountInfo = lrc_.getAccountModel().getAccountInfo(account_id);
919 920
                    if (accountInfo.callModel->hasCall(callId)) {
                        const auto& call = accountInfo.callModel->getCall(callId);
921 922 923
                        uri = call.peerUri.find("ring:") == std::string::npos ?
                            call.peerUri : call.peerUri.substr(std::string("ring:").length());
                        uris.emplace_back(uri);
924 925
                        curis.emplace_back(uri);
                        accountId = account_id;
926 927 928 929
                        break;
                    }
                } catch (...) {}
            }
930
        }
931

932 933
        if (c == callToRender) {
            continue;
934 935
        }

936 937 938 939 940 941 942 943 944 945 946
        if (first) {
            add_title(_("Current conference (all accounts)"));
            first = false;
        }
        if (!uri.empty()) add_conference(curis, c, accountId);
    }

    first = true;
    for (const auto& c : lrc_.getCalls()) {
        std::string uri, accountId;
        for (const auto &account_id : lrc_.getAccountModel().getAccountList()) {
947
            try {
948 949 950 951 952 953 954 955
                auto &accountInfo = lrc_.getAccountModel().getAccountInfo(account_id);
                if (accountInfo.callModel->hasCall(c)) {
                    const auto& call = accountInfo.callModel->getCall(c);
                    if (call.status != lrc::api::call::Status::PAUSED
                        && call.status != lrc::api::call::Status::IN_PROGRESS) {
                        // Ignore non active calls
                        callsId.emplace_back(call.id);
                        continue;
956
                    }
957 958 959 960 961
                    uri = call.peerUri.find("ring:") == std::string::npos ?
                        call.peerUri : call.peerUri.substr(std::string("ring:").length());
                    accountId = account_id;
                    uris.emplace_back(uri);
                    break;
962 963 964
                }
            } catch (...) {}
        }
965 966 967 968
        auto isPresent = std::find(callsId.cbegin(), callsId.cend(), c) != callsId.cend();
        if (c == callToRender || isPresent) {
            continue;
        }
969

970 971 972 973 974 975
        if (first) {
            add_title(_("Current calls (all accounts)"));
            first = false;
        }
        if (!uri.empty()) add_present_contact(uri, c, RowType::CALL, accountId);
    }
976

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
    first = true;
    for (const auto& c : (*accountInfo)->conversationModel->getFilteredConversations((*accountInfo)->profileInfo.type)) {
        try {
            auto participant = c.participants.front();
            auto contactInfo = (*accountInfo)->contactModel->getContact(participant);
            auto isPresent = std::find(uris.cbegin(), uris.cend(), participant) != uris.cend();
            if (!isPresent
                && ((*accountInfo)->profileInfo.type == lrc::api::profile::Type::SIP || contactInfo.isPresent)) {
                if (first) {
                    add_title(_("Online contacts"));
                    first = false;
                }
                add_present_contact(participant, participant, RowType::CONTACT, (*accountInfo)->id);
            }
        } catch (...) {}
    }


    g_signal_connect(widgets->conversation_filter_entry, "search-changed", G_CALLBACK(on_search_participant), self);

    if ((*accountInfo)->profileInfo.type == lrc::api::profile::Type::SIP) {
998 999 1000 1001 1002 1003 1004 1005 1006
        // Remove previous list
        while (GtkWidget* children = GTK_WIDGET(gtk_list_box_get_row_at_index(GTK_LIST_BOX(widgets->list_conversations), 10)))
            gtk_container_remove(GTK_CONTAINER(widgets->list_conversations), children);
        // Fill with SIP contacts
        add_transfer_contact("");  // Temporary item
        for (const auto& c : (*accountInfo)->conversationModel->getFilteredConversations(lrc::api::profile::Type::SIP))
            add_transfer_contact(c.participants.front());
        gtk_widget_show_all(widgets->list_conversations);
        gtk_widget_show(widgets->togglebutton_transfer);
1007 1008 1009
        gtk_widget_show(widgets->togglebutton_hold);
    } else {
        gtk_widget_hide(widgets->togglebutton_hold);
1010
    }
1011 1012

    set_record_animation(widgets);
1013 1014
}

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
void
CppImpl::add_title(const std::string& title) {
    auto* box_item = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    auto* avatar = gtk_image_new_from_icon_name("pan-down-symbolic", GTK_ICON_SIZE_MENU);
    auto* info = gtk_label_new(nullptr);
    gtk_label_set_markup(GTK_LABEL(info), title.c_str());
    gtk_widget_set_halign(info, GTK_ALIGN_CENTER);
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(avatar));
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(info));
    g_object_set(G_OBJECT(info), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
    gtk_list_box_insert(GTK_LIST_BOX(widgets->list_conversations_invite), GTK_WIDGET(box_item), -1);

    titles_.emplace_back(title);
}

void
CppImpl::add_present_contact(const std::string& uri, const std::string& custom_data, RowType custom_type, const std::string& accountId)
{
1033
    std::string bestName = uri, bestUri = uri;
1034
    auto default_avatar = Interfaces::PixbufManipulator().generateAvatar("", "");
1035
    auto default_scaled = Interfaces::PixbufManipulator().scaleAndFrame(default_avatar.get(), QSize(48, 48), true, IconStatus::PRESENT);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    auto photo = default_scaled;

    try {
        auto &accInfo = lrc_.getAccountModel().getAccountInfo(accountId);
        auto contactInfo = accInfo.contactModel->getContact(uri);
        auto photostr = contactInfo.profileInfo.avatar;
        auto alias = contactInfo.profileInfo.alias;

        if (!alias.empty()) {
            bestName = alias;
1046 1047 1048 1049
        }

        if (!contactInfo.registeredName.empty()) {
            bestUri = contactInfo.registeredName;
1050 1051 1052 1053 1054
        }

        if (!photostr.empty()) {
            QByteArray byteArray(photostr.c_str(), photostr.length());
            QVariant avatar = Interfaces::PixbufManipulator().personPhoto(byteArray);
1055
            auto pixbuf_photo = Interfaces::PixbufManipulator().scaleAndFrame(avatar.value<std::shared_ptr<GdkPixbuf>>().get(), QSize(48, 48), true, IconStatus::PRESENT);
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
            if (avatar.isValid()) {
                photo = pixbuf_photo;
            }
        } else {
            auto name = alias.empty()? contactInfo.registeredName : alias;
            auto firstLetter = (name == contactInfo.profileInfo.uri || name.empty()) ?
            "" : QString(QString(name.c_str()).at(0)).toStdString();  // NOTE best way to be compatible with UTF-8
            auto fullUri = contactInfo.profileInfo.uri;
            if (accInfo.profileInfo.type != lrc::api::profile::Type::SIP)
                fullUri = "ring:" + fullUri;
            else
                fullUri = "sip:" + fullUri;
            photo = Interfaces::PixbufManipulator().generateAvatar(firstLetter, fullUri);
1069
            photo = Interfaces::PixbufManipulator().scaleAndFrame(photo.get(), QSize(48, 48), true, IconStatus::PRESENT);
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
        }
    } catch (const std::out_of_range&) {
        // ContactModel::getContact() exception
    }

    gchar* text = nullptr;
    if (uri != bestName) {
        bestName.erase(std::remove(bestName.begin(), bestName.end(), '\r'), bestName.end());
        bestName.erase(std::remove(bestName.begin(), bestName.end(), '\n'), bestName.end());
        text = g_markup_printf_escaped(
            "<span font_weight=\"bold\">%s</span>\n<span size=\"smaller\" color=\"#666\">%s</span>",
            bestName.c_str(),
1082
            bestUri.c_str()
1083 1084 1085 1086
        );
    } else {
        text = g_markup_printf_escaped(
            "<span font=\"10\">%s</span>",
1087
            bestUri.c_str()
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
        );
    }

    auto* box_item = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    auto* avatar = gtk_image_new_from_pixbuf(photo.get());
    auto* info = gtk_label_new(nullptr);
    gtk_label_set_markup(GTK_LABEL(info), text);
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(avatar));
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(info));
    g_object_set(G_OBJECT(info), "ellipsize", PANGO_ELLIPSIZE_END, NULL);

    g_object_set_data(G_OBJECT(info), "custom_type", GINT_TO_POINTER(custom_type));
    g_object_set_data(G_OBJECT(info), "custom_data", (void*)g_strdup(custom_data.c_str()));

    gtk_list_box_insert(GTK_LIST_BOX(widgets->list_conversations_invite), GTK_WIDGET(box_item), -1);
}

void
CppImpl::add_conference(const std::vector<std::string>& uris, const std::string& custom_data, const std::string& accountId)
{
    GError *error = nullptr;
    auto default_avatar = std::shared_ptr<GdkPixbuf>(
        gdk_pixbuf_new_from_resource_at_scale("/net/jami/JamiGnome/contacts_list", 50, 50, true, &error),
        g_object_unref
    );
    if (default_avatar == nullptr) {
        g_debug("Could not load icon: %s", error->message);
        g_clear_error(&error);
        return;
    }
    auto default_scaled = Interfaces::PixbufManipulator().scaleAndFrame(default_avatar.get(), QSize(50, 50));
    auto photo = default_scaled;

    std::string label;
    auto idx = 0;

    for (const auto& uri: uris) {
        try {
            auto bestName = uri;
            auto &accInfo = lrc_.getAccountModel().getAccountInfo(accountId);
            auto contactInfo = accInfo.contactModel->getContact(uri);
            auto alias = contactInfo.profileInfo.alias;

            if (!alias.empty()) {
                bestName = alias;
            } else if (!contactInfo.registeredName.empty()) {
                bestName = contactInfo.registeredName;
            }
            bestName.erase(std::remove(bestName.begin(), bestName.end(), '\r'), bestName.end());
            bestName.erase(std::remove(bestName.begin(), bestName.end(), '\n'), bestName.end());
            label += bestName;
            if (idx != static_cast<int>(uris.size()) - 1)
                label += ", ";
            idx ++;
        } catch (const std::out_of_range&) {
            // ContactModel::getContact() exception
        }
    }


    gchar* text = g_markup_printf_escaped(
        "<span font=\"10\">%s</span>",
        label.c_str()
    );

    auto* box_item = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    auto* avatar = gtk_image_new_from_pixbuf(photo.get());
    auto* info = gtk_label_new(nullptr);
    gtk_label_set_markup(GTK_LABEL(info), text);
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(avatar));
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(info));
    g_object_set(G_OBJECT(info), "ellipsize", PANGO_ELLIPSIZE_END, NULL);

    g_object_set_data(G_OBJECT(info), "custom_type", GINT_TO_POINTER(RowType::CONFERENCE));
    g_object_set_data(G_OBJECT(info), "custom_data", (void*)g_strdup(custom_data.c_str()));

    gtk_list_box_insert(GTK_LIST_BOX(widgets->list_conversations_invite), GTK_WIDGET(box_item), -1);
}

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
void
CppImpl::add_transfer_contact(const std::string& uri)
{
    auto* box_item = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    auto pixbufmanipulator = Interfaces::PixbufManipulator();
    auto image_buf = pixbufmanipulator.generateAvatar("", uri.empty() ? uri : "sip" + uri);
    auto scaled = pixbufmanipulator.scaleAndFrame(image_buf.get(), QSize(48, 48));
    auto* avatar = gtk_image_new_from_pixbuf(scaled.get());
    auto* address = gtk_label_new(uri.c_str());
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(avatar));
    gtk_container_add(GTK_CONTAINER(box_item), GTK_WIDGET(address));
    gtk_list_box_insert(GTK_LIST_BOX(widgets->list_conversations), GTK_WIDGET(box_item), -1);
1179
}
1180

1181 1182 1183 1184 1185 1186
void
CppImpl::setCallInfo()
{
    // change some things depending on call state
    updateState();
    updateDetails();
1187
    updateNameAndPhoto();
1188

1189 1190
    g_signal_connect(widgets->video_widget, "button-press-event",
                     G_CALLBACK(video_widget_on_button_press_in_screen_event), nullptr);
1191

1192 1193 1194 1195 1196
    // check if we already have a renderer
    auto callToRender = conversation->callId;
    if (!conversation->confId.empty())
        callToRender = conversation->confId;

1197
    (*accountInfo)->callModel->setCurrentCall(callToRender);
1198
    currentCall_ = callToRender;
1199 1200 1201 1202 1203
    try {
        // local renderer
        const lrc::api::video::Renderer* previewRenderer =
             &avModel_->getRenderer(
             lrc::api::video::PREVIEW_RENDERER_ID);
1204 1205 1206 1207 1208 1209 1210
        if (previewRenderer->isRendering()) {
            video_widget_add_new_renderer(VIDEO_WIDGET(widgets->video_widget),
                avModel_, previewRenderer, VIDEO_RENDERER_LOCAL);
        }
        try {
            auto call = (*accountInfo)->callModel->getCall(callToRender);
            video_widget_set_preview_visible(VIDEO_WIDGET(widgets->video_widget),
1211
                (call.status != lrc::api::call::Status::PAUSED) && conversation->confId.empty());
1212 1213 1214
        } catch (...) {
            g_warning("Can't change preview visibility for non existant call");
        }
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226

        const lrc::api::video::Renderer* vRenderer =
            &avModel_->getRenderer(
            callToRender);

        video_widget_add_new_renderer(VIDEO_WIDGET(widgets->video_widget),
            avModel_, vRenderer, VIDEO_RENDERER_REMOTE);
    } catch (...) {
        // The renderer doesn't exist for now. Ignore
    }


1227 1228

    // callback for local renderer
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
    renderer_connection = QObject::connect(
        &*avModel_,
        &lrc::api::AVModel::rendererStarted,
        [=](const std::string& id) {
            if (id == lrc::api::video::PREVIEW_RENDERER_ID) {
                try {
                    // local renderer
                    const lrc::api::video::Renderer* previewRenderer =
                        &avModel_->getRenderer(lrc::api::video::PREVIEW_RENDERER_ID);
                    if (previewRenderer->isRendering()) {
                        video_widget_add_new_renderer(VIDEO_WIDGET(widgets->video_widget),
                            avModel_, previewRenderer, VIDEO_RENDERER_LOCAL);
1241 1242 1243
                        auto call = (*accountInfo)->callModel->getCall(callToRender);
                        video_widget_set_preview_visible(VIDEO_WIDGET(widgets->video_widget),
                            (call.status != lrc::api::call::Status::PAUSED));
1244 1245 1246 1247
                    }
                } catch (...) {
                    g_warning("Preview renderer is not accessible! This should not happen");
                }
1248
            } else if (id == callToRender) {
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
                try {
                    const lrc::api::video::Renderer* vRenderer =
                        &avModel_->getRenderer(
                        callToRender);
                    video_widget_add_new_renderer(VIDEO_WIDGET(widgets->video_widget),
                        avModel_, vRenderer, VIDEO_RENDERER_REMOTE);
                } catch (...) {
                    g_warning("Remote renderer is not accessible! This should not happen");
                }
            }
        });
1260 1261 1262 1263 1264 1265 1266 1267

    smartinfo_refresh_connection = QObject::connect(
        &SmartInfoHub::instance(),
        &SmartInfoHub::changed,
        [this] { updateSmartInfo(); }
    );

    state_change_connection = QObject::connect(
1268
        &*(*accountInfo)->callModel,
1269 1270 1271
        &lrc::api::NewCallModel::callStatusChanged,
        [this] (const std::string& callId) {
            if (callId == conversation->callId) {
1272 1273 1274 1275 1276 1277 1278
                try {
                    auto call = (*accountInfo)->callModel->getCall(callId);
                    video_widget_set_preview_visible(VIDEO_WIDGET(widgets->video_widget),
                        (call.status != lrc::api::call::Status::PAUSED));
                } catch (...) {
                    g_warning("Can't set preview visible for inexistant call");
                }
1279
                updateNameAndPhoto();
1280
                updateState();
1281 1282 1283
            }
        });

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    update_vcard_connection = QObject::connect(
        &*(*accountInfo)->contactModel,
        &lrc::api::ContactModel::contactAdded,
        [this] (const std::string& uri) {
            if (uri == conversation->participants.front()) {
                updateNameAndPhoto();
            }
        });



1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
    // catch double click to make full screen
    g_signal_connect(widgets->video_widget, "button-press-event",
                     G_CALLBACK(on_button_press_in_video_event), self);

    // handle smartinfo in right click menu
    auto display_smartinfo = g_action_map_lookup_action(G_ACTION_MAP(g_application_get_default()),
                                                        "display-smartinfo");
    smartinfo_action = g_signal_connect(display_smartinfo,
                                        "notify::state",
                                        G_CALLBACK(on_toggle_smartinfo),
                                        widgets->vbox_call_smartInfo);

    // init chat view
    widgets->chat_view = chat_view_new(WEBKIT_CHAT_CONTAINER(widgets->webkit_chat_container),
1309
                                       *accountInfo, conversation, *avModel_);
1310 1311 1312
    gtk_container_add(GTK_CONTAINER(widgets->frame_chat), widgets->chat_view);

    chat_view_set_header_visible(CHAT_VIEW(widgets->chat_view), FALSE);
1313
    chat_view_set_record_visible(CHAT_VIEW(widgets->chat_view), FALSE);
1314 1315
}

1316 1317
void
CppImpl::insertControls()
1318
{
1319 1320 1321 1322 1323 1324 1325 1326 1327
    /* only add the controls once */
    g_signal_handler_disconnect(self, insert_controls_id);
    insert_controls_id = 0;

    auto stage = gtk_clutter_embed_get_stage(GTK_CLUTTER_EMBED(widgets->video_widget));
    auto actor_info = gtk_clutter_actor_new_with_contents(widgets->hbox_call_info);
    auto actor_controls = gtk_clutter_actor_new_with_contents(widgets->hbox_call_controls);
    auto actor_smartInfo = gtk_clutter_actor_new_with_contents(widgets->vbox_call_smartInfo);

1328 1329 1330 1331 1332 1333 1334 1335
    auto audioOnly = false;
    auto callId = conversation->callId;
    try {
        auto call = (*accountInfo)->callModel->getCall(callId);
        audioOnly = call.isAudioOnly;
    } catch (std::out_of_range& e) {
    }

1336 1337
    clutter_actor_add_child(stage, actor_info);
    clutter_actor_set_x_align(actor_info, CLUTTER_ACTOR_ALIGN_FILL);
1338 1339 1340 1341 1342 1343 1344 1345 1346
    if (!audioOnly) {
        clutter_actor_set_y_align(actor_info, CLUTTER_ACTOR_ALIGN_START);
    } else {
        clutter_actor_set_y_align(actor_info, CLUTTER_ACTOR_ALIGN_CENTER);
        gtk_orientable_set_orientation(GTK_ORIENTABLE(widgets->hbox_call_info), GTK_ORIENTATION_VERTICAL);
        gtk_widget_set_halign(widgets->vbox_peer_identity, GTK_ALIGN_CENTER);
        gtk_widget_set_halign(widgets->hbox_call_status, GTK_ALIGN_CENTER);
        gtk_widget_set_halign(widgets->label_bestId, GTK_ALIGN_CENTER);
    }
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

    clutter_actor_add_child(stage, actor_controls);
    clutter_actor_set_x_align(actor_controls, CLUTTER_ACTOR_ALIGN_CENTER);
    clutter_actor_set_y_align(actor_controls, CLUTTER_ACTOR_ALIGN_END);

    clutter_actor_add_child(stage, actor_smartInfo);
    clutter_actor_set_x_align(actor_smartInfo, CLUTTER_ACTOR_ALIGN_END);
    clutter_actor_set_y_align(actor_smartInfo, CLUTTER_ACTOR_ALIGN_START);
    ClutterMargin clutter_margin_smartInfo;
    clutter_margin_smartInfo.top = 50;
    clutter_margin_smartInfo.right = 10;
    clutter_margin_smartInfo.left = 10;
    clutter_margin_smartInfo.bottom = 10;
    clutter_actor_set_margin (actor_smartInfo, &clutter_margin_smartInfo);

    /* add fade in and out states to the info and controls */
    time_last_mouse_motion = g_get_monotonic_time();
    fade_info = create_fade_out_transition();
    fade_controls = create_fade_out_transition();
    clutter_actor_add_transition(actor_info, "fade_info", fade_info);
    clutter_actor_add_transition(actor_controls, "fade_controls", fade_controls);
    clutter_timeline_set_direction(CLUTTER_TIMELINE(fade_info), CLUTTER_TIMELINE_BACKWARD);
    clutter_timeline_set_direction(CLUTTER_TIMELINE(fade_controls), CLUTTER_TIMELINE_BACKWARD);
    clutter_timeline_stop(CLUTTER_TIMELINE(fade_info));
    clutter_timeline_stop(CLUTTER_TIMELINE(fade_controls));

    /* have a timer check every 1 second if the controls should fade out */
    timer_fade = g_timeout_add(1000, (GSourceFunc)on_timer_fade_timeout, self);

    /* connect the controllers (new model) */
    g_signal_connect_swapped(widgets->button_hangup, "clicked", G_CALLBACK(on_button_hangup_clicked), self);
1378
    g_signal_connect_swapped(widgets->togglebutton_add_participant, "clicked", G_CALLBACK(on_button_add_participant_clicked), self);
1379 1380 1381 1382
    g_signal_connect_swapped(widgets->togglebutton_transfer, "clicked", G_CALLBACK(on_button_transfer_clicked), self);
    g_signal_connect_swapped(widgets->siptransfer_filter_entry, "activate", G_CALLBACK(on_siptransfer_filter_activated), self);
    g_signal_connect(widgets->siptransfer_filter_entry, "search-changed", G_CALLBACK(on_siptransfer_text_changed), self);
    g_signal_connect(widgets->list_conversations, "row-activated", G_CALLBACK(transfer_to_conversation), self);
1383
    g_signal_connect(widgets->list_conversations_invite, "row-activated", G_CALLBACK(invite_to_conversation), self);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
    g_signal_connect_swapped(widgets->togglebutton_hold, "clicked", G_CALLBACK(on_togglebutton_hold_clicked), self);
    g_signal_connect_swapped(widgets->togglebutton_muteaudio, "clicked", G_CALLBACK(on_togglebutton_muteaudio_clicked), self);
    g_signal_connect_swapped(widgets->togglebutton_record, "clicked", G_CALLBACK(on_togglebutton_record_clicked), self);
    g_signal_connect_swapped(widgets->togglebutton_mutevideo, "clicked", G_CALLBACK(on_togglebutton_mutevideo_clicked), self);

    /* connect to the mouse motion event to reset the last moved time */
    g_signal_connect_swapped(widgets->video_widget, "motion-notify-event", G_CALLBACK(on_mouse_moved), self);
    g_signal_connect_swapped(widgets->video_widget, "button-press-event", G_CALLBACK(on_mouse_moved), self);
    g_signal_connect_swapped(widgets->video_widget, "button-release-event", G_CALLBACK(on_mouse_moved), self);

    /* manually handle the focus of the video widget to be able to focus on the call controls */
    g_signal_connect(widgets->video_widget, "focus", G_CALLBACK(on_video_widget_focus), self);


    /* toggle whether or not the chat is displayed */
    g_signal_connect(widgets->togglebutton_chat, "toggled", G_CALLBACK(on_togglebutton_chat_toggled), self);

    /* bind the chat orientation to the gsetting */
1402
    widgets->settings = g_settings_new_full(get_settings_schema(), nullptr, nullptr);
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
    g_settings_bind_with_mapping(widgets->settings, "chat-pane-horizontal",
                                 widgets->paned_call, "orientation",
                                 G_SETTINGS_BIND_GET,
                                 map_boolean_to_orientation,
                                 nullptr, nullptr, nullptr);

    g_signal_connect(widgets->scalebutton_quality, "value-changed", G_CALLBACK(on_quality_changed), self);

    /* customize the quality button scale */
    if (auto scale_box = gtk_scale_button_get_box(GTK_SCALE_BUTTON(widgets->scalebutton_quality))) {
        widgets->checkbutton_autoquality = gtk_check_button_new_with_label(C_("Enable automatic video quality",
                                                                              "Auto"));
        gtk_widget_show(widgets->checkbutton_autoquality);
        gtk_box_pack_start(GTK_BOX(scale_box), widgets->checkbutton_autoquality, FALSE, TRUE, 0);
        g_signal_connect(widgets->checkbutton_autoquality, "toggled", G_CALLBACK(on_autoquality_toggled), self);
    }
    if (auto scale = gtk_scale_button_get_scale(GTK_SCALE_BUTTON(widgets->scalebutton_quality))) {
        g_signal_connect(scale, "button-press-event", G_CALLBACK(on_quality_button_pressed), self);
        g_signal_connect(scale, "button-release-event", G_CALLBACK(on_quality_button_released), self);
    }

1424 1425 1426
    g_signal_connect(widgets->video_widget, "drag-data-received",
                     G_CALLBACK(video_widget_on_drag_data_received), nullptr);

1427 1428 1429 1430 1431 1432 1433 1434 1435
    auto videoCodecs = (*accountInfo)->codecModel->getVideoCodecs();
    if (!videoCodecs.empty()) {
        bool autoQualityEnabled = videoCodecs.front().auto_quality_enabled;
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->checkbutton_autoquality),
                                     autoQualityEnabled);
    } else {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->checkbutton_autoquality),
                                     false);
    }
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

    // Get if the user wants to show the smartInfo box
    auto display_smartinfo = g_action_map_lookup_action(G_ACTION_MAP(g_application_get_default()),
                                                        "display-smartinfo");
    if (g_variant_get_boolean(g_action_get_state(G_ACTION(display_smartinfo)))) {
        gtk_widget_show(widgets->vbox_call_smartInfo);
    } else {
        gtk_widget_hide(widgets->vbox_call_smartInfo);
    }
}

void
CppImpl::updateDetails()
{
1450 1451 1452 1453 1454
    if (!conversation) {
        g_warning("Could not update currentcallview details (null conversation)");
        return;
    }

1455 1456 1457 1458 1459 1460
    auto callRendered = conversation->callId;

    if (!conversation->confId.empty())
        callRendered = conversation->confId;

    gtk_label_set_text(GTK_LABEL(widgets->label_duration),
1461
    (*accountInfo)->callModel->getFormattedCallDuration(callRendered).c_str());
1462 1463 1464 1465 1466
}

void
CppImpl::updateState()
{
1467 1468 1469 1470
    if (!conversation) {
        g_warning("Could not update currentcallview state (null conversation)");
        return;
    }
1471 1472

    auto callId = conversation->callId;
1473 1474

    try {
1475
        auto call = (*accountInfo)->callModel->getCall(callId);
1476

1477
        auto pauseBtn = GTK_TOGGLE_BUTTON(widgets->togglebutton_hold);
1478
        auto image = gtk_image_new_from_resource ("/net/jami/JamiGnome/pause");
1479
        if (call.status == lrc::api::call::Status::PAUSED)
1480
            image = gtk_image_new_from_resource ("/net/jami/JamiGnome/play");
1481 1482
        gtk_button_set_image(GTK_BUTTON(pauseBtn), image);

1483
        auto audioButton = GTK_TOGGLE_BUTTON(widgets->togglebutton_muteaudio);
1484 1485
        gtk_widget_set_sensitive(GTK_WIDGET(widgets->togglebutton_muteaudio),
                                 (call.type != lrc::api::call::Type::CONFERENCE));
1486
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->togglebutton_muteaudio), call.audioMuted);
1487
        auto imageMuteAudio = gtk_image_new_from_resource ("/net/jami/JamiGnome/mute_audio");
1488
        if (call.audioMuted)
1489
            imageMuteAudio = gtk_image_new_from_resource ("/net/jami/JamiGnome/unmute_audio");
1490 1491
        gtk_button_set_image(GTK_BUTTON(audioButton), imageMuteAudio);

1492 1493 1494
        if (!call.isAudioOnly) {
            auto videoButton = GTK_TOGGLE_BUTTON(widgets->togglebutton_mutevideo);
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->togglebutton_mutevideo), call.videoMuted);
1495
            auto imageMuteVideo = gtk_image_new_from_resource ("/net/jami/JamiGnome/mute_video");
1496
            if (call.videoMuted)
1497
                imageMuteVideo = gtk_image_new_from_resource ("/net/jami/JamiGnome/unmute_video");
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
            gtk_button_set_image(GTK_BUTTON(videoButton), imageMuteVideo);
            gtk_widget_set_sensitive(GTK_WIDGET(widgets->togglebutton_mutevideo),
                                 (call.type != lrc::api::call::Type::CONFERENCE));

            gtk_widget_show(widgets->togglebutton_mutevideo);
            gtk_widget_show(widgets->scalebutton_quality);
        } else {
            gtk_widget_hide(widgets->scalebutton_quality);
            gtk_widget_hide(widgets->togglebutton_mutevideo);
        }
1508 1509

        gchar *status = g_strdup_printf("%s", lrc::api::call::to_string(call.status).c_str());
1510
        gtk_label_set_text(GTK_LABEL(widgets->label_status), status);
1511 1512 1513 1514
        g_free(status);
    } catch (std::out_of_range& e) {
        g_warning("Can't update state for callId=%s", callId.c_str());
    }
1515 1516
}

1517 1518
void
CppImpl::updateNameAndPhoto()
1519
{
1520 1521 1522 1523 1524 1525 1526 1527 1528
    QSize photoSize = QSize(60, 60);
    auto callId = conversation->callId;
    try {
        auto call = (*accountInfo)->callModel->getCall(callId);
        if (call.isAudioOnly)
            photoSize = QSize(150, 150);
    } catch (std::out_of_range& e) {
    }

1529
    QVariant var_i = GlobalInstances::pixmapManipulator().conversationPhoto(
1530
        *conversation,
1531
        **(accountInfo),
1532
        photoSize,
1533 1534
        false
    );
1535
    std::shared_ptr<GdkPixbuf> image = var_i.value<std::shared_ptr<GdkPixbuf>>();
1536
    gtk_image_set_from_pixbuf(GTK_IMAGE(widgets->image_peer), image.get());
1537

1538
    try {
1539
        auto contactInfo = (*accountInfo)->contactModel->getContact(conversation->participants.front());
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
        auto alias = contactInfo.profileInfo.alias;
        auto bestName = contactInfo.registeredName;
        if (bestName.empty())
            bestName = contactInfo.profileInfo.uri;
        if (bestName == alias)
            alias = "";
        bestName.erase(std::remove(bestName.begin(), bestName.end(), '\r'), bestName.end());
        alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end());

        if (alias != "") {
            gtk_label_set_text(GTK_LABEL(widgets->label_name), alias.c_str());
            gtk_widget_show(widgets->label_name);
1552
        }