Add constructors for GdkFileList
authorEmmanuele Bassi <ebassi@gnome.org>
Thu, 28 Jul 2022 09:13:17 +0000 (10:13 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Thu, 28 Jul 2022 13:41:35 +0000 (14:41 +0100)
C API users can keep dealing with the implicit equivalence of
GdkFileList and GSList, but language bindings have no idea that one type
is another, and none of them exposes GSList as a type anyway, so they
will need a way to construct a GdkFileList.

Instead of making GdkFileList mutable, and re-implement GSList, we only
provide a constructors pair that lets you create a GdkFileList from a
linked list or from an array.

gdk/gdkcontentformats.c
gdk/gdkcontentformats.h

index 6caaa2ca82f86b4ffc640acfd422f7c858274503..172fa1712e692ae6539c921273f36a9e4e1b9e6e 100644 (file)
@@ -184,7 +184,7 @@ gdk_content_formats_new_for_gtype (GType type)
  * @string: the string to parse
  *
  * Parses the given @string into `GdkContentFormats` and
- * returns the formats. 
+ * returns the formats.
  *
  * Strings printed via [method@Gdk.ContentFormats.to_string]
  * can be read in again successfully using this function.
@@ -540,7 +540,7 @@ gdk_content_formats_get_gtypes (const GdkContentFormats *formats,
 
   if (n_gtypes)
     *n_gtypes = formats->n_gtypes;
-  
+
   return formats->gtypes;
 }
 
@@ -567,7 +567,7 @@ gdk_content_formats_get_mime_types (const GdkContentFormats *formats,
 
   if (n_mime_types)
     *n_mime_types = formats->n_mime_types;
-  
+
   return formats->mime_types;
 }
 
@@ -663,7 +663,7 @@ gdk_content_formats_builder_unref (GdkContentFormatsBuilder *builder)
 
   if (builder->ref_count > 0)
     return;
-  
+
   gdk_content_formats_builder_clear (builder);
   g_slice_free (GdkContentFormatsBuilder, builder);
 }
@@ -862,4 +862,50 @@ gdk_file_list_get_files (GdkFileList *file_list)
   return g_slist_copy ((GSList *) file_list);
 }
 
+/**
+ * gdk_file_list_new_from_list:
+ * @files: (element-type GFile): a list of files
+ *
+ * Creates a new files list container from a singly linked list of
+ * `GFile` instances.
+ *
+ * This function is meant to be used by language bindings
+ *
+ * Returns: (transfer full): the newly created files list
+ *
+ * Since: 4.8
+ */
+GdkFileList *
+gdk_file_list_new_from_list (GSList *files)
+{
+  return gdk_file_list_copy (files);
+}
+
+/**
+ * gdk_file_list_new_from_array:
+ * @files: (array length=n_files): the files to add to the list
+ * @n_files: the number of files in the array
+ *
+ * Creates a new `GdkFileList` for the given array of files.
+ *
+ * This function is meant to be used by language bindings.
+ *
+ * Returns: (transfer full): the newly create files list
+ *
+ * Since: 4.8
+ */
+GdkFileList *
+gdk_file_list_new_from_array (GFile **files,
+                              gsize   n_files)
+{
+  if (files == NULL || n_files == 0)
+    return NULL;
+
+  GSList *res = NULL;
+  for (gssize i = n_files - 1; i >= 0; i--)
+    res = g_slist_prepend (res, g_object_ref (files[i]));
+
+  return (GdkFileList *) res;
+}
+
 /* }}} */
index df536ccef227628e03a2a362321e8a105419b2e5..e37238f1be5f68bef0cb4fd27f654ac28ca9a924 100644 (file)
@@ -122,6 +122,11 @@ typedef struct _GdkFileList GdkFileList;
 
 GDK_AVAILABLE_IN_4_6
 GSList *        gdk_file_list_get_files (GdkFileList *file_list);
+GDK_AVAILABLE_IN_4_8
+GdkFileList *   gdk_file_list_new_from_list (GSList *files);
+GDK_AVAILABLE_IN_4_8
+GdkFileList *   gdk_file_list_new_from_array (GFile **files,
+                                              gsize   n_files);
 
 G_END_DECLS