Skip to content

Commit

Permalink
Implement screenshots cleanup
Browse files Browse the repository at this point in the history
Extend gschema schema with flag for screenshot cleanup. When
enabled, include the ~/Pictures/Screenshots directory in tmpfiles
configuration file. Refactor code of Housekeeping class to make
it easier to add more cleanup options in the future.
  • Loading branch information
Antolius committed May 7, 2023
1 parent 3301d76 commit bf083ca
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
5 changes: 5 additions & 0 deletions data/io.elementary.settings-daemon.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<summary>Whether the Downloads folder should be automatically cleaned up.</summary>
<description></description>
</key>
<key type="b" name="cleanup-screenshots-folder">
<default>false</default>
<summary>Whether the Screenshots folder should be automatically cleaned up.</summary>
<description></description>
</key>
<key type="i" name="old-files-age">
<default>30</default>
<summary>How many days to keep an old file before it is cleaned up.</summary>
Expand Down
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ gio_dep = dependency ('gio-2.0')
glib_dep = dependency('glib-2.0')
granite_dep = dependency('granite', version: '>= 5.3.0')
i18n = import('i18n')
add_global_arguments(
'-DGETTEXT_PACKAGE="@0@"'.format (meson.project_name()),
language:'c'
)

cc = meson.get_compiler('c')
m_dep = cc.find_library('m', required : false)
Expand Down
79 changes: 54 additions & 25 deletions src/Backends/Housekeeping.vala
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
"io.elementary.settings-daemon.downloads-folder.conf"
);

var downloads_cleanup_enabled = housekeeping_settings.get_boolean ("cleanup-downloads-folder");
var cleanup_config = new CleanupConfig (housekeeping_settings);

var config_file = File.new_for_path (config_path);
if (!config_file.get_parent ().query_exists ()) {
if (!downloads_cleanup_enabled) {
// No point continuing if cleanup isn't enabled
if (cleanup_config.is_disabled) {
// No point continuing if cleanup is disabled
return;
}

Expand All @@ -98,22 +98,8 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
}
}

int downloads_cleanup_days = housekeeping_settings.get_int ("old-files-age");

var downloads_folder = Environment.get_user_special_dir (
UserDirectory.DOWNLOAD
);

var home_folder = Environment.get_home_dir ();
if (File.new_for_path (home_folder).equal (File.new_for_path (downloads_folder))) {
// TODO: Possibly throw a notification as a warning here? This will currently just silently fail
// and no downloads will be cleaned up, despite the setting being enabled
warning ("Downloads folder seems to point to home directory, not enabling cleanup");
downloads_cleanup_enabled = false;
}

// Delete the systemd-tmpfiles config if download cleanup is disabled
if (!downloads_cleanup_enabled || downloads_cleanup_days < 1) {
// Delete the systemd-tmpfiles config if cleanup is disabled
if (cleanup_config.is_disabled) {
try {
yield config_file.delete_async ();
} catch (Error e) {
Expand All @@ -133,16 +119,59 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
return;
}

// See https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html for details
// Results in a config line like:
// e "/home/david/Downloads" - - - 30d
string config = "e \"%s\" - - - %dd".printf (downloads_folder, downloads_cleanup_days);

FileOutputStream os = config_stream.output_stream as FileOutputStream;
try {
yield os.write_all_async (config.data, Priority.DEFAULT, null, null);
yield os.write_all_async (cleanup_config.data (), Priority.DEFAULT, null, null);
} catch (Error e) {
warning ("Unable to write systemd-tmpfiles config: %s", e.message);
}
}

private class CleanupConfig : Object {
public bool clean_downloads { private get; public construct; }
public bool clean_screenshots { private get; public construct; }
public int clean_after_days { private get; public construct; }

public bool is_disabled { get {
return (!clean_downloads && !clean_screenshots) || clean_after_days < 1;
}}

public CleanupConfig (Settings settings) {
Object (
clean_downloads: settings.get_boolean ("cleanup-downloads-folder")
&& downloads_are_not_home (),
clean_screenshots: settings.get_boolean ("cleanup-screenshots-folder"),
clean_after_days: settings.get_int ("old-files-age")
);
}

private static bool downloads_are_not_home () {
var home_dir = Environment.get_home_dir ();
var home = File.new_for_path (home_dir);
var downloads_dir = Environment.get_user_special_dir (UserDirectory.DOWNLOAD);
var downloads = File.new_for_path (downloads_dir);
return !home.equal (downloads);
}

public uint8[] data () {
// See https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html for details
// Results in a config line like:
// e "/home/david/Downloads" - - - 30d
var template = "e \"%s\" - - - %dd";
string[] lines = {};

if (clean_downloads) {
var downloads_dir = Environment.get_user_special_dir (UserDirectory.DOWNLOAD);
lines += template.printf (downloads_dir, clean_after_days);
}

if (clean_screenshots) {
var pictures_dir = Environment.get_user_special_dir (UserDirectory.PICTURES);
var screenshots_dir = Path.build_filename (pictures_dir, _("Screenshots"));
lines += template.printf (screenshots_dir, clean_after_days);
}

return string.joinv ("\n", lines).data;
}
}
}

0 comments on commit bf083ca

Please sign in to comment.