Skip to content

Commit

Permalink
Store avatars in the user's cache directory. (#1621)
Browse files Browse the repository at this point in the history
* Store avatars in the user's cache directory.

	- Not anymore in ~/.local/share, where media files are stored.
	- Already existing ~/.local/share/dino/avatars directory will be
	  moved to ~/.cache/dino/avatars
	- If both directories already exists, the old one (in
	  ~/.local/share) is removed.

* Simplify old-to-new-location logic

---------

Co-authored-by: fiaxh <[email protected]>
  • Loading branch information
eerielili and fiaxh authored Sep 28, 2024
1 parent 8853ffe commit 439c375
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
35 changes: 33 additions & 2 deletions libdino/src/service/avatar_manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,39 @@ public class AvatarManager : StreamInteractionModule, Object {
private AvatarManager(StreamInteractor stream_interactor, Database db) {
this.stream_interactor = stream_interactor;
this.db = db;
this.folder = Path.build_filename(Dino.get_storage_dir(), "avatars");
DirUtils.create_with_parents(this.folder, 0700);

File old_avatars = File.new_build_filename(Dino.get_storage_dir(), "avatars");
File new_avatars = File.new_build_filename(Dino.get_cache_dir(), "avatars");
this.folder = new_avatars.get_path();

// Move old avatar location to new one
if (old_avatars.query_exists()) {
if (!new_avatars.query_exists()) {
// Move old avatars folder (~/.local/share/dino) to new location (~/.cache/dino)
try {
new_avatars.get_parent().make_directory_with_parents();
} catch (Error e) { }
try {
old_avatars.move(new_avatars, FileCopyFlags.NONE);
debug("Avatars directory %s moved to %s", old_avatars.get_path(), new_avatars.get_path());
} catch (Error e) { }
} else {
// If both old and new folders exist, remove the old one
try {
FileEnumerator enumerator = old_avatars.enumerate_children("standard::*", FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
FileInfo info = null;
while ((info = enumerator.next_file()) != null) {
FileUtils.remove(old_avatars.get_path() + "/" + info.get_name());
}
DirUtils.remove(old_avatars.get_path());
} catch (Error e) { }
}
}

// Create avatar folder
try {
new_avatars.make_directory_with_parents();
} catch (Error e) { }

stream_interactor.account_added.connect(on_account_added);
stream_interactor.module_manager.initialize_account_modules.connect((_, modules) => {
Expand Down
4 changes: 4 additions & 0 deletions libdino/src/util/util.vala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public static string get_storage_dir() {
return Path.build_filename(Environment.get_user_data_dir(), "dino");
}

public static string get_cache_dir() {
return Path.build_filename(Environment.get_user_cache_dir(), "dino");
}

[CCode (cname = "dino_gettext", cheader_filename = "dino_i18n.h")]
public static extern unowned string _(string s);

Expand Down

0 comments on commit 439c375

Please sign in to comment.