blob: bc6db5714f91c64b7c69d78a40c6a9fdaaf12b65 [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 Salenikovichc64523b2015-02-27 16:31:00 -050042
43struct _CurrentCallView
44{
45 GtkBox parent;
46};
47
48struct _CurrentCallViewClass
49{
50 GtkBoxClass parent_class;
51};
52
53typedef struct _CurrentCallViewPrivate CurrentCallViewPrivate;
54
55struct _CurrentCallViewPrivate
56{
57 GtkWidget *image_peer;
58 GtkWidget *label_identity;
59 GtkWidget *label_status;
60 GtkWidget *label_duration;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050061 GtkWidget *frame_video;
62 GtkWidget *video_widget;
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -040063 GtkWidget *button_hangup;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -050064
Stepan Salenikovichc5f08152015-03-19 00:53:23 -040065 /* new renderers should be put into the queue for processing by a g_idle
66 * functions whose id should be saved into renderer_idle_source;
67 * this way when the CurrentCallView object is destroyed, we do not try
68 * to process any new renderers by stoping the g_idle function.
69 */
70 guint renderer_idle_source;
71 GAsyncQueue *new_renderer_queue;
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050072
73 QMetaObject::Connection state_change_connection;
74 QMetaObject::Connection call_details_connection;
Stepan Salenikovichc5f08152015-03-19 00:53:23 -040075 QMetaObject::Connection local_renderer_connection;
76 QMetaObject::Connection remote_renderer_connection;
Stepan Salenikovichc64523b2015-02-27 16:31:00 -050077};
78
79G_DEFINE_TYPE_WITH_PRIVATE(CurrentCallView, current_call_view, GTK_TYPE_BOX);
80
81#define CURRENT_CALL_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CURRENT_CALL_VIEW_TYPE, CurrentCallViewPrivate))
82
83static void
84current_call_view_dispose(GObject *object)
85{
86 CurrentCallView *view;
87 CurrentCallViewPrivate *priv;
88
89 view = CURRENT_CALL_VIEW(object);
90 priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
91
92 QObject::disconnect(priv->state_change_connection);
93 QObject::disconnect(priv->call_details_connection);
Stepan Salenikovichc5f08152015-03-19 00:53:23 -040094 QObject::disconnect(priv->local_renderer_connection);
95 QObject::disconnect(priv->remote_renderer_connection);
96
97 /* dispose may be called multiple times, make sure
98 * not to call g_source_remove more than once */
99 if (priv->renderer_idle_source) {
100 g_source_remove(priv->renderer_idle_source);
101 priv->renderer_idle_source = 0;
102 }
103
104 if (priv->new_renderer_queue) {
105 g_async_queue_unref(priv->new_renderer_queue);
106 priv->new_renderer_queue = NULL;
107 }
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500108
109 G_OBJECT_CLASS(current_call_view_parent_class)->dispose(object);
110}
111
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400112static gboolean
113check_renderer_queue(CurrentCallView *self)
114{
115 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(self), FALSE);
116 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
117
118 /* get all the renderers in the queue */
119 gpointer new_video_renderer = g_async_queue_try_pop(priv->new_renderer_queue);
120 while (new_video_renderer) {
121 video_widget_add_renderer(VIDEO_WIDGET(priv->video_widget), (const VideoRenderer *)new_video_renderer);
122 g_free(new_video_renderer);
123 new_video_renderer = g_async_queue_try_pop(priv->new_renderer_queue);
124 }
125
126 return TRUE; /* keep going */
127}
128
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500129static void
130current_call_view_init(CurrentCallView *view)
131{
132 gtk_widget_init_template(GTK_WIDGET(view));
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400133
134 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
135
136 /* init new renderer queue */
137 priv->new_renderer_queue = g_async_queue_new_full((GDestroyNotify)g_free);
Stepan Salenikovich213fd912015-03-26 15:07:57 -0400138 /* check new render every 30 ms (30ms is "fast enough");
139 * we don't use an idle function so it doesn't consume cpu needlessly */
140 priv->renderer_idle_source = g_timeout_add_full(
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400141 G_PRIORITY_DEFAULT_IDLE,
Stepan Salenikovich213fd912015-03-26 15:07:57 -0400142 30,
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400143 (GSourceFunc)check_renderer_queue,
144 view,
145 NULL);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500146}
147
148static void
149current_call_view_class_init(CurrentCallViewClass *klass)
150{
151 G_OBJECT_CLASS(klass)->dispose = current_call_view_dispose;
152
153 gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS (klass),
154 "/cx/ring/RingGnome/currentcallview.ui");
155
156 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, image_peer);
157 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_identity);
158 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_status);
159 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, label_duration);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500160 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, frame_video);
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400161 gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), CurrentCallView, button_hangup);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500162}
163
164GtkWidget *
165current_call_view_new(void)
166{
167 return (GtkWidget *)g_object_new(CURRENT_CALL_VIEW_TYPE, NULL);
168}
169
170static void
171update_state(CurrentCallView *view, Call *call)
172{
173 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
174
175 /* change state label */
176 Call::State state = call->state();
177
178 switch(state) {
Stepan Salenikovich46e3e992015-04-08 11:42:13 -0400179 case Call::State::NEW:
180 gtk_label_set_text(GTK_LABEL(priv->label_status), "New.");
181 break;
182 case Call::State::ABORTED:
183 gtk_label_set_text(GTK_LABEL(priv->label_status), "Aborted.");
184 break;
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500185 case Call::State::INCOMING:
186 gtk_label_set_text(GTK_LABEL(priv->label_status), "Incoming...");
187 break;
188 case Call::State::RINGING:
189 gtk_label_set_text(GTK_LABEL(priv->label_status), "Ringing...");
190 break;
191 case Call::State::CURRENT:
192 /* note: shouldn't be displayed, as the view should change */
193 gtk_label_set_text(GTK_LABEL(priv->label_status), "In progress.");
194 break;
195 case Call::State::DIALING:
196 gtk_label_set_text(GTK_LABEL(priv->label_status), "Dialing...");
197 break;
198 case Call::State::HOLD:
199 /* note: shouldn't be displayed, as the view should change */
200 gtk_label_set_text(GTK_LABEL(priv->label_status), "On hold.");
201 break;
202 case Call::State::FAILURE:
203 gtk_label_set_text(GTK_LABEL(priv->label_status), "Failed.");
204 break;
205 case Call::State::BUSY:
206 gtk_label_set_text(GTK_LABEL(priv->label_status), "Busy.");
207 break;
208 case Call::State::TRANSFERRED:
209 /* note: shouldn't be displayed, as the view should change */
210 gtk_label_set_text(GTK_LABEL(priv->label_status), "Transfered.");
211 break;
212 case Call::State::TRANSF_HOLD:
213 /* note: shouldn't be displayed, as the view should change */
214 gtk_label_set_text(GTK_LABEL(priv->label_status), "Transfer hold.");
215 break;
216 case Call::State::OVER:
217 /* note: shouldn't be displayed, as the view should change */
218 gtk_label_set_text(GTK_LABEL(priv->label_status), "Over.");
219 break;
220 case Call::State::ERROR:
221 gtk_label_set_text(GTK_LABEL(priv->label_status), "Error.");
222 break;
223 case Call::State::CONFERENCE:
224 /* note: shouldn't be displayed, as the view should change */
225 gtk_label_set_text(GTK_LABEL(priv->label_status), "Conference.");
226 break;
227 case Call::State::CONFERENCE_HOLD:
228 /* note: shouldn't be displayed, as the view should change */
229 gtk_label_set_text(GTK_LABEL(priv->label_status), "Conference hold.");
230 break;
231 case Call::State::INITIALIZATION:
232 gtk_label_set_text(GTK_LABEL(priv->label_status), "Initialization...");
233 break;
234 case Call::State::COUNT__:
235 break;
236 }
237}
238
239static void
240update_details(CurrentCallView *view, Call *call)
241{
242 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
243
244 /* update call duration */
245 QByteArray ba_length = call->length().toLocal8Bit();
246 gtk_label_set_text(GTK_LABEL(priv->label_duration), ba_length.constData());
247}
248
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400249static void
250push_new_renderer(CurrentCallView *self, Video::Renderer *renderer, VideoRendererType type)
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500251{
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400252 g_return_if_fail(IS_CURRENT_CALL_VIEW(self));
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500253
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400254 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(self);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500255
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400256 VideoRenderer *new_video_renderer = g_new0(VideoRenderer, 1);
257 new_video_renderer->renderer = renderer;
258 new_video_renderer->type = type;
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500259
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400260 g_async_queue_push(priv->new_renderer_queue, new_video_renderer);
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400261}
262
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400263static void
264fullscreen_destroy(CurrentCallView *view)
265{
266 g_return_if_fail(IS_CURRENT_CALL_VIEW(view));
267 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
268
269 /* check if the video widgets parent is the the fullscreen window */
270 GtkWidget *parent = gtk_widget_get_parent(priv->video_widget);
271 if (parent != NULL && parent != priv->frame_video) {
272 /* put the videw widget back in the call view */
273 g_object_ref(priv->video_widget);
274 gtk_container_remove(GTK_CONTAINER(parent), priv->video_widget);
275 gtk_container_add(GTK_CONTAINER(priv->frame_video), priv->video_widget);
276 g_object_unref(priv->video_widget);
277 /* destroy the fullscreen window */
278 gtk_widget_destroy(parent);
279 }
280}
281
282static gboolean
283fullscreen_handle_keys(GtkWidget *self, GdkEventKey *event, G_GNUC_UNUSED gpointer user_data)
284{
285 if (event->keyval == GDK_KEY_Escape)
286 gtk_widget_destroy(self);
287
288 /* the event has been fully handled */
289 return TRUE;
290}
291
292static gboolean
293on_button_press_in_video_event(GtkWidget *self, GdkEventButton *event, CurrentCallView *view)
294{
295 g_return_val_if_fail(IS_VIDEO_WIDGET(self), FALSE);
296 g_return_val_if_fail(IS_CURRENT_CALL_VIEW(view), FALSE);
297 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
298
299 /* on double click */
300 if (event->type == GDK_2BUTTON_PRESS) {
301
302 /* get the parent to check if its in fullscreen window or not */
303 GtkWidget *parent = gtk_widget_get_parent(GTK_WIDGET(self));
304 if (parent == priv->frame_video){
305 /* not fullscreen, so put it in a separate widget and make it so */
306 GtkWidget *fullscreen_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
307 gtk_window_set_decorated(GTK_WINDOW(fullscreen_window), FALSE);
308 gtk_window_set_transient_for(GTK_WINDOW(fullscreen_window),
309 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view))));
310 g_object_ref(self);
311 gtk_container_remove(GTK_CONTAINER(priv->frame_video), self);
312 gtk_container_add(GTK_CONTAINER(fullscreen_window), self);
313 g_object_unref(self);
314 /* connect signals to make sure we can un-fullscreen */
315 g_signal_connect_swapped(fullscreen_window, "destroy", G_CALLBACK(fullscreen_destroy), view);
316 g_signal_connect(view, "destroy", G_CALLBACK(fullscreen_destroy), NULL);
317 g_signal_connect(fullscreen_window, "key_press_event", G_CALLBACK(fullscreen_handle_keys), NULL);
318 /* present the fullscreen widnow */
319 gtk_window_present(GTK_WINDOW(fullscreen_window));
320 gtk_window_fullscreen(GTK_WINDOW(fullscreen_window));
321 } else {
322 /* put it back in the call view */
323 fullscreen_destroy(view);
324 }
325 }
326
327 /* the event has been fully handled */
328 return TRUE;
329}
330
331
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500332void
333current_call_view_set_call_info(CurrentCallView *view, const QModelIndex& idx) {
334 CurrentCallViewPrivate *priv = CURRENT_CALL_VIEW_GET_PRIVATE(view);
335
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400336 Call *call = CallModel::instance()->getCall(idx);
337
338 /* get call image */
339 QVariant var_i = PixbufDelegate::instance()->callPhoto(call, QSize(60, 60), false);
340 std::shared_ptr<GdkPixbuf> image = var_i.value<std::shared_ptr<GdkPixbuf>>();
341 gtk_image_set_from_pixbuf(GTK_IMAGE(priv->image_peer), image.get());
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500342
343 /* get name */
Stepan Salenikovich9c1f6682015-03-09 16:21:28 -0400344 QVariant var = idx.model()->data(idx, static_cast<int>(Call::Role::Name));
Stepan Salenikovich6f687072015-03-26 10:43:37 -0400345 QByteArray ba_name = var.toString().toUtf8();
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500346 gtk_label_set_text(GTK_LABEL(priv->label_identity), ba_name.constData());
347
348 /* change some things depending on call state */
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500349 update_state(view, call);
350 update_details(view, call);
351
352 priv->state_change_connection = QObject::connect(
353 call,
354 &Call::stateChanged,
355 [=]() { update_state(view, call); }
356 );
357
358 priv->call_details_connection = QObject::connect(
359 call,
360 static_cast<void (Call::*)(void)>(&Call::changed),
361 [=]() { update_details(view, call); }
362 );
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500363
364 /* video widget */
365 priv->video_widget = video_widget_new();
366 gtk_container_add(GTK_CONTAINER(priv->frame_video), priv->video_widget);
367 gtk_widget_show_all(priv->frame_video);
368
369 /* check if we already have a renderer */
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400370 push_new_renderer(view, call->videoRenderer(), VIDEO_RENDERER_REMOTE);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500371
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400372 /* callback for remote renderer */
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400373 priv->remote_renderer_connection = QObject::connect(
Stepan Salenikovich9c1f6682015-03-09 16:21:28 -0400374 call,
375 &Call::videoStarted,
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500376 [=](Video::Renderer *renderer) {
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400377 push_new_renderer(view, renderer, VIDEO_RENDERER_REMOTE);
Stepan Salenikovich36c025c2015-03-03 19:06:44 -0500378 }
379 );
Stepan Salenikovich4ac89f12015-03-10 16:48:47 -0400380
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400381 /* local renderer */
Stepan Salenikovich57058802015-03-25 14:16:13 -0400382 if (Video::PreviewManager::instance()->isPreviewing())
383 push_new_renderer(view, Video::PreviewManager::instance()->previewRenderer(), VIDEO_RENDERER_LOCAL);
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400384
385 /* callback for local renderer */
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400386 priv->local_renderer_connection = QObject::connect(
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400387 Video::PreviewManager::instance(),
388 &Video::PreviewManager::previewStarted,
389 [=](Video::Renderer *renderer) {
Stepan Salenikovichc5f08152015-03-19 00:53:23 -0400390 push_new_renderer(view, renderer, VIDEO_RENDERER_LOCAL);
Stepan Salenikovich8e5c9d02015-03-11 14:07:10 -0400391 }
392 );
Stepan Salenikovichbfe9ac62015-03-11 12:49:20 -0400393
394 /* catch double click to make full screen */
395 g_signal_connect(priv->video_widget, "button-press-event",
396 G_CALLBACK(on_button_press_in_video_event),
397 view);
Stepan Salenikovichc64523b2015-02-27 16:31:00 -0500398}