currentcallview: reactivate the quality slider

When switching to new models from the LRC, the auto-quality was disabled
because not implemented. But now, the video quality is really bad. So we
can use the old models to re-enable this functionnality for now, and: th
will replace this when we will replace the video manager.

Note, the code was taken in commit fad674f560cb9a07902b4058ba0c938299ae036c

Change-Id: Ibeb950afbe56f66a904266a2042cd9af39b2b3cb
Tuleap: #1759
Reviewed-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
diff --git a/src/currentcallview.cpp b/src/currentcallview.cpp
index 3256032..3850bac 100644
--- a/src/currentcallview.cpp
+++ b/src/currentcallview.cpp
@@ -30,6 +30,7 @@
 #include <api/contact.h>
 #include <api/contactmodel.h>
 #include <api/newcallmodel.h>
+#include <callmodel.h>
 #include <codecmodel.h>
 #include <globalinstances.h>
 #include <smartinfohub.h>
@@ -334,6 +335,47 @@
 }
 
 static void
+set_quality(Call *call, gboolean auto_quality_on, double desired_quality)
+{
+    /* set auto quality true or false, also set the bitrate and quality values;
+     * the slider is from 0 to 100, use the min and max vals to scale each value accordingly */
+    if (const auto& codecModel = call->account()->codecModel()) {
+        const auto& videoCodecs = codecModel->videoCodecs();
+
+        for (int i=0; i < videoCodecs->rowCount();i++) {
+            const auto& idx = videoCodecs->index(i,0);
+
+            if (auto_quality_on) {
+                // g_debug("enable auto quality");
+                videoCodecs->setData(idx, "true", CodecModel::Role::AUTO_QUALITY_ENABLED);
+            } else {
+                auto min_bitrate = idx.data(static_cast<int>(CodecModel::Role::MIN_BITRATE)).toInt();
+                auto max_bitrate = idx.data(static_cast<int>(CodecModel::Role::MAX_BITRATE)).toInt();
+                auto min_quality = idx.data(static_cast<int>(CodecModel::Role::MIN_QUALITY)).toInt();
+                auto max_quality = idx.data(static_cast<int>(CodecModel::Role::MAX_QUALITY)).toInt();
+
+                // g_debug("bitrate min: %d, max: %d, quality min: %d, max: %d", min_bitrate, max_bitrate, min_quality, max_quality);
+
+                double bitrate;
+                bitrate = min_bitrate + (double)(max_bitrate - min_bitrate)*(desired_quality/100.0);
+                if (bitrate < 0) bitrate = 0;
+
+                double quality;
+                // note: a lower value means higher quality
+                quality = (double)min_quality - (min_quality - max_quality)*(desired_quality/100.0);
+                if (quality < 0) quality = 0;
+
+                // g_debug("disable auto quality; %% quality: %d; bitrate: %d; quality: %d", (int)desired_quality, (int)bitrate, (int)quality);
+                videoCodecs->setData(idx, "false", CodecModel::Role::AUTO_QUALITY_ENABLED);
+                videoCodecs->setData(idx, QString::number((int)bitrate), CodecModel::Role::BITRATE);
+                videoCodecs->setData(idx, QString::number((int)quality), CodecModel::Role::QUALITY);
+            }
+        }
+        codecModel << CodecModel::EditAction::SAVE;
+    }
+}
+
+static void
 autoquality_toggled(GtkToggleButton *button, CurrentCallView *self)
 {
     // TODO
@@ -349,6 +391,16 @@
     gtk_widget_set_sensitive(GTK_WIDGET(scale), !auto_quality_on);
     gtk_widget_set_sensitive(plus_button, !auto_quality_on);
     gtk_widget_set_sensitive(minus_button, !auto_quality_on);
+
+    double desired_quality = gtk_scale_button_get_value(GTK_SCALE_BUTTON(priv->scalebutton_quality));
+
+    auto callToRender = priv->conversation_->callId;
+    if (!priv->conversation_->confId.empty())
+        callToRender = priv->conversation_->confId;
+    auto renderer = priv->accountContainer_->info.callModel->getRenderer(callToRender);
+    for (const auto& activeCall: CallModel::instance().getActiveCalls())
+        if (activeCall->videoRenderer() == renderer)
+            set_quality(activeCall, auto_quality_on, desired_quality);
 }
 
 static void
@@ -363,6 +415,14 @@
 
     /* only update if the scale button is released, to reduce the number of updates */
     if (priv->quality_scale_pressed) return;
+
+    auto callToRender = priv->conversation_->callId;
+    if (!priv->conversation_->confId.empty())
+        callToRender = priv->conversation_->confId;
+    auto renderer = priv->accountContainer_->info.callModel->getRenderer(callToRender);
+    for (const auto& activeCall: CallModel::instance().getActiveCalls())
+        if (activeCall->videoRenderer() == renderer)
+            set_quality(activeCall, FALSE, gtk_scale_button_get_value(button));
 }
 
 static gboolean
@@ -542,6 +602,33 @@
         g_signal_connect(scale, "button-release-event", G_CALLBACK(quality_button_released), view);
     }
 
+    /* by this time we should have the call already set, but we check to make sure */
+    auto callToRender = priv->conversation_->callId;
+    if (!priv->conversation_->confId.empty())
+        callToRender = priv->conversation_->confId;
+    auto renderer = priv->accountContainer_->info.callModel->getRenderer(callToRender);
+    for (const auto& activeCall: CallModel::instance().getActiveCalls())
+        if (activeCall->videoRenderer() == renderer) {
+            /* check if auto quality is enabled or not */
+            if (const auto& codecModel = activeCall->account()->codecModel()) {
+                const auto& videoCodecs = codecModel->videoCodecs();
+                if (videoCodecs->rowCount() > 0) {
+                    /* we only need to check the first codec since by default it is ON for all, and the
+                     * gnome client sets its ON or OFF for all codecs as well */
+                    const auto& idx = videoCodecs->index(0,0);
+                    auto auto_quality_enabled = idx.data(static_cast<int>(CodecModel::Role::AUTO_QUALITY_ENABLED)).toString() == "true";
+                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->checkbutton_autoquality), auto_quality_enabled);
+
+                    // TODO: save the manual quality setting in the client and set the slider to that value here;
+                    //       the daemon resets the bitrate/quality between each call, and the default may be
+                    //       different for each codec, so there is no reason to check it here
+                }
+            }
+        } else {
+            /* Auto-quality is off by default */
+            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->checkbutton_autoquality), FALSE);
+        }
+
     // Get if the user wants to show the smartInfo box
     auto display_smartinfo = g_action_map_lookup_action(G_ACTION_MAP(g_application_get_default()), "display-smartinfo");
     if (g_variant_get_boolean(g_action_get_state(G_ACTION(display_smartinfo)))) {