[Spice-devel] [PATCH spice-gtk] add optional format string to spice_usb_device_get_description()

Hans de Goede hdegoede at redhat.com
Tue Jan 3 07:28:06 PST 2012


Hi,

Ah, I had imagined a more elaborate scheme were we would define
our own conversion specifier letters for the differerent bits,
but this works too and is much simpler, ack!

Regards,

Hans



On 01/03/2012 04:20 PM, Marc-André Lureau wrote:
> ---
>   gtk/usb-device-manager.c |   39 +++++++++++++++++++++++++--------------
>   gtk/usb-device-manager.h |    2 +-
>   2 files changed, 26 insertions(+), 15 deletions(-)
>
> diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
> index e55caae..0f53755 100644
> --- a/gtk/usb-device-manager.c
> +++ b/gtk/usb-device-manager.c
> @@ -490,7 +490,7 @@ static void spice_usb_device_manager_auto_connect_cb(GObject      *gobject,
>
>       spice_usb_device_manager_connect_device_finish(self, res,&err);
>       if (err) {
> -        gchar *desc = spice_usb_device_get_description((SpiceUsbDevice *)device);
> +        gchar *desc = spice_usb_device_get_description((SpiceUsbDevice *)device, NULL);
>           g_prefix_error(&err, "Could not auto-redirect %s: ", desc);
>           g_free(desc);
>
> @@ -871,20 +871,30 @@ void spice_usb_device_manager_disconnect_device(SpiceUsbDeviceManager *self,
>   /**
>    * spice_usb_device_get_description:
>    * @device: #SpiceUsbDevice to get the description of
> + * @format: an optionnal printf() format string with positionnal parameters
>    *
>    * Get a string describing the device which is suitable as a description of
>    * the device for the end user. The returned string should be freed with
>    * g_free() when no longer needed.
>    *
> - * Returns: a newly-allocated string holding the description
> + * The @format positionnal parameters are the following:
> + * - '%%1$s' manufacturer
> + * - '%%2$s' product
> + * - '%%3$s' descriptor (a [vendor_id:product_id] string)
> + * - '%%4$d' bus
> + * - '%%5$d' address
> + *
> + * (the default format string is "%%s %%s %%s at %%d-%%d")
> + *
> + * Returns: a newly-allocated string holding the description, or %NULL if failed
>    */
> -gchar *spice_usb_device_get_description(SpiceUsbDevice *_device)
> +gchar *spice_usb_device_get_description(SpiceUsbDevice *_device, const gchar *format)
>   {
>   #ifdef USE_USBREDIR
>       libusb_device *device = (libusb_device *)_device;
>       struct libusb_device_descriptor desc;
> -    int rc, bus, address;
> -    gchar *description, *manufacturer = NULL, *product = NULL;
> +    int bus, address;
> +    gchar *description, *descriptor, *manufacturer = NULL, *product = NULL;
>
>       g_return_val_if_fail(device != NULL, NULL);
>
> @@ -902,17 +912,18 @@ gchar *spice_usb_device_get_description(SpiceUsbDevice *_device)
>       if (!product)
>           product = g_strdup(_("Device"));
>
> -    rc = libusb_get_device_descriptor(device,&desc);
> -    if (rc == LIBUSB_SUCCESS) {
> -        description = g_strdup_printf(_("%s %s [%04x:%04x] at %d-%d"),
> -                                      manufacturer, product, desc.idVendor,
> -                                      desc.idProduct, bus, address);
> -    } else {
> -        description = g_strdup_printf(_("%s %s at %d-%d"), manufacturer,
> -                                      product, bus, address);
> -    }
> +    if (libusb_get_device_descriptor(device,&desc) == LIBUSB_SUCCESS)
> +        descriptor = g_strdup_printf("[%04x:%04x]", desc.idVendor, desc.idProduct);
> +    else
> +        descriptor = g_strdup("");
> +
> +    if (!format)
> +        format = _("%s %s %s at %d-%d");
> +
> +    description = g_strdup_printf(format, manufacturer, product, descriptor, bus, address);
>
>       g_free(manufacturer);
> +    g_free(descriptor);
>       g_free(product);
>
>       return description;
> diff --git a/gtk/usb-device-manager.h b/gtk/usb-device-manager.h
> index a8b3969..d01bc12 100644
> --- a/gtk/usb-device-manager.h
> +++ b/gtk/usb-device-manager.h
> @@ -87,7 +87,7 @@ struct _SpiceUsbDeviceManagerClass
>   GType spice_usb_device_get_type(void);
>   GType spice_usb_device_manager_get_type(void);
>
> -gchar *spice_usb_device_get_description(SpiceUsbDevice *device);
> +gchar *spice_usb_device_get_description(SpiceUsbDevice *device, const gchar *format);
>
>   SpiceUsbDeviceManager *spice_usb_device_manager_get(SpiceSession *session,
>                                                       GError **err);


More information about the Spice-devel mailing list