Commit 7ce2f47e authored by William Jon McCann's avatar William Jon McCann Committed by Lucas Beeler

Stores auxilliary Shotwell files in their XDG recommended locations; closes #421.

parent a17621b6
......@@ -103,6 +103,7 @@ Sven Mahlstedt <1ne_mail_an-sven.m@web.de>
Sérgio Marques <smarquespt@gmail.com>
Pablo Martin-Gomez <pablo.martin-gomez@laposte.net>
Rūdolfs Mazurs <rudolfs.mazurs@gmail.com>
William Jon McCann <william.jon.mccann@gmail.com>
Antón Méixome <meixome@certima.net>
Baurzhan Muftakhidinov <baurthefirst@gmail.com>
Sankarshan Mukhopadhyay <sankarshan@fedoraproject.org>
......
......@@ -5,7 +5,7 @@
*/
class AppDirs {
private const string DEFAULT_DATA_DIR = ".shotwell";
private const string DEFAULT_DATA_DIR = "shotwell";
private static File exec_dir;
private static File data_dir = null;
......@@ -25,7 +25,43 @@ class AppDirs {
public static File get_home_dir() {
return File.new_for_path(Environment.get_home_dir());
}
public static File get_cache_dir() {
return File.new_for_path(Environment.get_user_cache_dir()).get_child(DEFAULT_DATA_DIR);
}
public static void try_migrate_data() {
File new_dir = get_data_dir();
File old_dir = get_home_dir().get_child(".shotwell");
if (new_dir.query_exists() || !old_dir.query_exists())
return;
File cache_dir = get_cache_dir();
Posix.mode_t mask = Posix.umask(0700);
if (!cache_dir.query_exists()) {
try {
cache_dir.make_directory_with_parents(null);
} catch (Error err) {
AppWindow.panic(_("Unable to create cache directory %s: %s").printf(cache_dir.get_path(),
err.message));
}
}
GLib.FileUtils.rename(old_dir.get_child("thumbs").get_path(), cache_dir.get_child("thumbs").get_path());
if (!new_dir.get_parent().query_exists()) {
try {
new_dir.get_parent().make_directory_with_parents(null);
} catch (Error err) {
AppWindow.panic(_("Unable to create data directory %s: %s").printf(new_dir.get_parent().get_path(),
err.message));
}
}
GLib.FileUtils.rename(old_dir.get_path(), new_dir.get_path());
GLib.FileUtils.chmod(new_dir.get_path(), 0700);
Posix.umask(mask);
}
// This can only be called once, and it better be called at startup
public static void set_data_dir(string user_data_dir) requires (!is_string_empty(user_data_dir)) {
assert(data_dir == null);
......@@ -51,13 +87,24 @@ class AppDirs {
}
}
public static void verify_cache_dir() {
File cache_dir = get_cache_dir();
try {
if (!cache_dir.query_exists(null))
cache_dir.make_directory_with_parents(null);
} catch (Error err) {
AppWindow.panic(_("Unable to create cache directory %s: %s").printf(cache_dir.get_path(),
err.message));
}
}
// Return the directory in which Shotwell is installed, or null if uninstalled.
public static File? get_install_dir() {
return get_sys_install_dir(exec_dir);
}
public static File get_data_dir() {
return (data_dir == null) ? get_home_dir().get_child(DEFAULT_DATA_DIR) : data_dir;
return (data_dir == null) ? File.new_for_path(Environment.get_user_data_dir()).get_child(DEFAULT_DATA_DIR) : data_dir;
}
// The "import directory" is the same as the library directory, and are often used
......@@ -143,6 +190,22 @@ class AppDirs {
return subdir;
}
public static File get_cache_subdir(string name, string? subname = null) {
File subdir = get_cache_dir().get_child(name);
if (subname != null)
subdir = subdir.get_child(subname);
try {
if (!subdir.query_exists(null))
subdir.make_directory_with_parents(null);
} catch (Error err) {
AppWindow.panic(_("Unable to create data subdirectory %s: %s").printf(subdir.get_path(),
err.message));
}
return subdir;
}
public static File get_resources_dir() {
File? install_dir = get_install_dir();
......
......@@ -158,7 +158,7 @@ public class ThumbnailCache : Object {
private ThumbnailCache(Size size, ulong max_cached_bytes, Gdk.InterpType interp = DEFAULT_INTERP,
Jpeg.Quality quality = DEFAULT_QUALITY) {
cache_dir = AppDirs.get_data_subdir("thumbs", "thumbs%d".printf(size.get_scale()));
cache_dir = AppDirs.get_cache_subdir("thumbs", "thumbs%d".printf(size.get_scale()));
this.size = size;
this.max_cached_bytes = max_cached_bytes;
this.interp = interp;
......
......@@ -407,9 +407,12 @@ void main(string[] args) {
// set custom data directory if it's been supplied
if (CommandlineOptions.data_dir != null)
AppDirs.set_data_dir(CommandlineOptions.data_dir);
else
AppDirs.try_migrate_data();
// Verify the private data directory before continuing
AppDirs.verify_data_dir();
AppDirs.verify_cache_dir();
// init internationalization with the default system locale
InternationalSupport.init(Resources.APP_GETTEXT_PACKAGE, args);
......
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