Skip to content

Commit bf083ca

Browse files
committed
Implement screenshots cleanup
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.
1 parent 3301d76 commit bf083ca

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

data/io.elementary.settings-daemon.gschema.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<summary>Whether the Downloads folder should be automatically cleaned up.</summary>
3636
<description></description>
3737
</key>
38+
<key type="b" name="cleanup-screenshots-folder">
39+
<default>false</default>
40+
<summary>Whether the Screenshots folder should be automatically cleaned up.</summary>
41+
<description></description>
42+
</key>
3843
<key type="i" name="old-files-age">
3944
<default>30</default>
4045
<summary>How many days to keep an old file before it is cleaned up.</summary>

meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ gio_dep = dependency ('gio-2.0')
88
glib_dep = dependency('glib-2.0')
99
granite_dep = dependency('granite', version: '>= 5.3.0')
1010
i18n = import('i18n')
11+
add_global_arguments(
12+
'-DGETTEXT_PACKAGE="@0@"'.format (meson.project_name()),
13+
language:'c'
14+
)
1115

1216
cc = meson.get_compiler('c')
1317
m_dep = cc.find_library('m', required : false)

src/Backends/Housekeeping.vala

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
8181
"io.elementary.settings-daemon.downloads-folder.conf"
8282
);
8383

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

8686
var config_file = File.new_for_path (config_path);
8787
if (!config_file.get_parent ().query_exists ()) {
88-
if (!downloads_cleanup_enabled) {
89-
// No point continuing if cleanup isn't enabled
88+
if (cleanup_config.is_disabled) {
89+
// No point continuing if cleanup is disabled
9090
return;
9191
}
9292

@@ -98,22 +98,8 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
9898
}
9999
}
100100

101-
int downloads_cleanup_days = housekeeping_settings.get_int ("old-files-age");
102-
103-
var downloads_folder = Environment.get_user_special_dir (
104-
UserDirectory.DOWNLOAD
105-
);
106-
107-
var home_folder = Environment.get_home_dir ();
108-
if (File.new_for_path (home_folder).equal (File.new_for_path (downloads_folder))) {
109-
// TODO: Possibly throw a notification as a warning here? This will currently just silently fail
110-
// and no downloads will be cleaned up, despite the setting being enabled
111-
warning ("Downloads folder seems to point to home directory, not enabling cleanup");
112-
downloads_cleanup_enabled = false;
113-
}
114-
115-
// Delete the systemd-tmpfiles config if download cleanup is disabled
116-
if (!downloads_cleanup_enabled || downloads_cleanup_days < 1) {
101+
// Delete the systemd-tmpfiles config if cleanup is disabled
102+
if (cleanup_config.is_disabled) {
117103
try {
118104
yield config_file.delete_async ();
119105
} catch (Error e) {
@@ -133,16 +119,59 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
133119
return;
134120
}
135121

136-
// See https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html for details
137-
// Results in a config line like:
138-
// e "/home/david/Downloads" - - - 30d
139-
string config = "e \"%s\" - - - %dd".printf (downloads_folder, downloads_cleanup_days);
140-
141122
FileOutputStream os = config_stream.output_stream as FileOutputStream;
142123
try {
143-
yield os.write_all_async (config.data, Priority.DEFAULT, null, null);
124+
yield os.write_all_async (cleanup_config.data (), Priority.DEFAULT, null, null);
144125
} catch (Error e) {
145126
warning ("Unable to write systemd-tmpfiles config: %s", e.message);
146127
}
147128
}
129+
130+
private class CleanupConfig : Object {
131+
public bool clean_downloads { private get; public construct; }
132+
public bool clean_screenshots { private get; public construct; }
133+
public int clean_after_days { private get; public construct; }
134+
135+
public bool is_disabled { get {
136+
return (!clean_downloads && !clean_screenshots) || clean_after_days < 1;
137+
}}
138+
139+
public CleanupConfig (Settings settings) {
140+
Object (
141+
clean_downloads: settings.get_boolean ("cleanup-downloads-folder")
142+
&& downloads_are_not_home (),
143+
clean_screenshots: settings.get_boolean ("cleanup-screenshots-folder"),
144+
clean_after_days: settings.get_int ("old-files-age")
145+
);
146+
}
147+
148+
private static bool downloads_are_not_home () {
149+
var home_dir = Environment.get_home_dir ();
150+
var home = File.new_for_path (home_dir);
151+
var downloads_dir = Environment.get_user_special_dir (UserDirectory.DOWNLOAD);
152+
var downloads = File.new_for_path (downloads_dir);
153+
return !home.equal (downloads);
154+
}
155+
156+
public uint8[] data () {
157+
// See https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html for details
158+
// Results in a config line like:
159+
// e "/home/david/Downloads" - - - 30d
160+
var template = "e \"%s\" - - - %dd";
161+
string[] lines = {};
162+
163+
if (clean_downloads) {
164+
var downloads_dir = Environment.get_user_special_dir (UserDirectory.DOWNLOAD);
165+
lines += template.printf (downloads_dir, clean_after_days);
166+
}
167+
168+
if (clean_screenshots) {
169+
var pictures_dir = Environment.get_user_special_dir (UserDirectory.PICTURES);
170+
var screenshots_dir = Path.build_filename (pictures_dir, _("Screenshots"));
171+
lines += template.printf (screenshots_dir, clean_after_days);
172+
}
173+
174+
return string.joinv ("\n", lines).data;
175+
}
176+
}
148177
}

0 commit comments

Comments
 (0)