blob: 0c21a311d8b7bd0fe20efa1f87b339b72d71f42c [file] [log] [blame]
Stepan Salenikovichc64523b2015-02-27 16:31:00 -05001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#include "currentcallview.h"
32
33#include <gtk/gtk.h>
34#include <call.h>
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050035#include <callmodel.h>
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050036#include "utils/drawing.h"
37#include "video/video_widget.h"
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040038#include <video/previewmanager.h>
Stepan Salenikovich6f687072015-03-26 10:43:37 -040039#include <contactmethod.h>
40#include <person.h>
41#include "delegates/pixbufdelegate.h"
Stepan Salenikovicha448f602015-05-29 13:33:06 -040042#include <media/media.h>
43#include <media/text.h>
44#include <media/textrecording.h>
45#include "models/gtkqtreemodel.h"
Stepan Salenikovichb94873c2015-06-02 16:53:18 -040046#include "video/videowindow.h"
Stepan Salenikovich67112d12015-06-16 16:57:06 -040047#include "ringnotify.h"
Stepan Salenikovichf6f42652015-07-15 12:46:14 -040048#include <audio/codecmodel.h>
49#include <account.h>
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050050
51struct _CurrentCallView
52{
53 GtkBox parent;
54};
55
56struct _CurrentCallViewClass
57{
58 GtkBoxClass parent_class;
59};
60
61typedef struct _CurrentCallViewPrivate CurrentCallViewPrivate;
62
63struct _CurrentCallViewPrivate
64{
65 GtkWidget *image_peer;
66 GtkWidget *label_identity;
67 GtkWidget *label_status;
68 GtkWidget *label_duration;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050069 GtkWidget *frame_video;
70 GtkWidget *video_widget;
Stepan Salenikovicha448f602015-05-29 13:33:06 -040071 GtkWidget *revealer_chat;
72 GtkWidget *togglebutton_chat;
73 GtkWidget *textview_chat;
74 GtkWidget *button_chat_input;
75 GtkWidget *entry_chat_input;
76 GtkWidget *scrolledwindow_chat;
Stepan Salenikovichb94873c2015-06-02 16:53:18 -040077 GtkWidget *fullscreen_window;
Stepan Salenikovich77baa522015-07-07 15:29:14 -040078 GtkWidget *buttonbox_call_controls;
79 GtkWidget *button_hangup;
Stepan Salenikovichf6f42652015-07-15 12:46:14 -040080 GtkWidget *scalebutton_quality;
81
82 /* flag used to keep track of the video quality scale pressed state;
83 * we do not want to update the codec bitrate until the user releases the
84 * scale button */
85 gboolean quality_scale_pressed;
Stepan Salenikovicha448f602015-05-29 13:33:06 -040086
87 Call *call;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050088
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050089 QMetaObject::Connection state_change_connection;
90 QMetaObject::Connection call_details_connection;
Stepan Salenikovichc5f08152015-03-19 00:53:23 -040091 QMetaObject::Connection local_renderer_connection;
92 QMetaObject::Connection remote_renderer_connection;
Stepan Salenikovicha448f602015-05-29 13:33:06 -040093 QMetaObject::Connection media_added_connection;
94 QMetaObject::Connection new_message_connection;
Stepan Salenikovich7b60b592015-06-16 12:29:07 -040095 QMetaObject::Connection incoming_msg_connection;
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050096};
97
98G_DEFINE_TYPE_WITH_PRIVATE(CurrentCallView, current_call_view, GTK_TYPE_BOX);
99
100#define CURRENT_CALL_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CURRENT_CALL_VIEW_TYPE, CurrentCallViewPrivate))
101
102static void
103current_call_view_dispose(GObject *object)
104{
105 CurrentCallView *view;
106 CurrentCallViewPrivate *priv;
107
108 view = CURRENT_CALL_VIEW(object);
109 priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
110
111 QObject::disconnect(priv->state_change_connection);
112 QObject::disconnect(priv->call_details_connection);
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400113 QObject::disconnect(priv->local_renderer_connection);
114 QObject::disconnect(priv->remote_renderer_connection);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400115 QObject::disconnect(priv->media_added_connection);
116 QObject::disconnect(priv->new_message_connection);
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400117 QObject::disconnect(priv->incoming_msg_connection);
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400118
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400119 if (priv->fullscreen_window) {
120 gtk_widget_destroy(priv->fullscreen_window);
121 priv->fullscreen_window = NULL;
122 }
123
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500124 G_OBJECT_CLASS(current_call_view_parent_class)->dispose(object);
125}
126
127static void
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400128chat_toggled(GtkToggleButton *togglebutton, CurrentCallView *self)
129{
130 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
131 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
132
133 gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer_chat),
134 gtk_toggle_button_get_active(togglebutton));
135
136 if (gtk_toggle_button_get_active(togglebutton)) {
137 /* create an outgoing media to bring up chat history, if any */
138 priv->call->addOutgoingMedia<Media::Text>();
Stepan Salenikovichbffaf582015-06-22 14:10:40 -0400139 /* change focus to the chat entry */
140 gtk_widget_grab_focus(priv->entry_chat_input);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400141 }
142}
143
144static void
145send_chat(G_GNUC_UNUSED GtkWidget *widget, CurrentCallView *self)
146{
147 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
148 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
149
150 /* make sure there is text to send */
151 const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry_chat_input));
152 if (text && strlen(text) > 0) {
Stepan Salenikovich6af88db2015-07-17 12:41:29 -0400153 QMap<QString, QString> messages;
154 messages["text/plain"] = text;
155 priv->call->addOutgoingMedia<Media::Text>()->send(messages);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400156 /* clear the entry */
157 gtk_entry_set_text(GTK_ENTRY(priv->entry_chat_input), "");
158 }
159}
160
161static void
162scroll_to_bottom(GtkAdjustment *adjustment, G_GNUC_UNUSED gpointer user_data)
163{
164 gtk_adjustment_set_value(adjustment,
165 gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment));
166}
167
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400168/**
169 * This gets the GtkScaleButtonScale widget (which is a GtkScale) from the
170 * given GtkScaleButton in order to be able to modify its properties and connect
171 * to its signals
172 */
173static GtkScale *
174gtk_scale_button_get_scale(GtkScaleButton *button)
175{
176 GtkScale *scale = NULL;
177 GtkWidget *dock = gtk_scale_button_get_popup(button);
178
179 // the dock is a popup window which contains a frame, which contains a box
180 // which contains the + button, scale, and - button
181 // we want to get the scale
182 if (GtkWidget *box = gtk_bin_get_child(GTK_BIN(dock))) {
183 GList *children = gtk_container_get_children(GTK_CONTAINER(box));
184 for (GList *c = children; c && !scale; c = c->next) {
185 if (GTK_IS_SCALE(c->data))
186 scale = GTK_SCALE(c->data);
187 }
188 g_list_free(children);
189 }
190
191 return scale;
192}
193
194static void
195quality_changed(GtkScaleButton *button, G_GNUC_UNUSED gdouble value, CurrentCallView *self)
196{
197 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
198 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
199
200 /* only update if the scale button is released, to reduce the number of updates */
201 if (priv->quality_scale_pressed) return;
202
203 /* we get the value directly from the widget, in case this function is not
204 * called from the event */
205 unsigned int bitrate = (unsigned int)gtk_scale_button_get_value(button);
206
207 if (const auto& codecModel = priv->call->account()->codecModel()) {
208 const auto& videoCodecs = codecModel->videoCodecs();
209 for (int i=0; i < videoCodecs->rowCount();i++) {
210 const auto& idx = videoCodecs->index(i,0);
211 g_debug("setting codec bitrate to %u", bitrate);
212 videoCodecs->setData(idx, QString::number(bitrate), CodecModel::Role::BITRATE);
213 }
214 codecModel << CodecModel::EditAction::SAVE;
215 }
216}
217
218static gboolean
219quality_button_pressed(G_GNUC_UNUSED GtkWidget *widget, G_GNUC_UNUSED GdkEvent *event, CurrentCallView *self)
220{
221 g_debug("button pressed");
222 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(self), FALSE);
223 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
224
225 priv->quality_scale_pressed = TRUE;
226
227 return FALSE; // propogate the event
228}
229
230static gboolean
231quality_button_released(G_GNUC_UNUSED GtkWidget *widget, G_GNUC_UNUSED GdkEvent *event, CurrentCallView *self)
232{
233 g_debug("button released");
234 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(self), FALSE);
235 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
236
237 priv->quality_scale_pressed = FALSE;
238
239 /* now make sure the quality gets updated */
240 quality_changed(GTK_SCALE_BUTTON(priv->scalebutton_quality), 0, self);
241
242 return FALSE; // propogate the event
243}
244
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400245static void
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500246current_call_view_init(CurrentCallView *view)
247{
248 gtk_widget_init_template(GTK_WIDGET(view));
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400249
250 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
251
252 g_signal_connect(priv->togglebutton_chat, "toggled", G_CALLBACK(chat_toggled), view);
253 g_signal_connect(priv->button_chat_input, "clicked", G_CALLBACK(send_chat), view);
254 g_signal_connect(priv->entry_chat_input, "activate", G_CALLBACK(send_chat), view);
255
256 /* the adjustment params will change only when the model is created and when
257 * new messages are added; in these cases we want to scroll to the bottom of
258 * the chat treeview */
259 GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(priv->scrolledwindow_chat));
260 g_signal_connect(adjustment, "changed", G_CALLBACK(scroll_to_bottom), NULL);
Stepan Salenikovich77baa522015-07-07 15:29:14 -0400261
262 GtkCssProvider *provider = gtk_css_provider_new();
263 GdkDisplay *display = gdk_display_get_default();
264 GdkScreen *screen = gdk_display_get_default_screen(display);
265 gtk_css_provider_load_from_data(provider,
266 "GtkBox#call-controls GtkButton {\n"
267 " border-radius: 21px;\n"
268 "}\n"
269 , -1, NULL);
270 gtk_style_context_add_provider_for_screen(screen,
271 GTK_STYLE_PROVIDER(provider),
272 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400273
274 /* customize the quality button scale */
275 if (GtkScale *scale = gtk_scale_button_get_scale(GTK_SCALE_BUTTON(priv->scalebutton_quality))) {
276 gtk_scale_set_draw_value(scale, TRUE);
277 gtk_scale_set_value_pos(scale, GTK_POS_RIGHT);
278 gtk_scale_set_digits(scale, 0);
279 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500280}
281
282static void
283current_call_view_class_init(CurrentCallViewClass *klass)
284{
285 G_OBJECT_CLASS(klass)->dispose = current_call_view_dispose;
286
287 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
288 "/cx/ring/RingGnome/currentcallview.ui");
289
290 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, image_peer);
291 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_identity);
292 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_status);
293 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_duration);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500294 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, frame_video);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400295 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, revealer_chat);
296 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, togglebutton_chat);
297 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, textview_chat);
298 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, button_chat_input);
299 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, entry_chat_input);
300 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, scrolledwindow_chat);
Stepan Salenikovich77baa522015-07-07 15:29:14 -0400301 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, buttonbox_call_controls);
302 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, button_hangup);
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400303 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, scalebutton_quality);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500304}
305
306GtkWidget *
307current_call_view_new(void)
308{
309 return (GtkWidget *)g_object_new(CURRENT_CALL_VIEW_TYPE, NULL);
310}
311
312static void
313update_state(CurrentCallView *view, Call *call)
314{
315 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
316
Stepan Salenikovich7ec8fe82015-06-02 18:26:39 -0400317 gchar *status = g_strdup_printf("%s", call->toHumanStateName().toUtf8().constData());
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500318
Stepan Salenikovich7ec8fe82015-06-02 18:26:39 -0400319 gtk_label_set_text(GTK_LABEL(priv->label_status), status);
320
321 g_free(status);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500322}
323
324static void
325update_details(CurrentCallView *view, Call *call)
326{
327 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
328
329 /* update call duration */
330 QByteArray ba_length = call->length().toLocal8Bit();
331 gtk_label_set_text(GTK_LABEL(priv->label_duration), ba_length.constData());
332}
333
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400334static void
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400335on_fullscreen_destroy(CurrentCallView *view)
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400336{
337 g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
338 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
339
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400340 /* fullscreen is being destroyed, clear the pointer and un-pause the rendering
341 * in this window */
342 priv->fullscreen_window = NULL;
343 video_widget_pause_rendering(VIDEO_WIDGET(priv->video_widget), FALSE);
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400344}
345
346static gboolean
347on_button_press_in_video_event(GtkWidget *self, GdkEventButton *event, CurrentCallView *view)
348{
349 g_return_val_if_fail(IS_VIDEO_WIDGET(self), FALSE);
350 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);
351 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
352
353 /* on double click */
354 if (event->type == GDK_2BUTTON_PRESS) {
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400355 if (priv->fullscreen_window) {
356 /* destroy the fullscreen */
357 gtk_widget_destroy(priv->fullscreen_window);
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400358 } else {
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400359 /* pause rendering in this window and create fullscreen */
360 video_widget_pause_rendering(VIDEO_WIDGET(priv->video_widget), TRUE);
361
362 priv->fullscreen_window = video_window_new(priv->call,
363 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view))));
364
365 /* connect to destruction of fullscreen so we know when to un-pause
366 * the rendering in thiw window */
367 g_signal_connect_swapped(priv->fullscreen_window,
368 "destroy",
369 G_CALLBACK(on_fullscreen_destroy),
370 view);
371
372 /* present the fullscreen widnow */
373 gtk_window_present(GTK_WINDOW(priv->fullscreen_window));
374 gtk_window_fullscreen(GTK_WINDOW(priv->fullscreen_window));
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400375 }
376 }
377
378 /* the event has been fully handled */
379 return TRUE;
380}
381
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400382static void
383print_message_to_buffer(const QModelIndex &idx, GtkTextBuffer *buffer)
384{
385 if (idx.isValid()) {
Stepan Salenikovichdeae6252015-06-22 12:51:52 -0400386 auto message = idx.data().value<QString>().toUtf8();
387 auto sender = idx.data(static_cast<int>(Media::TextRecording::Role::AuthorDisplayname)).value<QString>().toUtf8();
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400388
389 GtkTextIter iter;
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400390
Stepan Salenikovichdeae6252015-06-22 12:51:52 -0400391 /* unless its the very first message, insert a new line */
392 if (idx.row() != 0) {
393 gtk_text_buffer_get_end_iter(buffer, &iter);
394 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
395 }
396
397 auto format_sender = g_strconcat(sender.constData(), ": ", NULL);
398 gtk_text_buffer_get_end_iter(buffer, &iter);
399 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
400 format_sender, -1,
401 "bold", NULL);
402 g_free(format_sender);
403
404 /* if the sender name is too long, insert a new line after it */
405 if (sender.length() > 20) {
406 gtk_text_buffer_get_end_iter(buffer, &iter);
407 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
408 }
409
410 gtk_text_buffer_get_end_iter(buffer, &iter);
411 gtk_text_buffer_insert(buffer, &iter, message.constData(), -1);
412
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400413 } else {
414 g_warning("QModelIndex in im model is not valid");
415 }
416}
417
418static void
419parse_chat_model(QAbstractItemModel *model, CurrentCallView *self)
420{
421 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
422 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
423
424 /* new model, disconnect from the old model updates and clear the text buffer */
425 QObject::disconnect(priv->new_message_connection);
426
427 GtkTextBuffer *new_buffer = gtk_text_buffer_new(NULL);
428 gtk_text_view_set_buffer(GTK_TEXT_VIEW(priv->textview_chat), new_buffer);
Stepan Salenikovichdeae6252015-06-22 12:51:52 -0400429
430 /* add tags to the buffer */
431 gtk_text_buffer_create_tag(new_buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
432
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400433 g_object_unref(new_buffer);
434
435 /* put all the messages in the im model into the text view */
436 for (int row = 0; row < model->rowCount(); ++row) {
437 QModelIndex idx = model->index(row, 0);
438 print_message_to_buffer(idx, new_buffer);
439 }
440
441 /* append new messages */
442 priv->new_message_connection = QObject::connect(
443 model,
444 &QAbstractItemModel::rowsInserted,
445 [priv, model] (const QModelIndex &parent, int first, int last) {
446 for (int row = first; row <= last; ++row) {
447 QModelIndex idx = model->index(row, 0, parent);
448 print_message_to_buffer(idx, gtk_text_view_get_buffer(GTK_TEXT_VIEW(priv->textview_chat)));
449 }
450 }
451 );
452}
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400453
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500454void
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400455monitor_incoming_message(CurrentCallView *self, Media::Text *media)
456{
457 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
458 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
459
460 /* connect to incoming chat messages to open the chat view */
461 QObject::disconnect(priv->incoming_msg_connection);
462 priv->incoming_msg_connection = QObject::connect(
463 media,
464 &Media::Text::messageReceived,
Stepan Salenikovich6af88db2015-07-17 12:41:29 -0400465 [priv] (G_GNUC_UNUSED const QMap<QString,QString>& m) {
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400466 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->togglebutton_chat), TRUE);
467 }
468 );
469}
470
471void
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500472current_call_view_set_call_info(CurrentCallView *view, const QModelIndex& idx) {
473 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
474
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400475 priv->call = CallModel::instance()->getCall(idx);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400476
477 /* get call image */
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400478 QVariant var_i = PixbufDelegate::instance()->callPhoto(priv->call, QSize(60, 60), false);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400479 std::shared_ptr<GdkPixbuf> image = var_i.value<std::shared_ptr<GdkPixbuf>>();
480 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_peer), image.get());
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500481
482 /* get name */
Stepan Salenikovich9c1f6682015-03-09 16:21:28 -0400483 QVariant var = idx.model()->data(idx, static_cast<int>(Call::Role::Name));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400484 QByteArray ba_name = var.toString().toUtf8();
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500485 gtk_label_set_text(GTK_LABEL(priv->label_identity), ba_name.constData());
486
487 /* change some things depending on call state */
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400488 update_state(view, priv->call);
489 update_details(view, priv->call);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500490
491 priv->state_change_connection = QObject::connect(
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400492 priv->call,
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500493 &Call::stateChanged,
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400494 [view, priv]() { update_state(view, priv->call); }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500495 );
496
497 priv->call_details_connection = QObject::connect(
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400498 priv->call,
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500499 static_cast<void (Call::*)(void)>(&Call::changed),
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400500 [view, priv]() { update_details(view, priv->call); }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500501 );
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500502
503 /* video widget */
504 priv->video_widget = video_widget_new();
505 gtk_container_add(GTK_CONTAINER(priv->frame_video), priv->video_widget);
506 gtk_widget_show_all(priv->frame_video);
507
508 /* check if we already have a renderer */
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400509 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400510 priv->call->videoRenderer(),
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400511 VIDEO_RENDERER_REMOTE);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500512
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400513 /* callback for remote renderer */
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400514 priv->remote_renderer_connection = QObject::connect(
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400515 priv->call,
Stepan Salenikovich9c1f6682015-03-09 16:21:28 -0400516 &Call::videoStarted,
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400517 [priv](Video::Renderer *renderer) {
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400518 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
519 renderer,
520 VIDEO_RENDERER_REMOTE);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500521 }
522 );
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400523
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400524 /* local renderer */
Stepan Salenikovich57058802015-03-25 14:16:13 -0400525 if (Video::PreviewManager::instance()->isPreviewing())
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400526 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
527 Video::PreviewManager::instance()->previewRenderer(),
528 VIDEO_RENDERER_LOCAL);
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400529
530 /* callback for local renderer */
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400531 priv->local_renderer_connection = QObject::connect(
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400532 Video::PreviewManager::instance(),
533 &Video::PreviewManager::previewStarted,
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400534 [priv](Video::Renderer *renderer) {
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400535 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
536 renderer,
537 VIDEO_RENDERER_LOCAL);
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400538 }
539 );
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400540
541 /* catch double click to make full screen */
542 g_signal_connect(priv->video_widget, "button-press-event",
543 G_CALLBACK(on_button_press_in_video_event),
544 view);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400545
546 /* check if text media is already present */
547 if (priv->call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
548 Media::Text *text = priv->call->firstMedia<Media::Text>(Media::Media::Direction::IN);
549 parse_chat_model(text->recording()->instantMessagingModel(), view);
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400550 monitor_incoming_message(view, text);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400551 } else if (priv->call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
552 Media::Text *text = priv->call->firstMedia<Media::Text>(Media::Media::Direction::OUT);
553 parse_chat_model(text->recording()->instantMessagingModel(), view);
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400554 monitor_incoming_message(view, text);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400555 } else {
556 /* monitor media for messaging text messaging */
557 priv->media_added_connection = QObject::connect(
558 priv->call,
559 &Call::mediaAdded,
560 [view, priv] (Media::Media* media) {
561 if (media->type() == Media::Media::Type::TEXT) {
562 parse_chat_model(((Media::Text*)media)->recording()->instantMessagingModel(), view);
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400563 monitor_incoming_message(view, (Media::Text*)media);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400564 QObject::disconnect(priv->media_added_connection);
565 }
566 }
567 );
568 }
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400569
570 /* check if there were any chat notifications and open the chat view if so */
571 if (ring_notify_close_chat_notification(priv->call))
572 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->togglebutton_chat), TRUE);
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400573
574 /* get the current codec quality and set that as the initial slider value
575 * for now we assume that all codecs have the same quality */
576 if (const auto& codecModel = priv->call->account()->codecModel()) {
577 const auto& videoCodecs = codecModel->videoCodecs();
578 if (videoCodecs->rowCount() > 0) {
579 const auto& idx = videoCodecs->index(0,0);
580 double value = idx.data(static_cast<int>(CodecModel::Role::BITRATE)).toDouble();
581 gtk_scale_button_set_value(GTK_SCALE_BUTTON(priv->scalebutton_quality), value);
582 }
583 }
584 g_signal_connect(priv->scalebutton_quality, "value-changed", G_CALLBACK(quality_changed), view);
585 g_signal_connect(gtk_scale_button_get_scale(GTK_SCALE_BUTTON(priv->scalebutton_quality)),
586 "button-press-event", G_CALLBACK(quality_button_pressed), view);
587 g_signal_connect(gtk_scale_button_get_scale(GTK_SCALE_BUTTON(priv->scalebutton_quality)),
588 "button-release-event", G_CALLBACK(quality_button_released), view);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500589}