Commit 3bbc1e15 authored by Cosimo Cecchi's avatar Cosimo Cecchi

screenshot: make non interactive mode headless

Don't show a dialog prompt if we're not in interactive mode, but just
save the image to the saved screenshot directory (or if that doesn't
exist, to the default location).
parent e2aae6f2
......@@ -10,10 +10,15 @@
<_summary>Screenshot delay</_summary>
<_description>The number of seconds to wait before taking the screenshot.</_description>
</key>
<key name="last-save-directory" type="s">
<key name="auto-save-directory" type="s">
<default>''</default>
<_summary>Screenshot directory</_summary>
<_description>The directory the last screenshot was saved in.</_description>
<_description>The directory where the screenshots will be saved by default.</_description>
</key>
<key name="last-save-directory" type="s">
<default>''</default>
<_summary>Last save directory</_summary>
<_description>The last directory a screenshot was saved in interactive mode.</_description>
</key>
<key name="include-border" type="b">
<default>true</default>
......
......@@ -53,15 +53,17 @@ struct _ScreenshotApplicationPriv {
gchar *icc_profile_base64;
GdkPixbuf *screenshot;
gchar *save_uri;
ScreenshotDialog *dialog;
};
static void
save_folder_to_settings (ScreenshotDialog *dialog)
save_folder_to_settings (ScreenshotApplication *self)
{
char *folder;
folder = screenshot_dialog_get_folder (dialog);
folder = screenshot_dialog_get_folder (self->priv->dialog);
g_settings_set_string (screenshot_config->settings,
LAST_SAVE_DIRECTORY_KEY, folder);
......@@ -69,9 +71,9 @@ save_folder_to_settings (ScreenshotDialog *dialog)
}
static void
set_recent_entry (ScreenshotDialog *dialog)
set_recent_entry (ScreenshotApplication *self)
{
char *uri, *app_exec = NULL;
char *app_exec = NULL;
GtkRecentManager *recent;
GtkRecentData recent_data;
GAppInfo *app;
......@@ -85,7 +87,6 @@ set_recent_entry (ScreenshotDialog *dialog)
return;
}
uri = screenshot_dialog_get_uri (dialog);
recent = gtk_recent_manager_get_default ();
exec_name = g_app_info_get_executable (app);
......@@ -99,68 +100,48 @@ set_recent_entry (ScreenshotDialog *dialog)
recent_data.groups = groups;
recent_data.is_private = FALSE;
gtk_recent_manager_add_full (recent, uri, &recent_data);
gtk_recent_manager_add_full (recent, self->priv->save_uri, &recent_data);
g_object_unref (app);
g_free (app_exec);
g_free (uri);
}
static void
error_dialog_response_cb (GtkDialog *d,
gint response,
ScreenshotDialog *dialog)
save_pixbuf_handle_success (ScreenshotApplication *self)
{
gtk_widget_destroy (GTK_WIDGET (d));
screenshot_dialog_focus_entry (dialog);
}
static void
save_file_failed_error (ScreenshotDialog *dialog,
GError *error)
{
GtkWidget *toplevel;
GtkWidget *error_dialog;
char *folder;
set_recent_entry (self);
toplevel = screenshot_dialog_get_toplevel (dialog);
screenshot_dialog_set_busy (dialog, FALSE);
/* we had an error, display a dialog to the user and let him choose
* another name/location to save the screenshot.
*/
folder = screenshot_dialog_get_folder (dialog);
error_dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
_("Error while saving screenshot"));
/* translators: first %s is the folder URI, second %s is the VFS error */
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (error_dialog),
_("Impossible to save the screenshot "
"to %s.\n Error was %s.\n Please choose another "
"location and retry."), folder, error->message);
gtk_widget_show (error_dialog);
g_signal_connect (error_dialog,
"response",
G_CALLBACK (error_dialog_response_cb),
dialog);
if (screenshot_config->interactive)
{
ScreenshotDialog *dialog = self->priv->dialog;
g_free (folder);
save_folder_to_settings (self);
gtk_widget_destroy (screenshot_dialog_get_toplevel (dialog));
}
else
{
g_application_release (G_APPLICATION (self));
}
}
static void
save_file_completed (ScreenshotDialog *dialog)
save_pixbuf_handle_error (ScreenshotApplication *self)
{
GtkWidget *toplevel;
toplevel = screenshot_dialog_get_toplevel (dialog);
if (screenshot_config->interactive)
{
ScreenshotDialog *dialog = self->priv->dialog;
save_folder_to_settings (dialog);
set_recent_entry (dialog);
gtk_widget_destroy (toplevel);
screenshot_dialog_set_busy (dialog, FALSE);
screenshot_show_error_dialog (GTK_WINDOW (screenshot_dialog_get_toplevel (dialog)),
_("Unable to capture a screenshot"),
_("Error creating file. Please choose another location and retry."));
screenshot_dialog_focus_entry (dialog);
}
else
{
screenshot_play_sound_effect ("dialog-error", _("Unable to capture a screenshot"));
g_application_release (G_APPLICATION (self));
}
}
static void
......@@ -169,18 +150,18 @@ save_pixbuf_ready_cb (GObject *source,
gpointer user_data)
{
GError *error = NULL;
ScreenshotDialog *dialog = user_data;
ScreenshotApplication *self = user_data;
gdk_pixbuf_save_to_stream_finish (res, &error);
if (error != NULL)
{
save_file_failed_error (dialog, error);
g_error_free (error);
save_pixbuf_handle_error (self);
return;
}
save_file_completed (dialog);
save_pixbuf_handle_success (self);
}
static void
......@@ -189,7 +170,6 @@ save_file_create_ready_cb (GObject *source,
gpointer user_data)
{
ScreenshotApplication *self = user_data;
ScreenshotDialog *dialog = self->priv->dialog;
GFileOutputStream *os;
GError *error = NULL;
......@@ -197,8 +177,8 @@ save_file_create_ready_cb (GObject *source,
if (error != NULL)
{
save_file_failed_error (dialog, error);
g_error_free (error);
save_pixbuf_handle_error (self);
return;
}
......@@ -206,7 +186,7 @@ save_file_create_ready_cb (GObject *source,
gdk_pixbuf_save_to_stream_async (self->priv->screenshot,
G_OUTPUT_STREAM (os),
"png", NULL,
save_pixbuf_ready_cb, dialog,
save_pixbuf_ready_cb, self,
"icc-profile", self->priv->icc_profile_base64,
"tEXt::Software", "gnome-screenshot",
NULL);
......@@ -214,7 +194,7 @@ save_file_create_ready_cb (GObject *source,
gdk_pixbuf_save_to_stream_async (self->priv->screenshot,
G_OUTPUT_STREAM (os),
"png", NULL,
save_pixbuf_ready_cb, dialog,
save_pixbuf_ready_cb, self,
"tEXt::Software", "gnome-screenshot",
NULL);
......@@ -222,17 +202,14 @@ save_file_create_ready_cb (GObject *source,
}
static void
try_to_save (ScreenshotApplication *self)
screenshot_save_to_file (ScreenshotApplication *self)
{
ScreenshotDialog *dialog = self->priv->dialog;
gchar *target_uri;
GFile *target_file;
screenshot_dialog_set_busy (dialog, TRUE);
target_uri = screenshot_dialog_get_uri (dialog);
target_file = g_file_new_for_uri (target_uri);
if (self->priv->dialog != NULL)
screenshot_dialog_set_busy (self->priv->dialog, TRUE);
target_file = g_file_new_for_uri (self->priv->save_uri);
g_file_create_async (target_file,
G_FILE_CREATE_NONE,
G_PRIORITY_DEFAULT,
......@@ -240,7 +217,6 @@ try_to_save (ScreenshotApplication *self)
save_file_create_ready_cb, self);
g_object_unref (target_file);
g_free (target_uri);
}
static void
......@@ -266,7 +242,10 @@ screenshot_dialog_response_cb (GtkDialog *d,
screenshot_display_help (GTK_WINDOW (d));
break;
case GTK_RESPONSE_OK:
try_to_save (self);
/* update to the new URI */
g_free (self->priv->save_uri);
self->priv->save_uri = screenshot_dialog_get_uri (self->priv->dialog);
screenshot_save_to_file (self);
break;
case SCREENSHOT_RESPONSE_COPY:
screenshot_save_to_clipboard (self);
......@@ -288,7 +267,7 @@ build_filename_ready_cb (GObject *source,
gchar *save_uri;
GError *error = NULL;
save_uri = screenshot_build_filename_finish (res, &error);
self->priv->save_uri = screenshot_build_filename_finish (res, &error);
/* now release the application */
g_application_release (G_APPLICATION (self));
......@@ -311,18 +290,24 @@ build_filename_ready_cb (GObject *source,
screenshot_play_sound_effect ("screen-capture", _("Screenshot taken"));
self->priv->dialog = screenshot_dialog_new (self->priv->screenshot, save_uri);
toplevel = screenshot_dialog_get_toplevel (self->priv->dialog);
gtk_widget_show (toplevel);
if (screenshot_config->interactive)
{
self->priv->dialog = screenshot_dialog_new (self->priv->screenshot, self->priv->save_uri);
toplevel = screenshot_dialog_get_toplevel (self->priv->dialog);
gtk_widget_show (toplevel);
gtk_application_add_window (GTK_APPLICATION (self), GTK_WINDOW (toplevel));
gtk_application_add_window (GTK_APPLICATION (self), GTK_WINDOW (toplevel));
g_signal_connect (toplevel,
"response",
G_CALLBACK (screenshot_dialog_response_cb),
self);
g_free (save_uri);
g_signal_connect (toplevel,
"response",
G_CALLBACK (screenshot_dialog_response_cb),
self);
}
else
{
g_application_hold (G_APPLICATION (self));
screenshot_save_to_file (self);
}
}
static void
......@@ -625,6 +610,7 @@ screenshot_application_finalize (GObject *object)
g_clear_object (&self->priv->connection);
g_clear_object (&self->priv->screenshot);
g_free (self->priv->icc_profile_base64);
g_free (self->priv->save_uri);
G_OBJECT_CLASS (screenshot_application_parent_class)->finalize (object);
}
......
......@@ -30,11 +30,15 @@
#define INCLUDE_BORDER_KEY "include-border"
#define INCLUDE_POINTER_KEY "include-pointer"
#define INCLUDE_ICC_PROFILE "include-icc-profile"
#define AUTO_SAVE_DIRECTORY_KEY "auto-save-directory"
#define LAST_SAVE_DIRECTORY_KEY "last-save-directory"
static void
populate_from_settings (ScreenshotConfig *config)
{
config->auto_save_dir =
g_settings_get_string (config->settings,
AUTO_SAVE_DIRECTORY_KEY);
config->last_save_dir =
g_settings_get_string (config->settings,
LAST_SAVE_DIRECTORY_KEY);
......
......@@ -28,6 +28,7 @@ G_BEGIN_DECLS
typedef struct {
GSettings *settings;
gchar *auto_save_dir;
gchar *last_save_dir;
gboolean copy_to_clipboard;
......
......@@ -265,7 +265,11 @@ screenshot_build_filename_async (GAsyncReadyCallback callback,
job = g_slice_new0 (AsyncExistenceJob);
job->base_uris[0] = sanitize_save_directory (screenshot_config->last_save_dir);
if (screenshot_config->interactive)
job->base_uris[0] = sanitize_save_directory (screenshot_config->last_save_dir);
else
job->base_uris[0] = sanitize_save_directory (screenshot_config->auto_save_dir);
job->base_uris[1] = get_default_screenshot_dir ();
job->iteration = 0;
job->type = TEST_SAVED_DIR;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment