Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 1 | /* |
| 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 Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 35 | #include <callmodel.h> |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 36 | #include "utils/drawing.h" |
| 37 | #include "video/video_widget.h" |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 38 | #include <video/previewmanager.h> |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 39 | #include <contactmethod.h> |
| 40 | #include <person.h> |
| 41 | #include "delegates/pixbufdelegate.h" |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 42 | #include <media/media.h> |
| 43 | #include <media/text.h> |
| 44 | #include <media/textrecording.h> |
| 45 | #include "models/gtkqtreemodel.h" |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 46 | #include "video/videowindow.h" |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 47 | #include "ringnotify.h" |
Stepan Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 48 | #include <audio/codecmodel.h> |
| 49 | #include <account.h> |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 50 | |
| 51 | struct _CurrentCallView |
| 52 | { |
| 53 | GtkBox parent; |
| 54 | }; |
| 55 | |
| 56 | struct _CurrentCallViewClass |
| 57 | { |
| 58 | GtkBoxClass parent_class; |
| 59 | }; |
| 60 | |
| 61 | typedef struct _CurrentCallViewPrivate CurrentCallViewPrivate; |
| 62 | |
| 63 | struct _CurrentCallViewPrivate |
| 64 | { |
| 65 | GtkWidget *image_peer; |
| 66 | GtkWidget *label_identity; |
| 67 | GtkWidget *label_status; |
| 68 | GtkWidget *label_duration; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 69 | GtkWidget *frame_video; |
| 70 | GtkWidget *video_widget; |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 71 | 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 Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 77 | GtkWidget *fullscreen_window; |
Stepan Salenikovich | 77baa52 | 2015-07-07 15:29:14 -0400 | [diff] [blame] | 78 | GtkWidget *buttonbox_call_controls; |
| 79 | GtkWidget *button_hangup; |
Stepan Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 80 | 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 Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 86 | |
| 87 | Call *call; |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 88 | |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 89 | QMetaObject::Connection state_change_connection; |
| 90 | QMetaObject::Connection call_details_connection; |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 91 | QMetaObject::Connection local_renderer_connection; |
| 92 | QMetaObject::Connection remote_renderer_connection; |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 93 | QMetaObject::Connection media_added_connection; |
| 94 | QMetaObject::Connection new_message_connection; |
Stepan Salenikovich | 7b60b59 | 2015-06-16 12:29:07 -0400 | [diff] [blame] | 95 | QMetaObject::Connection incoming_msg_connection; |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | G_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 | |
| 102 | static void |
| 103 | current_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 Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 113 | QObject::disconnect(priv->local_renderer_connection); |
| 114 | QObject::disconnect(priv->remote_renderer_connection); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 115 | QObject::disconnect(priv->media_added_connection); |
| 116 | QObject::disconnect(priv->new_message_connection); |
Stepan Salenikovich | 7b60b59 | 2015-06-16 12:29:07 -0400 | [diff] [blame] | 117 | QObject::disconnect(priv->incoming_msg_connection); |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 118 | |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 119 | if (priv->fullscreen_window) { |
| 120 | gtk_widget_destroy(priv->fullscreen_window); |
| 121 | priv->fullscreen_window = NULL; |
| 122 | } |
| 123 | |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 124 | G_OBJECT_CLASS(current_call_view_parent_class)->dispose(object); |
| 125 | } |
| 126 | |
| 127 | static void |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 128 | chat_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 Salenikovich | bffaf58 | 2015-06-22 14:10:40 -0400 | [diff] [blame] | 139 | /* change focus to the chat entry */ |
| 140 | gtk_widget_grab_focus(priv->entry_chat_input); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | static void |
| 145 | send_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 Salenikovich | 6af88db | 2015-07-17 12:41:29 -0400 | [diff] [blame] | 153 | QMap<QString, QString> messages; |
| 154 | messages["text/plain"] = text; |
| 155 | priv->call->addOutgoingMedia<Media::Text>()->send(messages); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 156 | /* clear the entry */ |
| 157 | gtk_entry_set_text(GTK_ENTRY(priv->entry_chat_input), ""); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | scroll_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 Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 168 | /** |
| 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 | */ |
| 173 | static GtkScale * |
| 174 | gtk_scale_button_get_scale(GtkScaleButton *button) |
| 175 | { |
| 176 | GtkScale *scale = NULL; |
| 177 | GtkWidget *dock = gtk_scale_button_get_popup(button); |
| 178 | |
Stepan Salenikovich | 9b8f545 | 2015-07-16 10:36:07 -0400 | [diff] [blame^] | 179 | // the dock is a popover which contains a box |
Stepan Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 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))) { |
Stepan Salenikovich | 9b8f545 | 2015-07-16 10:36:07 -0400 | [diff] [blame^] | 183 | 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 Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 187 | 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 | |
| 198 | static void |
| 199 | quality_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 | |
| 222 | static gboolean |
| 223 | quality_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 | |
| 234 | static gboolean |
| 235 | quality_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 Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 249 | static void |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 250 | current_call_view_init(CurrentCallView *view) |
| 251 | { |
| 252 | gtk_widget_init_template(GTK_WIDGET(view)); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 253 | |
| 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 Salenikovich | 77baa52 | 2015-07-07 15:29:14 -0400 | [diff] [blame] | 265 | |
| 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 Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 277 | |
| 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 Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static void |
| 287 | current_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 Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 298 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, frame_video); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 299 | 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 Salenikovich | 77baa52 | 2015-07-07 15:29:14 -0400 | [diff] [blame] | 305 | 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 Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 307 | gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, scalebutton_quality); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | GtkWidget * |
| 311 | current_call_view_new(void) |
| 312 | { |
| 313 | return (GtkWidget *)g_object_new(CURRENT_CALL_VIEW_TYPE, NULL); |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | update_state(CurrentCallView *view, Call *call) |
| 318 | { |
| 319 | CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view); |
| 320 | |
Stepan Salenikovich | 7ec8fe8 | 2015-06-02 18:26:39 -0400 | [diff] [blame] | 321 | gchar *status = g_strdup_printf("%s", call->toHumanStateName().toUtf8().constData()); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 322 | |
Stepan Salenikovich | 7ec8fe8 | 2015-06-02 18:26:39 -0400 | [diff] [blame] | 323 | gtk_label_set_text(GTK_LABEL(priv->label_status), status); |
| 324 | |
| 325 | g_free(status); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static void |
| 329 | update_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 Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 338 | static void |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 339 | on_fullscreen_destroy(CurrentCallView *view) |
Stepan Salenikovich | bfe9ac6 | 2015-03-11 12:49:20 -0400 | [diff] [blame] | 340 | { |
| 341 | g_return_if_fail(IS_CURRENT_CALL_VIEW(view)); |
| 342 | CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view); |
| 343 | |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 344 | /* 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 Salenikovich | bfe9ac6 | 2015-03-11 12:49:20 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static gboolean |
| 351 | on_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 Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 359 | if (priv->fullscreen_window) { |
| 360 | /* destroy the fullscreen */ |
| 361 | gtk_widget_destroy(priv->fullscreen_window); |
Stepan Salenikovich | bfe9ac6 | 2015-03-11 12:49:20 -0400 | [diff] [blame] | 362 | } else { |
Stepan Salenikovich | b94873c | 2015-06-02 16:53:18 -0400 | [diff] [blame] | 363 | /* 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 Salenikovich | bfe9ac6 | 2015-03-11 12:49:20 -0400 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | /* the event has been fully handled */ |
| 383 | return TRUE; |
| 384 | } |
| 385 | |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 386 | static void |
| 387 | print_message_to_buffer(const QModelIndex &idx, GtkTextBuffer *buffer) |
| 388 | { |
| 389 | if (idx.isValid()) { |
Stepan Salenikovich | deae625 | 2015-06-22 12:51:52 -0400 | [diff] [blame] | 390 | auto message = idx.data().value<QString>().toUtf8(); |
| 391 | auto sender = idx.data(static_cast<int>(Media::TextRecording::Role::AuthorDisplayname)).value<QString>().toUtf8(); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 392 | |
| 393 | GtkTextIter iter; |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 394 | |
Stepan Salenikovich | deae625 | 2015-06-22 12:51:52 -0400 | [diff] [blame] | 395 | /* 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 Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 417 | } else { |
| 418 | g_warning("QModelIndex in im model is not valid"); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static void |
| 423 | parse_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 Salenikovich | deae625 | 2015-06-22 12:51:52 -0400 | [diff] [blame] | 433 | |
| 434 | /* add tags to the buffer */ |
| 435 | gtk_text_buffer_create_tag(new_buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL); |
| 436 | |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 437 | 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 Salenikovich | bfe9ac6 | 2015-03-11 12:49:20 -0400 | [diff] [blame] | 457 | |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 458 | void |
Stepan Salenikovich | 7b60b59 | 2015-06-16 12:29:07 -0400 | [diff] [blame] | 459 | monitor_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 Salenikovich | 6af88db | 2015-07-17 12:41:29 -0400 | [diff] [blame] | 469 | [priv] (G_GNUC_UNUSED const QMap<QString,QString>& m) { |
Stepan Salenikovich | 7b60b59 | 2015-06-16 12:29:07 -0400 | [diff] [blame] | 470 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->togglebutton_chat), TRUE); |
| 471 | } |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | void |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 476 | current_call_view_set_call_info(CurrentCallView *view, const QModelIndex& idx) { |
| 477 | CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view); |
| 478 | |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 479 | priv->call = CallModel::instance()->getCall(idx); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 480 | |
| 481 | /* get call image */ |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 482 | QVariant var_i = PixbufDelegate::instance()->callPhoto(priv->call, QSize(60, 60), false); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 483 | 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 Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 485 | |
| 486 | /* get name */ |
Stepan Salenikovich | 9c1f668 | 2015-03-09 16:21:28 -0400 | [diff] [blame] | 487 | QVariant var = idx.model()->data(idx, static_cast<int>(Call::Role::Name)); |
Stepan Salenikovich | 6f68707 | 2015-03-26 10:43:37 -0400 | [diff] [blame] | 488 | QByteArray ba_name = var.toString().toUtf8(); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 489 | gtk_label_set_text(GTK_LABEL(priv->label_identity), ba_name.constData()); |
| 490 | |
| 491 | /* change some things depending on call state */ |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 492 | update_state(view, priv->call); |
| 493 | update_details(view, priv->call); |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 494 | |
| 495 | priv->state_change_connection = QObject::connect( |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 496 | priv->call, |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 497 | &Call::stateChanged, |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 498 | [view, priv]() { update_state(view, priv->call); } |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 499 | ); |
| 500 | |
| 501 | priv->call_details_connection = QObject::connect( |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 502 | priv->call, |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 503 | static_cast<void (Call::*)(void)>(&Call::changed), |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 504 | [view, priv]() { update_details(view, priv->call); } |
Stepan Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 505 | ); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 506 | |
| 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 Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 513 | video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget), |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 514 | priv->call->videoRenderer(), |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 515 | VIDEO_RENDERER_REMOTE); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 516 | |
Stepan Salenikovich | 8e5c9d0 | 2015-03-11 14:07:10 -0400 | [diff] [blame] | 517 | /* callback for remote renderer */ |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 518 | priv->remote_renderer_connection = QObject::connect( |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 519 | priv->call, |
Stepan Salenikovich | 9c1f668 | 2015-03-09 16:21:28 -0400 | [diff] [blame] | 520 | &Call::videoStarted, |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 521 | [priv](Video::Renderer *renderer) { |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 522 | video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget), |
| 523 | renderer, |
| 524 | VIDEO_RENDERER_REMOTE); |
Stepan Salenikovich | 36c025c | 2015-03-03 19:06:44 -0500 | [diff] [blame] | 525 | } |
| 526 | ); |
Stepan Salenikovich | 4ac89f1 | 2015-03-10 16:48:47 -0400 | [diff] [blame] | 527 | |
Stepan Salenikovich | 8e5c9d0 | 2015-03-11 14:07:10 -0400 | [diff] [blame] | 528 | /* local renderer */ |
Stepan Salenikovich | 5705880 | 2015-03-25 14:16:13 -0400 | [diff] [blame] | 529 | if (Video::PreviewManager::instance()->isPreviewing()) |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 530 | video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget), |
| 531 | Video::PreviewManager::instance()->previewRenderer(), |
| 532 | VIDEO_RENDERER_LOCAL); |
Stepan Salenikovich | 8e5c9d0 | 2015-03-11 14:07:10 -0400 | [diff] [blame] | 533 | |
| 534 | /* callback for local renderer */ |
Stepan Salenikovich | c5f0815 | 2015-03-19 00:53:23 -0400 | [diff] [blame] | 535 | priv->local_renderer_connection = QObject::connect( |
Stepan Salenikovich | 8e5c9d0 | 2015-03-11 14:07:10 -0400 | [diff] [blame] | 536 | Video::PreviewManager::instance(), |
| 537 | &Video::PreviewManager::previewStarted, |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 538 | [priv](Video::Renderer *renderer) { |
Stepan Salenikovich | 0f69323 | 2015-04-22 10:45:08 -0400 | [diff] [blame] | 539 | video_widget_push_new_renderer(VIDEO_WIDGET(priv->video_widget), |
| 540 | renderer, |
| 541 | VIDEO_RENDERER_LOCAL); |
Stepan Salenikovich | 8e5c9d0 | 2015-03-11 14:07:10 -0400 | [diff] [blame] | 542 | } |
| 543 | ); |
Stepan Salenikovich | bfe9ac6 | 2015-03-11 12:49:20 -0400 | [diff] [blame] | 544 | |
| 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 Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 549 | |
| 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 Salenikovich | 7b60b59 | 2015-06-16 12:29:07 -0400 | [diff] [blame] | 554 | monitor_incoming_message(view, text); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 555 | } 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 Salenikovich | 7b60b59 | 2015-06-16 12:29:07 -0400 | [diff] [blame] | 558 | monitor_incoming_message(view, text); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 559 | } 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 Salenikovich | 7b60b59 | 2015-06-16 12:29:07 -0400 | [diff] [blame] | 567 | monitor_incoming_message(view, (Media::Text*)media); |
Stepan Salenikovich | a448f60 | 2015-05-29 13:33:06 -0400 | [diff] [blame] | 568 | QObject::disconnect(priv->media_added_connection); |
| 569 | } |
| 570 | } |
| 571 | ); |
| 572 | } |
Stepan Salenikovich | 67112d1 | 2015-06-16 16:57:06 -0400 | [diff] [blame] | 573 | |
| 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 Salenikovich | f6f4265 | 2015-07-15 12:46:14 -0400 | [diff] [blame] | 577 | |
| 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 Salenikovich | c64523b | 2015-02-27 16:31:00 -0500 | [diff] [blame] | 593 | } |