From 74e7541c2b6e0a37890274cbf00698df8d21d62c Mon Sep 17 00:00:00 2001 From: Peter Uithoven Date: Wed, 6 Feb 2019 01:15:02 +0100 Subject: [PATCH] Save screenshots to (self created) screenshots folder (#459) * Save screenshots to (self created) screenshots folder * Fallback to pictures folder * Explicitly forwarding error --- src/ScreenshotManager.vala | 39 +++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/ScreenshotManager.vala b/src/ScreenshotManager.vala index 360830a1..89434064 100644 --- a/src/ScreenshotManager.vala +++ b/src/ScreenshotManager.vala @@ -86,7 +86,7 @@ namespace Gala public async void screenshot_area (int x, int y, int width, int height, bool flash, string filename, out bool success, out string filename_used) throws DBusError, IOError { debug ("Taking area screenshot"); - + yield wait_stage_repaint (); var image = take_screenshot (x, y, width, height, false); @@ -138,19 +138,44 @@ namespace Gala yield; selection_area.destroy (); - + yield wait_stage_repaint (); selection_area.get_selection_rectangle (out x, out y, out width, out height); } + public static void create_dir_if_missing (string path) throws Error { + File file = File.new_for_path (path); + if (!file.query_exists ()) { + try { + file.make_directory_with_parents (); + } catch (Error e) { + throw e; + } + } + } + + static string find_target_path () + { + var pictures_path = Environment.get_user_special_dir (UserDirectory.PICTURES); + var screenshot_folder = _("Screenshots"); + var path = Path.build_path ( + Path.DIR_SEPARATOR_S, + pictures_path, + screenshot_folder + ); + try { + create_dir_if_missing (path); + return path; + } catch (Error e) { + warning (e.message); + return pictures_path; + } + } + static bool save_image (Cairo.ImageSurface image, string filename, out string used_filename) { if (!Path.is_absolute (filename)) { - string path = Environment.get_user_special_dir (UserDirectory.PICTURES); - if (!FileUtils.test (path, FileTest.EXISTS)) { - path = Environment.get_home_dir (); - } - + var path = find_target_path (); if (!filename.has_suffix (".png")) { used_filename = Path.build_filename (path, filename.concat (".png"), null); } else {