gdkcontentformats: Change the matching API

Instead of having just one function that has the gtype and mime type as
out arguments, have 3 functions: 1 that finds any match, 1 that finds a
GType match and one for a mime type match.

This makes the API way more convenient to use.
This commit is contained in:
Benjamin Otte 2017-11-24 11:34:19 +01:00
parent 25c3895836
commit 7426f1a16b
7 changed files with 70 additions and 45 deletions

View File

@ -377,6 +377,8 @@ gdk_content_formats_get_gtypes
gdk_content_formats_get_mime_types
gdk_content_formats_union
gdk_content_formats_match
gdk_content_formats_match_gtype
gdk_content_formats_match_mime_types
gdk_content_formats_contain_gtype
gdk_content_formats_contain_mime_type

View File

@ -166,9 +166,9 @@ gdk_clipboard_read_local_async (GdkClipboard *clipboard,
content_formats = gdk_content_provider_ref_formats (priv->content);
content_formats = gdk_content_formats_union_serialize_mime_types (content_formats);
mime_type = gdk_content_formats_match_mime_type (content_formats, formats);
if (gdk_content_formats_match (content_formats, formats, NULL, &mime_type)
&& mime_type != NULL)
if (mime_type != NULL)
{
GOutputStream *output_stream;
GIOStream *stream;
@ -892,7 +892,8 @@ gdk_clipboard_write_async (GdkClipboard *clipboard,
mime_formats = gdk_content_formats_new ((const gchar *[2]) { mime_type, NULL }, 1);
mime_formats = gdk_content_formats_union_serialize_gtypes (mime_formats);
if (gdk_content_formats_match (mime_formats, formats, &gtype, NULL))
gtype = gdk_content_formats_match_gtype (formats, mime_formats);
if (gtype != G_TYPE_INVALID)
{
GValue value = G_VALUE_INIT;
GError *error = NULL;

View File

@ -276,55 +276,78 @@ gdk_content_formats_contain_interned_mime_type (const GdkContentFormats *formats
* gdk_content_formats_match:
* @first: the primary #GdkContentFormats to intersect
* @second: the #GdkContentFormats to intersect with
* @out_gtype: (out) (allow-none): pointer to take the
* matching #GType or %G_TYPE_INVALID if @out_mime_type was set.
* @out_mime_type: (out) (allow-none) (transfer none): The matching
* mime type or %NULL if @out_gtype is set
*
* Finds the first element from @first that is also contained
* in @second. If no matching format is found, %FALSE is returned
* and @out_gtype and @out_mime_type are set to %G_TYPE_INVALID and
* %NULL respectively.
* Checks if @first and @second have any matching formats.
*
* Returns: %TRUE if a matching format was found.
*/
gboolean
gdk_content_formats_match (const GdkContentFormats *first,
const GdkContentFormats *second,
GType *out_gtype,
const char **out_mime_type)
const GdkContentFormats *second)
{
g_return_val_if_fail (first != NULL, FALSE);
g_return_val_if_fail (second != NULL, FALSE);
return gdk_content_formats_match_gtype (first, second) != G_TYPE_INVALID
|| gdk_content_formats_match_mime_type (first, second) != NULL;
}
/**
* gdk_content_formats_match_gtype:
* @first: the primary #GdkContentFormats to intersect
* @second: the #GdkContentFormats to intersect with
*
* Finds the first #GType from @first that is also contained
* in @second. If no matching #GType is found, %G_TYPE_INVALID
* is returned.
*
* Returns: The first common #GType or %G_TYPE_INVALID if none.
**/
GType
gdk_content_formats_match_gtype (const GdkContentFormats *first,
const GdkContentFormats *second)
{
gsize i;
g_return_val_if_fail (first != NULL, FALSE);
g_return_val_if_fail (second != NULL, FALSE);
if (out_gtype)
*out_gtype = G_TYPE_INVALID;
if (out_mime_type)
*out_mime_type = NULL;
for (i = 0; i < first->n_gtypes; i++)
{
if (gdk_content_formats_contain_gtype (second, first->gtypes[i]))
{
if (out_gtype)
*out_gtype = first->gtypes[i];
return TRUE;
}
return first->gtypes[i];
}
return G_TYPE_INVALID;
}
/**
* gdk_content_formats_match_mime_type:
* @first: the primary #GdkContentFormats to intersect
* @second: the #GdkContentFormats to intersect with
*
* Finds the first mime type from @first that is also contained
* in @second. If no matching mime type is found, %NULL is
* returned.
*
* Returns: The first common mime type or %NULL if none.
**/
const char *
gdk_content_formats_match_mime_type (const GdkContentFormats *first,
const GdkContentFormats *second)
{
gsize i;
g_return_val_if_fail (first != NULL, FALSE);
g_return_val_if_fail (second != NULL, FALSE);
for (i = 0; i < first->n_mime_types; i++)
{
if (gdk_content_formats_contain_interned_mime_type (second, first->mime_types[i]))
{
if (out_mime_type)
*out_mime_type = first->mime_types[i];
return TRUE;
}
return first->mime_types[i];
}
return FALSE;
return NULL;
}
/**
@ -534,6 +557,7 @@ gdk_content_formats_builder_add_gtype (GdkContentFormatsBuilder *builder,
GType type)
{
g_return_if_fail (builder != NULL);
g_return_if_fail (type != G_TYPE_INVALID);
if (g_slist_find (builder->gtypes, GSIZE_TO_POINTER (type)))
return;

View File

@ -58,9 +58,13 @@ GdkContentFormats * gdk_content_formats_union (GdkContentForma
const GdkContentFormats *second) G_GNUC_WARN_UNUSED_RESULT;
GDK_AVAILABLE_IN_3_94
gboolean gdk_content_formats_match (const GdkContentFormats *first,
const GdkContentFormats *second,
GType *out_gtype,
const char **out_mime_type);
const GdkContentFormats *second);
GDK_AVAILABLE_IN_3_94
GType gdk_content_formats_match_gtype (const GdkContentFormats *first,
const GdkContentFormats *second);
GDK_AVAILABLE_IN_3_94
const char * gdk_content_formats_match_mime_type (const GdkContentFormats *first,
const GdkContentFormats *second);
GDK_AVAILABLE_IN_3_94
gboolean gdk_content_formats_contain_gtype (const GdkContentFormats *formats,
GType type);

View File

@ -406,13 +406,11 @@ gtk_drag_dest_get_track_motion (GtkWidget *widget)
* Returns: (transfer none) (nullable): first target that the source offers
* and the dest can accept, or %NULL
*/
GdkAtom
const char *
gtk_drag_dest_find_target (GtkWidget *widget,
GdkDragContext *context,
GdkContentFormats *target_list)
{
GdkAtom result;
g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
@ -422,11 +420,7 @@ gtk_drag_dest_find_target (GtkWidget *widget,
if (target_list == NULL)
return NULL;
gdk_content_formats_match (target_list,
gdk_drag_context_get_formats (context),
NULL,
&result);
return result;
return gdk_content_formats_match_mime_type (target_list,
gdk_drag_context_get_formats (context));
}

View File

@ -76,7 +76,7 @@ GDK_AVAILABLE_IN_ALL
void gtk_drag_dest_unset (GtkWidget *widget);
GDK_AVAILABLE_IN_ALL
GdkAtom gtk_drag_dest_find_target (GtkWidget *widget,
const char * gtk_drag_dest_find_target (GtkWidget *widget,
GdkDragContext *context,
GdkContentFormats *target_list);
GDK_AVAILABLE_IN_ALL

View File

@ -344,7 +344,7 @@ target_drag_drop (GtkWidget *widget,
guint time)
{
GdkContentFormats *formats;
GdkAtom format;
const char *format;
g_print("drop\n");
have_drag = FALSE;
@ -352,7 +352,7 @@ target_drag_drop (GtkWidget *widget,
gtk_image_set_from_pixbuf (GTK_IMAGE (widget), trashcan_closed);
formats = gdk_drag_context_get_formats (context);
gdk_content_formats_match (formats, formats, NULL, &format);
format = gdk_content_formats_match_mime_type (formats, formats);
if (format)
{
gtk_drag_get_data (widget, context,