blob: 1a8b704220588f17abdc981dd715e4a27989806f [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
Stepan Salenikovich9b8f5452015-07-16 10:36:07 -0400179 // the dock is a popover which contains a box
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400180 // 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))) {
Stepan Salenikovich9b8f5452015-07-16 10:36:07 -0400183 if (GTK_IS_FRAME(box)) {
184 // support older versions of gtk; the box used to be in a frame
185 box = gtk_bin_get_child(GTK_BIN(box));
186 }
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400187 GList *children = gtk_container_get_children(GTK_CONTAINER(box));
188 for (GList *c = children; c && !scale; c = c->next) {
189 if (GTK_IS_SCALE(c->data))
190 scale = GTK_SCALE(c->data);
191 }
192 g_list_free(children);
193 }
194
195 return scale;
196}
197
198static void
199quality_changed(GtkScaleButton *button, G_GNUC_UNUSED gdouble value, CurrentCallView *self)
200{
201 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
202 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
203
204 /* only update if the scale button is released, to reduce the number of updates */
205 if (priv->quality_scale_pressed) return;
206
207 /* we get the value directly from the widget, in case this function is not
208 * called from the event */
209 unsigned int bitrate = (unsigned int)gtk_scale_button_get_value(button);
210
211 if (const auto& codecModel = priv->call->account()->codecModel()) {
212 const auto& videoCodecs = codecModel->videoCodecs();
213 for (int i=0; i < videoCodecs->rowCount();i++) {
214 const auto& idx = videoCodecs->index(i,0);
215 g_debug("setting codec bitrate to %u", bitrate);
216 videoCodecs->setData(idx, QString::number(bitrate), CodecModel::Role::BITRATE);
217 }
218 codecModel << CodecModel::EditAction::SAVE;
219 }
220}
221
222static gboolean
223quality_button_pressed(G_GNUC_UNUSED GtkWidget *widget, G_GNUC_UNUSED GdkEvent *event, CurrentCallView *self)
224{
225 g_debug("button pressed");
226 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(self), FALSE);
227 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
228
229 priv->quality_scale_pressed = TRUE;
230
231 return FALSE; // propogate the event
232}
233
234static gboolean
235quality_button_released(G_GNUC_UNUSED GtkWidget *widget, G_GNUC_UNUSED GdkEvent *event, CurrentCallView *self)
236{
237 g_debug("button released");
238 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(self), FALSE);
239 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
240
241 priv->quality_scale_pressed = FALSE;
242
243 /* now make sure the quality gets updated */
244 quality_changed(GTK_SCALE_BUTTON(priv->scalebutton_quality), 0, self);
245
246 return FALSE; // propogate the event
247}
248
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400249static void
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500250current_call_view_init(CurrentCallView *view)
251{
252 gtk_widget_init_template(GTK_WIDGET(view));
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400253
254 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
255
256 g_signal_connect(priv->togglebutton_chat, "toggled", G_CALLBACK(chat_toggled), view);
257 g_signal_connect(priv->button_chat_input, "clicked", G_CALLBACK(send_chat), view);
258 g_signal_connect(priv->entry_chat_input, "activate", G_CALLBACK(send_chat), view);
259
260 /* the adjustment params will change only when the model is created and when
261 * new messages are added; in these cases we want to scroll to the bottom of
262 * the chat treeview */
263 GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(priv->scrolledwindow_chat));
264 g_signal_connect(adjustment, "changed", G_CALLBACK(scroll_to_bottom), NULL);
Stepan Salenikovich77baa522015-07-07 15:29:14 -0400265
266 GtkCssProvider *provider = gtk_css_provider_new();
267 GdkDisplay *display = gdk_display_get_default();
268 GdkScreen *screen = gdk_display_get_default_screen(display);
269 gtk_css_provider_load_from_data(provider,
270 "GtkBox#call-controls GtkButton {\n"
271 " border-radius: 21px;\n"
272 "}\n"
273 , -1, NULL);
274 gtk_style_context_add_provider_for_screen(screen,
275 GTK_STYLE_PROVIDER(provider),
276 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400277
278 /* customize the quality button scale */
279 if (GtkScale *scale = gtk_scale_button_get_scale(GTK_SCALE_BUTTON(priv->scalebutton_quality))) {
280 gtk_scale_set_draw_value(scale, TRUE);
281 gtk_scale_set_value_pos(scale, GTK_POS_RIGHT);
282 gtk_scale_set_digits(scale, 0);
283 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500284}
285
286static void
287current_call_view_class_init(CurrentCallViewClass *klass)
288{
289 G_OBJECT_CLASS(klass)->dispose = current_call_view_dispose;
290
291 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
292 "/cx/ring/RingGnome/currentcallview.ui");
293
294 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, image_peer);
295 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_identity);
296 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_status);
297 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_duration);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500298 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, frame_video);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400299 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, revealer_chat);
300 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, togglebutton_chat);
301 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, textview_chat);
302 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, button_chat_input);
303 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, entry_chat_input);
304 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, scrolledwindow_chat);
Stepan Salenikovich77baa522015-07-07 15:29:14 -0400305 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, buttonbox_call_controls);
306 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, button_hangup);
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400307 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, scalebutton_quality);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500308}
309
310GtkWidget *
311current_call_view_new(void)
312{
313 return (GtkWidget *)g_object_new(CURRENT_CALL_VIEW_TYPE, NULL);
314}
315
316static void
317update_state(CurrentCallView *view, Call *call)
318{
319 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
320
Stepan Salenikovich7ec8fe82015-06-02 18:26:39 -0400321 gchar *status = g_strdup_printf("%s", call->toHumanStateName().toUtf8().constData());
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500322
Stepan Salenikovich7ec8fe82015-06-02 18:26:39 -0400323 gtk_label_set_text(GTK_LABEL(priv->label_status), status);
324
325 g_free(status);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500326}
327
328static void
329update_details(CurrentCallView *view, Call *call)
330{
331 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
332
333 /* update call duration */
334 QByteArray ba_length = call->length().toLocal8Bit();
335 gtk_label_set_text(GTK_LABEL(priv->label_duration), ba_length.constData());
336}
337
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400338static void
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400339on_fullscreen_destroy(CurrentCallView *view)
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400340{
341 g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
342 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
343
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400344 /* fullscreen is being destroyed, clear the pointer and un-pause the rendering
345 * in this window */
346 priv->fullscreen_window = NULL;
347 video_widget_pause_rendering(VIDEO_WIDGET(priv->video_widget), FALSE);
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400348}
349
350static gboolean
351on_button_press_in_video_event(GtkWidget *self, GdkEventButton *event, CurrentCallView *view)
352{
353 g_return_val_if_fail(IS_VIDEO_WIDGET(self), FALSE);
354 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);
355 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
356
357 /* on double click */
358 if (event->type == GDK_2BUTTON_PRESS) {
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400359 if (priv->fullscreen_window) {
360 /* destroy the fullscreen */
361 gtk_widget_destroy(priv->fullscreen_window);
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400362 } else {
Stepan Salenikovichb94873c2015-06-02 16:53:18 -0400363 /* pause rendering in this window and create fullscreen */
364 video_widget_pause_rendering(VIDEO_WIDGET(priv->video_widget), TRUE);
365
366 priv->fullscreen_window = video_window_new(priv->call,
367 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view))));
368
369 /* connect to destruction of fullscreen so we know when to un-pause
370 * the rendering in thiw window */
371 g_signal_connect_swapped(priv->fullscreen_window,
372 "destroy",
373 G_CALLBACK(on_fullscreen_destroy),
374 view);
375
376 /* present the fullscreen widnow */
377 gtk_window_present(GTK_WINDOW(priv->fullscreen_window));
378 gtk_window_fullscreen(GTK_WINDOW(priv->fullscreen_window));
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400379 }
380 }
381
382 /* the event has been fully handled */
383 return TRUE;
384}
385
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400386static void
387print_message_to_buffer(const QModelIndex &idx, GtkTextBuffer *buffer)
388{
389 if (idx.isValid()) {
Stepan Salenikovichdeae6252015-06-22 12:51:52 -0400390 auto message = idx.data().value<QString>().toUtf8();
391 auto sender = idx.data(static_cast<int>(Media::TextRecording::Role::AuthorDisplayname)).value<QString>().toUtf8();
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400392
393 GtkTextIter iter;
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400394
Stepan Salenikovichdeae6252015-06-22 12:51:52 -0400395 /* unless its the very first message, insert a new line */
396 if (idx.row() != 0) {
397 gtk_text_buffer_get_end_iter(buffer, &iter);
398 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
399 }
400
401 auto format_sender = g_strconcat(sender.constData(), ": ", NULL);
402 gtk_text_buffer_get_end_iter(buffer, &iter);
403 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
404 format_sender, -1,
405 "bold", NULL);
406 g_free(format_sender);
407
408 /* if the sender name is too long, insert a new line after it */
409 if (sender.length() > 20) {
410 gtk_text_buffer_get_end_iter(buffer, &iter);
411 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
412 }
413
414 gtk_text_buffer_get_end_iter(buffer, &iter);
415 gtk_text_buffer_insert(buffer, &iter, message.constData(), -1);
416
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400417 } else {
418 g_warning("QModelIndex in im model is not valid");
419 }
420}
421
422static void
423parse_chat_model(QAbstractItemModel *model, CurrentCallView *self)
424{
425 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
426 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
427
428 /* new model, disconnect from the old model updates and clear the text buffer */
429 QObject::disconnect(priv->new_message_connection);
430
431 GtkTextBuffer *new_buffer = gtk_text_buffer_new(NULL);
432 gtk_text_view_set_buffer(GTK_TEXT_VIEW(priv->textview_chat), new_buffer);
Stepan Salenikovichdeae6252015-06-22 12:51:52 -0400433
434 /* add tags to the buffer */
435 gtk_text_buffer_create_tag(new_buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
436
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400437 g_object_unref(new_buffer);
438
439 /* put all the messages in the im model into the text view */
440 for (int row = 0; row < model->rowCount(); ++row) {
441 QModelIndex idx = model->index(row, 0);
442 print_message_to_buffer(idx, new_buffer);
443 }
444
445 /* append new messages */
446 priv->new_message_connection = QObject::connect(
447 model,
448 &QAbstractItemModel::rowsInserted,
449 [priv, model] (const QModelIndex &parent, int first, int last) {
450 for (int row = first; row <= last; ++row) {
451 QModelIndex idx = model->index(row, 0, parent);
452 print_message_to_buffer(idx, gtk_text_view_get_buffer(GTK_TEXT_VIEW(priv->textview_chat)));
453 }
454 }
455 );
456}
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400457
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500458void
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400459monitor_incoming_message(CurrentCallView *self, Media::Text *media)
460{
461 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
462 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
463
464 /* connect to incoming chat messages to open the chat view */
465 QObject::disconnect(priv->incoming_msg_connection);
466 priv->incoming_msg_connection = QObject::connect(
467 media,
468 &Media::Text::messageReceived,
Stepan Salenikovich6af88db2015-07-17 12:41:29 -0400469 [priv] (G_GNUC_UNUSED const QMap<QString,QString>& m) {
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400470 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->togglebutton_chat), TRUE);
471 }
472 );
473}
474
475void
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500476current_call_view_set_call_info(CurrentCallView *view, const QModelIndex& idx) {
477 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
478
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400479 priv->call = CallModel::instance()->getCall(idx);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400480
481 /* get call image */
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400482 QVariant var_i = PixbufDelegate::instance()->callPhoto(priv->call, QSize(60, 60), false);
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400483 std::shared_ptr<GdkPixbuf> image = var_i.value<std::shared_ptr<GdkPixbuf>>();
484 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_peer), image.get());
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500485
486 /* get name */
Stepan Salenikovich9c1f6682015-03-09 16:21:28 -0400487 QVariant var = idx.model()->data(idx, static_cast<int>(Call::Role::Name));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400488 QByteArray ba_name = var.toString().toUtf8();
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500489 gtk_label_set_text(GTK_LABEL(priv->label_identity), ba_name.constData());
490
491 /* change some things depending on call state */
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400492 update_state(view, priv->call);
493 update_details(view, priv->call);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500494
495 priv->state_change_connection = QObject::connect(
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400496 priv->call,
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500497 &Call::stateChanged,
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400498 [view, priv]() { update_state(view, priv->call); }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500499 );
500
501 priv->call_details_connection = QObject::connect(
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400502 priv->call,
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500503 static_cast<void (Call::*)(void)>(&Call::changed),
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400504 [view, priv]() { update_details(view, priv->call); }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500505 );
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500506
507 /* video widget */
508 priv->video_widget = video_widget_new();
509 gtk_container_add(GTK_CONTAINER(priv->frame_video), priv->video_widget);
510 gtk_widget_show_all(priv->frame_video);
511
512 /* check if we already have a renderer */
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400513 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400514 priv->call->videoRenderer(),
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400515 VIDEO_RENDERER_REMOTE);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500516
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400517 /* callback for remote renderer */
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400518 priv->remote_renderer_connection = QObject::connect(
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400519 priv->call,
Stepan Salenikovich9c1f6682015-03-09 16:21:28 -0400520 &Call::videoStarted,
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400521 [priv](Video::Renderer *renderer) {
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400522 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
523 renderer,
524 VIDEO_RENDERER_REMOTE);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500525 }
526 );
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400527
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400528 /* local renderer */
Stepan Salenikovich57058802015-03-25 14:16:13 -0400529 if (Video::PreviewManager::instance()->isPreviewing())
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400530 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
531 Video::PreviewManager::instance()->previewRenderer(),
532 VIDEO_RENDERER_LOCAL);
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400533
534 /* callback for local renderer */
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400535 priv->local_renderer_connection = QObject::connect(
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400536 Video::PreviewManager::instance(),
537 &Video::PreviewManager::previewStarted,
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400538 [priv](Video::Renderer *renderer) {
Stepan Salenikovich0f693232015-04-22 10:45:08 -0400539 video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget),
540 renderer,
541 VIDEO_RENDERER_LOCAL);
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400542 }
543 );
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400544
545 /* catch double click to make full screen */
546 g_signal_connect(priv->video_widget, "button-press-event",
547 G_CALLBACK(on_button_press_in_video_event),
548 view);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400549
550 /* check if text media is already present */
551 if (priv->call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
552 Media::Text *text = priv->call->firstMedia<Media::Text>(Media::Media::Direction::IN);
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 if (priv->call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
556 Media::Text *text = priv->call->firstMedia<Media::Text>(Media::Media::Direction::OUT);
557 parse_chat_model(text->recording()->instantMessagingModel(), view);
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400558 monitor_incoming_message(view, text);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400559 } else {
560 /* monitor media for messaging text messaging */
561 priv->media_added_connection = QObject::connect(
562 priv->call,
563 &Call::mediaAdded,
564 [view, priv] (Media::Media* media) {
565 if (media->type() == Media::Media::Type::TEXT) {
566 parse_chat_model(((Media::Text*)media)->recording()->instantMessagingModel(), view);
Stepan Salenikovich7b60b592015-06-16 12:29:07 -0400567 monitor_incoming_message(view, (Media::Text*)media);
Stepan Salenikovicha448f602015-05-29 13:33:06 -0400568 QObject::disconnect(priv->media_added_connection);
569 }
570 }
571 );
572 }
Stepan Salenikovich67112d12015-06-16 16:57:06 -0400573
574 /* check if there were any chat notifications and open the chat view if so */
575 if (ring_notify_close_chat_notification(priv->call))
576 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->togglebutton_chat), TRUE);
Stepan Salenikovichf6f42652015-07-15 12:46:14 -0400577
578 /* get the current codec quality and set that as the initial slider value
579 * for now we assume that all codecs have the same quality */
580 if (const auto& codecModel = priv->call->account()->codecModel()) {
581 const auto& videoCodecs = codecModel->videoCodecs();
582 if (videoCodecs->rowCount() > 0) {
583 const auto& idx = videoCodecs->index(0,0);
584 double value = idx.data(static_cast<int>(CodecModel::Role::BITRATE)).toDouble();
585 gtk_scale_button_set_value(GTK_SCALE_BUTTON(priv->scalebutton_quality), value);
586 }
587 }
588 g_signal_connect(priv->scalebutton_quality, "value-changed", G_CALLBACK(quality_changed), view);
589 g_signal_connect(gtk_scale_button_get_scale(GTK_SCALE_BUTTON(priv->scalebutton_quality)),
590 "button-press-event", G_CALLBACK(quality_button_pressed), view);
591 g_signal_connect(gtk_scale_button_get_scale(GTK_SCALE_BUTTON(priv->scalebutton_quality)),
592 "button-release-event", G_CALLBACK(quality_button_released), view);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500593}