/** * gs_screenshot_image_complete_cb: **/ static void gs_screenshot_image_complete_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) { _cleanup_object_unref_ GsScreenshotImage *ssimg = GS_SCREENSHOT_IMAGE (user_data); GsScreenshotImagePrivate *priv = gs_screenshot_image_get_instance_private (ssimg); gboolean ret; _cleanup_error_free_ GError *error = NULL; _cleanup_object_unref_ AsImage *im = NULL; _cleanup_object_unref_ GdkPixbuf *pixbuf = NULL; _cleanup_object_unref_ GInputStream *stream = NULL; /* return immediately if the message was cancelled or if we're in destruction */ if (msg->status_code == SOUP_STATUS_CANCELLED || priv->session == NULL) return; if (msg->status_code != SOUP_STATUS_OK) { /* TRANSLATORS: this is when we try to download a screenshot and * we get back 404 */ gs_screenshot_image_set_error (ssimg, _("Screenshot not found")); gtk_widget_hide (GTK_WIDGET (ssimg)); return; } /* create a buffer with the data */ stream = g_memory_input_stream_new_from_data (msg->response_body->data, msg->response_body->length, NULL); if (stream == NULL) return; /* load the image */ pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL); if (pixbuf == NULL) { /* TRANSLATORS: possibly image file corrupt or not an image */ gs_screenshot_image_set_error (ssimg, _("Failed to load image")); return; } /* is image size destination size unknown or exactly the correct size */ if (priv->width == G_MAXUINT || priv->height == G_MAXUINT || (priv->width * priv->scale == (guint) gdk_pixbuf_get_width (pixbuf) && priv->height * priv->scale == (guint) gdk_pixbuf_get_height (pixbuf))) { ret = g_file_set_contents (priv->filename, msg->response_body->data, msg->response_body->length, &error); if (!ret) { gs_screenshot_image_set_error (ssimg, error->message); return; } } else { /* save to file, using the same code as the AppStream builder * so the preview looks the same */ im = as_image_new (); as_image_set_pixbuf (im, pixbuf); ret = as_image_save_filename (im, priv->filename, priv->width * priv->scale, priv->height * priv->scale, AS_IMAGE_SAVE_FLAG_PAD_16_9, &error); if (!ret) { gs_screenshot_image_set_error (ssimg, error->message); return; } } /* got image, so show */ as_screenshot_show_image (ssimg); }
static gboolean gs_screenshot_image_save_downloaded_img (GsScreenshotImage *ssimg, GdkPixbuf *pixbuf, GError **error) { g_autoptr(AsImage) im = NULL; gboolean ret; const GPtrArray *images; g_autoptr(GError) error_local = NULL; g_autofree char *filename = NULL; g_autofree char *size_dir = NULL; g_autofree char *cache_kind = NULL; g_autofree char *basename = NULL; guint width = ssimg->width; guint height = ssimg->height; /* save to file, using the same code as the AppStream builder * so the preview looks the same */ im = as_image_new (); as_image_set_pixbuf (im, pixbuf); ret = as_image_save_filename (im, ssimg->filename, ssimg->width * ssimg->scale, ssimg->height * ssimg->scale, AS_IMAGE_SAVE_FLAG_PAD_16_9, error); if (!ret) return FALSE; if (ssimg->screenshot == NULL) return TRUE; images = as_screenshot_get_images (ssimg->screenshot); if (images->len > 1) return TRUE; if (width == AS_IMAGE_THUMBNAIL_WIDTH && height == AS_IMAGE_THUMBNAIL_HEIGHT) { width = AS_IMAGE_NORMAL_WIDTH; height = AS_IMAGE_NORMAL_HEIGHT; } else { width = AS_IMAGE_THUMBNAIL_WIDTH; height = AS_IMAGE_THUMBNAIL_HEIGHT; } width *= ssimg->scale; height *= ssimg->scale; basename = g_path_get_basename (ssimg->filename); size_dir = g_strdup_printf ("%ux%u", width, height); cache_kind = g_build_filename ("screenshots", size_dir, NULL); filename = gs_utils_get_cache_filename (cache_kind, basename, GS_UTILS_CACHE_FLAG_WRITEABLE, &error_local); if (filename == NULL) { /* if we cannot get a cache filename, warn about that but do not * set a user's visible error because this is a complementary * operation */ g_warning ("Failed to get cache filename for counterpart " "screenshot '%s' in folder '%s': %s", basename, cache_kind, error_local->message); return TRUE; } ret = as_image_save_filename (im, filename, width, height, AS_IMAGE_SAVE_FLAG_PAD_16_9, &error_local); if (!ret) { /* if we cannot save this screenshot, warn about that but do not * set a user's visible error because this is a complementary * operation */ g_warning ("Failed to save screenshot '%s': %s", filename, error_local->message); } return TRUE; }