1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2025-01-05 19:15:32 +00:00

ringer: Refactor to keep track of ongoing calls

This makes it easier to silence a call.
This commit is contained in:
Evangelos Ribeiro Tzaras 2021-10-14 11:41:18 +02:00
parent 17a2c94387
commit fd7193a525

View file

@ -37,13 +37,9 @@
struct _CallsRinger { struct _CallsRinger {
GObject parent_instance; GObject parent_instance;
/* call_count keeps track of total ongoing calls. GList *calls;
* ring_count keeps track of ringing calls.
*/
unsigned call_count;
unsigned ring_count;
gboolean playing;
LfbEvent *event; LfbEvent *event;
gboolean playing;
}; };
G_DEFINE_TYPE (CallsRinger, calls_ringer, G_TYPE_OBJECT); G_DEFINE_TYPE (CallsRinger, calls_ringer, G_TYPE_OBJECT);
@ -67,12 +63,13 @@ on_event_triggered (LfbEvent *event,
} }
static void static void
start (CallsRinger *self) start (CallsRinger *self,
gboolean quiet)
{ {
g_return_if_fail (self->playing == FALSE); g_return_if_fail (self->playing == FALSE);
if (self->event) { if (self->event) {
if (self->call_count > self->ring_count) if (quiet)
lfb_event_set_feedback_profile (self->event, "quiet"); lfb_event_set_feedback_profile (self->event, "quiet");
g_object_ref (self); g_object_ref (self);
@ -83,6 +80,7 @@ start (CallsRinger *self)
} }
} }
static void static void
on_event_feedback_ended (LfbEvent *event, on_event_feedback_ended (LfbEvent *event,
GAsyncResult *res, GAsyncResult *res,
@ -100,6 +98,7 @@ on_event_feedback_ended (LfbEvent *event,
lfb_event_get_event (event), err->message); lfb_event_get_event (event), err->message);
} }
static void static void
on_feedback_ended (LfbEvent *event, on_feedback_ended (LfbEvent *event,
CallsRinger *self) CallsRinger *self)
@ -108,6 +107,7 @@ on_feedback_ended (LfbEvent *event,
self->playing = FALSE; self->playing = FALSE;
} }
static void static void
stop (CallsRinger *self) stop (CallsRinger *self)
{ {
@ -119,23 +119,6 @@ stop (CallsRinger *self)
} }
static void
update_ring (CallsRinger *self)
{
if (!self->playing) {
if (self->ring_count > 0) {
g_debug ("Starting ringer");
start (self);
}
} else {
if (self->ring_count == 0) {
g_debug ("Stopping ringer");
stop (self);
}
}
}
static inline gboolean static inline gboolean
is_ring_state (CallsCallState state) is_ring_state (CallsCallState state)
{ {
@ -149,53 +132,82 @@ is_ring_state (CallsCallState state)
} }
static void static inline gboolean
state_changed_cb (CallsRinger *self, is_active_state (CallsCallState state)
CallsCallState new_state,
CallsCallState old_state)
{ {
gboolean old_is_ring; switch (state) {
case CALLS_CALL_STATE_ACTIVE:
case CALLS_CALL_STATE_DIALING:
case CALLS_CALL_STATE_ALERTING:
case CALLS_CALL_STATE_HELD:
return TRUE;
default:
return FALSE;
}
}
g_return_if_fail (old_state != new_state);
old_is_ring = is_ring_state (old_state); static gboolean
if (old_is_ring == is_ring_state (new_state)) has_active_call (CallsRinger *self)
// No change in ring state {
return; g_assert (CALLS_IS_RINGER (self));
if (old_is_ring) for (GList *node = self->calls; node != NULL; node = node->next) {
--self->ring_count; CallsCall *call = node->data;
else
++self->ring_count;
update_ring (self); if (is_active_state (calls_call_get_state (call)))
return TRUE;
}
return FALSE;
}
static gboolean
has_incoming_call (CallsRinger *self)
{
g_assert (CALLS_IS_RINGER (self));
for (GList *node = self->calls; node != NULL; node = node->next) {
CallsCall *call = node->data;
if (is_ring_state (calls_call_get_state (call)))
return TRUE;
}
return FALSE;
} }
static void static void
update_count (CallsRinger *self, update_ring (CallsRinger *self)
CallsCall *call,
short delta)
{ {
self->call_count += delta; g_assert (CALLS_IS_RINGER (self));
if (is_ring_state (calls_call_get_state (call))) if (!self->event) {
self->ring_count += delta; g_debug ("Can't ring because libfeedback is not initialized");
return;
}
update_ring (self); if (has_incoming_call (self))
start (self, has_active_call (self));
else
stop (self);
} }
static void static void
call_added_cb (CallsRinger *self, call_added_cb (CallsRinger *self,
CallsCall *call) CallsCall *call)
{ {
update_count (self, call, +1); g_assert (CALLS_IS_RINGER (self));
g_assert (CALLS_IS_CALL (call));
self->calls = g_list_append (self->calls, call);
g_signal_connect_swapped (call, g_signal_connect_swapped (call,
"state-changed", "state-changed",
G_CALLBACK (state_changed_cb), G_CALLBACK (update_ring),
self); self);
update_ring (self);
} }
@ -203,9 +215,11 @@ static void
call_removed_cb (CallsRinger *self, call_removed_cb (CallsRinger *self,
CallsCall *call) CallsCall *call)
{ {
update_count (self, call, -1); self->calls = g_list_remove (self->calls, call);
g_signal_handlers_disconnect_by_data (call, self); g_signal_handlers_disconnect_by_data (call, self);
update_ring (self);
} }