static void gs_page_needs_user_action (GsPageHelper *helper, AsScreenshot *ss) { GtkWidget *content_area; GtkWidget *dialog; GtkWidget *ssimg; g_autofree gchar *escaped = NULL; GsPagePrivate *priv = gs_page_get_instance_private (helper->page); dialog = gtk_message_dialog_new (gs_shell_get_window (priv->shell), GTK_DIALOG_MODAL | GTK_DIALOG_USE_HEADER_BAR, GTK_MESSAGE_INFO, GTK_BUTTONS_CANCEL, /* TRANSLATORS: this is a prompt message, and * '%s' is an application summary, e.g. 'GNOME Clocks' */ _("Prepare %s"), gs_app_get_name (helper->app)); escaped = g_markup_escape_text (as_screenshot_get_caption (ss, NULL), -1); gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog), "%s", escaped); /* this will be enabled when the device is in the right mode */ helper->button_install = gtk_dialog_add_button (GTK_DIALOG (dialog), /* TRANSLATORS: update the fw */ _("Install"), GTK_RESPONSE_OK); helper->notify_quirk_id = g_signal_connect (helper->app, "notify::quirk", G_CALLBACK (gs_page_notify_quirk_cb), helper); gtk_widget_set_sensitive (helper->button_install, FALSE); /* load screenshot */ helper->soup_session = soup_session_new_with_options (SOUP_SESSION_USER_AGENT, gs_user_agent (), NULL); ssimg = gs_screenshot_image_new (helper->soup_session); gs_screenshot_image_set_screenshot (GS_SCREENSHOT_IMAGE (ssimg), ss); gs_screenshot_image_set_size (GS_SCREENSHOT_IMAGE (ssimg), 400, 225); gs_screenshot_image_load_async (GS_SCREENSHOT_IMAGE (ssimg), helper->cancellable); gtk_widget_set_margin_start (ssimg, 24); gtk_widget_set_margin_end (ssimg, 24); content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); gtk_container_add (GTK_CONTAINER (content_area), ssimg); gtk_container_child_set (GTK_CONTAINER (content_area), ssimg, "pack-type", GTK_PACK_END, NULL); /* handle this async */ g_signal_connect (dialog, "response", G_CALLBACK (gs_page_update_app_response_cb), helper); gs_shell_modal_dialog_present (priv->shell, GTK_DIALOG (dialog)); }
static void gs_screenshot_image_destroy (GtkWidget *widget) { GsScreenshotImage *ssimg = GS_SCREENSHOT_IMAGE (widget); if (ssimg->message != NULL) { soup_session_cancel_message (ssimg->session, ssimg->message, SOUP_STATUS_CANCELLED); g_clear_object (&ssimg->message); } g_clear_object (&ssimg->screenshot); g_clear_object (&ssimg->session); g_clear_object (&ssimg->settings); g_clear_pointer (&ssimg->filename, g_free); GTK_WIDGET_CLASS (gs_screenshot_image_parent_class)->destroy (widget); }
/** * gs_screenshot_image_destroy: **/ static void gs_screenshot_image_destroy (GtkWidget *widget) { GsScreenshotImage *ssimg = GS_SCREENSHOT_IMAGE (widget); GsScreenshotImagePrivate *priv; priv = gs_screenshot_image_get_instance_private (ssimg); if (priv->message != NULL) { soup_session_cancel_message (priv->session, priv->message, SOUP_STATUS_CANCELLED); g_clear_object (&priv->message); } g_clear_object (&priv->screenshot); g_clear_object (&priv->session); g_clear_pointer (&priv->cachedir, g_free); g_clear_pointer (&priv->filename, g_free); GTK_WIDGET_CLASS (gs_screenshot_image_parent_class)->destroy (widget); }
/** * 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 void gs_screenshot_image_complete_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) { g_autoptr(GsScreenshotImage) ssimg = GS_SCREENSHOT_IMAGE (user_data); gboolean ret; g_autoptr(GError) error = NULL; g_autoptr(GdkPixbuf) pixbuf = NULL; g_autoptr(GInputStream) stream = NULL; /* return immediately if the message was cancelled or if we're in destruction */ if (msg->status_code == SOUP_STATUS_CANCELLED || ssimg->session == NULL) return; if (msg->status_code == SOUP_STATUS_NOT_MODIFIED) { g_debug ("screenshot has not been modified"); as_screenshot_show_image (ssimg); return; } if (msg->status_code != SOUP_STATUS_OK) { g_warning ("Result of screenshot downloading attempt with " "status code '%u': %s", msg->status_code, msg->reason_phrase); /* if we're already showing an image, then don't set the error * as having an image (even if outdated) is better */ if (ssimg->showing_image) return; /* TRANSLATORS: this is when we try to download a screenshot and * we get back 404 */ gs_screenshot_image_set_error (ssimg, _("Screenshot not found")); 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 (ssimg->width == G_MAXUINT || ssimg->height == G_MAXUINT || (ssimg->width * ssimg->scale == (guint) gdk_pixbuf_get_width (pixbuf) && ssimg->height * ssimg->scale == (guint) gdk_pixbuf_get_height (pixbuf))) { ret = g_file_set_contents (ssimg->filename, msg->response_body->data, msg->response_body->length, &error); if (!ret) { gs_screenshot_image_set_error (ssimg, error->message); return; } } else if (!gs_screenshot_image_save_downloaded_img (ssimg, pixbuf, &error)) { gs_screenshot_image_set_error (ssimg, error->message); return; } /* got image, so show */ as_screenshot_show_image (ssimg); }