@@ -81,12 +81,12 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
81
81
" io.elementary.settings-daemon.downloads-folder.conf"
82
82
);
83
83
84
- var downloads_cleanup_enabled = housekeeping_settings . get_boolean ( " cleanup-downloads-folder " );
84
+ var cleanup_config = new CleanupConfig (housekeeping_settings );
85
85
86
86
var config_file = File . new_for_path (config_path);
87
87
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
90
90
return ;
91
91
}
92
92
@@ -98,22 +98,8 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
98
98
}
99
99
}
100
100
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) {
117
103
try {
118
104
yield config_file. delete_async ();
119
105
} catch (Error e) {
@@ -133,16 +119,59 @@ public class SettingsDaemon.Backends.Housekeeping : Object {
133
119
return ;
134
120
}
135
121
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
-
141
122
FileOutputStream os = config_stream. output_stream as FileOutputStream ;
142
123
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 );
144
125
} catch (Error e) {
145
126
warning (" Unable to write systemd-tmpfiles config: %s " , e. message);
146
127
}
147
128
}
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
+ }
148
177
}
0 commit comments