Blame SOURCES/cheese-3.38.0-vala-genericarray.patch

bc6059
From 7cf6268e54620bbbe5e6e61800c50fb0cb4bea57 Mon Sep 17 00:00:00 2001
bc6059
From: =?UTF-8?q?Corentin=20No=C3=ABl?= <corentin@elementary.io>
bc6059
Date: Fri, 16 Oct 2020 19:56:26 +0200
bc6059
Subject: [PATCH] Change GLib.PtrArray into GLib.GenericArray
bc6059
bc6059
This is the vala-friendly way of handling GPtrArray.
bc6059
Fix several memory leaks on the go and unnecessary reference increase.
bc6059
---
bc6059
 src/cheese-preferences.vala | 26 ++++++++++++--------------
bc6059
 src/cheese-window.vala      | 22 +++++++++++-----------
bc6059
 src/vapi/cheese-common.vapi |  2 +-
bc6059
 3 files changed, 24 insertions(+), 26 deletions(-)
bc6059
bc6059
diff --git a/src/cheese-preferences.vala b/src/cheese-preferences.vala
bc6059
index f56af7e0..80a92431 100644
bc6059
--- a/src/cheese-preferences.vala
bc6059
+++ b/src/cheese-preferences.vala
bc6059
@@ -100,7 +100,7 @@ public PreferencesDialog (Cheese.Camera camera)
bc6059
    */
bc6059
   private void initialize_camera_devices ()
bc6059
   {
bc6059
-    unowned GLib.PtrArray devices = camera.get_camera_devices ();
bc6059
+    GLib.GenericArray<unowned Cheese.CameraDevice> devices = camera.get_camera_devices ();
bc6059
     camera_model = new Gtk.ListStore (2, typeof (string), typeof (Cheese.CameraDevice));
bc6059
 
bc6059
     source_combo.model = camera_model;
bc6059
@@ -357,13 +357,13 @@ public PreferencesDialog (Cheese.Camera camera)
bc6059
    */
bc6059
   private void on_camera_update_num_camera_devices ()
bc6059
   {
bc6059
-    unowned GLib.PtrArray devices = camera.get_camera_devices ();
bc6059
-    Cheese.CameraDevice   dev;
bc6059
+    GLib.GenericArray<unowned Cheese.CameraDevice> devices = camera.get_camera_devices ();
bc6059
+    unowned Cheese.CameraDevice   dev;
bc6059
 
bc6059
     // Add (if) / Remove (else) a camera device.
bc6059
-    if (devices.len > camera_model.iter_n_children (null))
bc6059
+    if (devices.length > camera_model.iter_n_children (null))
bc6059
     {
bc6059
-      dev = (Cheese.CameraDevice) devices.index (devices.len - 1);
bc6059
+      dev = devices.get (devices.length - 1);
bc6059
       add_camera_device(dev);
bc6059
     }
bc6059
     else
bc6059
@@ -382,12 +382,11 @@ public PreferencesDialog (Cheese.Camera camera)
bc6059
       bool device_removed = false;
bc6059
       devices.foreach ((device) =>
bc6059
       {
bc6059
-        var old_device = (Cheese.CameraDevice) device;
bc6059
         Cheese.CameraDevice new_device;
bc6059
         camera_model.get (iter, 1, out new_device, -1);
bc6059
 
bc6059
         // Found the device that was removed.
bc6059
-        if (old_device != new_device)
bc6059
+        if (device != new_device)
bc6059
         {
bc6059
             remove_camera_device (iter, new_device, active_device);
bc6059
             device_removed = true;
bc6059
@@ -418,17 +417,16 @@ public PreferencesDialog (Cheese.Camera camera)
bc6059
    *
bc6059
    * @param device a Cheese.CameraDevice to add to the device combo box model
bc6059
    */
bc6059
-  private void add_camera_device (void *device)
bc6059
+  private void add_camera_device (Cheese.CameraDevice device)
bc6059
   {
bc6059
     TreeIter iter;
bc6059
-    Cheese.CameraDevice dev = (Cheese.CameraDevice) device;
bc6059
 
bc6059
     camera_model.append (out iter);
bc6059
     camera_model.set (iter,
bc6059
-                      0, dev.get_name (),
bc6059
-                      1, dev);
bc6059
+                      0, device.get_name (),
bc6059
+                      1, device);
bc6059
 
bc6059
-    if (camera.get_selected_device () == dev)
bc6059
+    if (camera.get_selected_device () == device)
bc6059
         source_combo.set_active_iter (iter);
bc6059
 
bc6059
     if (camera_model.iter_n_children (null) > 1)
bc6059
@@ -445,12 +443,12 @@ public PreferencesDialog (Cheese.Camera camera)
bc6059
   private void remove_camera_device (TreeIter iter, Cheese.CameraDevice device_node,
bc6059
                              Cheese.CameraDevice active_device_node)
bc6059
   {
bc6059
-      unowned GLib.PtrArray devices = camera.get_camera_devices ();
bc6059
+      GLib.GenericArray<unowned Cheese.CameraDevice> devices = camera.get_camera_devices ();
bc6059
 
bc6059
       // Check if the camera that we want to remove, is the active one
bc6059
       if (device_node == active_device_node)
bc6059
       {
bc6059
-        if (devices.len > 0)
bc6059
+        if (devices.length > 0)
bc6059
           set_new_available_camera_device (iter);
bc6059
         else
bc6059
           this.hide ();
bc6059
diff --git a/src/cheese-window.vala b/src/cheese-window.vala
bc6059
index ff069808..cc119b68 100644
bc6059
--- a/src/cheese-window.vala
bc6059
+++ b/src/cheese-window.vala
bc6059
@@ -1216,9 +1216,9 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
bc6059
    */
bc6059
   public void on_switch_camera_clicked ()
bc6059
   {
bc6059
-      Cheese.CameraDevice selected;
bc6059
-      Cheese.CameraDevice next = null;
bc6059
-      GLib.PtrArray cameras;
bc6059
+      unowned Cheese.CameraDevice selected;
bc6059
+      unowned Cheese.CameraDevice next = null;
bc6059
+      GLib.GenericArray<unowned Cheese.CameraDevice> cameras;
bc6059
       uint i;
bc6059
 
bc6059
       if (camera == null)
bc6059
@@ -1235,9 +1235,9 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
bc6059
 
bc6059
       cameras = camera.get_camera_devices ();
bc6059
 
bc6059
-      for (i = 0; i < cameras.len; i++)
bc6059
+      for (i = 0; i < cameras.length; i++)
bc6059
       {
bc6059
-          next = (Cheese.CameraDevice )cameras.index (i);
bc6059
+          next = cameras.get (i);
bc6059
 
bc6059
           if (next == selected)
bc6059
           {
bc6059
@@ -1245,13 +1245,13 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
bc6059
           }
bc6059
       }
bc6059
 
bc6059
-      if (i + 1 < cameras.len)
bc6059
+      if (i + 1 < cameras.length)
bc6059
       {
bc6059
-          next = (Cheese.CameraDevice )cameras.index (i + 1);
bc6059
+          next = cameras.get (i + 1);
bc6059
       }
bc6059
       else
bc6059
       {
bc6059
-          next = (Cheese.CameraDevice )cameras.index (0);
bc6059
+          next = cameras.get (0);
bc6059
       }
bc6059
 
bc6059
       if (next == selected)
bc6059
@@ -1269,8 +1269,8 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
bc6059
    */
bc6059
   public void set_switch_camera_button_state ()
bc6059
   {
bc6059
-      Cheese.CameraDevice selected;
bc6059
-      GLib.PtrArray cameras;
bc6059
+      unowned Cheese.CameraDevice selected;
bc6059
+      GLib.GenericArray<unowned Cheese.CameraDevice> cameras;
bc6059
 
bc6059
       if (camera == null)
bc6059
       {
bc6059
@@ -1288,7 +1288,7 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
bc6059
 
bc6059
       cameras = camera.get_camera_devices ();
bc6059
 
bc6059
-      if (cameras.len > 1)
bc6059
+      if (cameras.length > 1)
bc6059
       {
bc6059
          switch_camera_button.set_visible (true);
bc6059
          return;
bc6059
diff --git a/src/vapi/cheese-common.vapi b/src/vapi/cheese-common.vapi
bc6059
index 6517cdfc..e4ae7ad3 100644
bc6059
--- a/src/vapi/cheese-common.vapi
bc6059
+++ b/src/vapi/cheese-common.vapi
bc6059
@@ -35,7 +35,7 @@ namespace Cheese
bc6059
     [CCode (has_construct_function = false)]
bc6059
     public Camera (Clutter.Actor video_texture, string camera_device_node, int x_resolution, int y_resolution);
bc6059
     public bool                        get_balance_property_range (string property, double min, double max, double def);
bc6059
-    public unowned GLib.PtrArray       get_camera_devices ();
bc6059
+    public GLib.GenericArray<unowned Cheese.CameraDevice> get_camera_devices ();
bc6059
     public unowned Cheese.VideoFormat  get_current_video_format ();
bc6059
     public int                         get_num_camera_devices ();
bc6059
     public unowned Cheese.CameraDevice get_selected_device ();
bc6059
-- 
bc6059
GitLab
bc6059