[calls] ringer: Refactor to keep track of ongoing calls



commit fd7193a525456172e2f0bdc3b128264476296ade
Author: Evangelos Ribeiro Tzaras <devrtz fortysixandtwo eu>
Date:   Thu Oct 14 11:41:18 2021 +0200

    ringer: Refactor to keep track of ongoing calls
    
    This makes it easier to silence a call.

 src/calls-ringer.c | 112 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 63 insertions(+), 49 deletions(-)
---
diff --git a/src/calls-ringer.c b/src/calls-ringer.c
index 9657ba84..7d2928b4 100644
--- a/src/calls-ringer.c
+++ b/src/calls-ringer.c
@@ -37,13 +37,9 @@
 struct _CallsRinger {
   GObject parent_instance;
 
-  /* call_count keeps track of total ongoing calls.
-   * ring_count keeps track of ringing calls.
-   */
-  unsigned  call_count;
-  unsigned  ring_count;
-  gboolean  playing;
+  GList *calls;
   LfbEvent *event;
+  gboolean playing;
 };
 
 G_DEFINE_TYPE (CallsRinger, calls_ringer, G_TYPE_OBJECT);
@@ -67,12 +63,13 @@ on_event_triggered (LfbEvent     *event,
 }
 
 static void
-start (CallsRinger *self)
+start (CallsRinger *self,
+       gboolean     quiet)
 {
   g_return_if_fail (self->playing == FALSE);
 
   if (self->event) {
-    if (self->call_count > self->ring_count)
+    if (quiet)
       lfb_event_set_feedback_profile (self->event, "quiet");
 
     g_object_ref (self);
@@ -83,6 +80,7 @@ start (CallsRinger *self)
   }
 }
 
+
 static void
 on_event_feedback_ended (LfbEvent     *event,
                          GAsyncResult *res,
@@ -100,6 +98,7 @@ on_event_feedback_ended (LfbEvent     *event,
                  lfb_event_get_event (event), err->message);
 }
 
+
 static void
 on_feedback_ended (LfbEvent    *event,
                    CallsRinger *self)
@@ -108,6 +107,7 @@ on_feedback_ended (LfbEvent    *event,
   self->playing = FALSE;
 }
 
+
 static void
 stop (CallsRinger *self)
 {
@@ -119,29 +119,27 @@ stop (CallsRinger *self)
 }
 
 
-static void
-update_ring (CallsRinger *self)
+static inline gboolean
+is_ring_state (CallsCallState state)
 {
-  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);
-    }
+  switch (state) {
+  case CALLS_CALL_STATE_INCOMING:
+  case CALLS_CALL_STATE_WAITING:
+    return TRUE;
+  default:
+    return FALSE;
   }
 }
 
 
 static inline gboolean
-is_ring_state (CallsCallState state)
+is_active_state (CallsCallState state)
 {
   switch (state) {
-  case CALLS_CALL_STATE_INCOMING:
-  case CALLS_CALL_STATE_WAITING:
+  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;
@@ -149,53 +147,67 @@ is_ring_state (CallsCallState state)
 }
 
 
-static void
-state_changed_cb (CallsRinger   *self,
-                  CallsCallState new_state,
-                  CallsCallState old_state)
+static gboolean
+has_active_call (CallsRinger *self)
 {
-  gboolean old_is_ring;
+  g_assert (CALLS_IS_RINGER (self));
 
-  g_return_if_fail (old_state != new_state);
+  for (GList *node = self->calls; node != NULL; node = node->next) {
+    CallsCall *call = node->data;
 
-  old_is_ring = is_ring_state (old_state);
-  if (old_is_ring == is_ring_state (new_state))
-    // No change in ring state
-    return;
+    if (is_active_state (calls_call_get_state (call)))
+      return TRUE;
+  }
+  return FALSE;
+}
 
-  if (old_is_ring)
-    --self->ring_count;
-  else
-    ++self->ring_count;
 
-  update_ring (self);
+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
-update_count (CallsRinger    *self,
-              CallsCall      *call,
-              short           delta)
+update_ring (CallsRinger *self)
 {
-  self->call_count += delta;
+  g_assert (CALLS_IS_RINGER (self));
 
-  if (is_ring_state (calls_call_get_state (call)))
-    self->ring_count += delta;
+  if (!self->event) {
+    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
 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,
                             "state-changed",
-                            G_CALLBACK (state_changed_cb),
+                            G_CALLBACK (update_ring),
                             self);
+  update_ring (self);
 }
 
 
@@ -203,9 +215,11 @@ static void
 call_removed_cb (CallsRinger *self,
                  CallsCall *call)
 {
-  update_count (self, call, -1);
+  self->calls = g_list_remove (self->calls, call);
 
   g_signal_handlers_disconnect_by_data (call, self);
+
+  update_ring (self);
 }
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]