dino / dino

Modern XMPP ("Jabber") Chat Client using GTK+/Vala
https://dino.im/
GNU General Public License v3.0
2.21k stars 251 forks source link

Add option or change Avatar storage to .cache permanently #1615

Open Earthw0rmJ1m opened 3 weeks ago

Earthw0rmJ1m commented 3 weeks ago

Either change the avatar storage permantly to .cache or add option in GUI for user to do so. Reasoning for privacy or distro specific like using Dino on TailsOS. Or for users that have .cache mounted to tmpfs. Most XMPP client aplications stores avatars in .cache.

Step 1: Modify avatar_manager.vala

public class AvatarManager {
    private string avatar_directory;

    public AvatarManager() {
        // Change the path to store avatars in .cache instead of .local/share
        avatar_directory = GLib.get_home_dir() + "/.cache/dino/avatars";
        // Ensure the directory exists
        GLib.file_test(avatar_directory, GLib.FileTest.IS_DIR) || GLib.mkdir(avatar_directory, 0755);
    }

    public void save_avatar(string user_id, Image avatar) {
        string avatar_path = avatar_directory + "/" + user_id + ".png";
        // Save the avatar image to the new path
        avatar.save(avatar_path);
    }

    public Image load_avatar(string user_id) {
        string avatar_path = avatar_directory + "/" + user_id + ".png";
        // Load the avatar image from the new path
        return Image.load(avatar_path);
    }
}

Step 2: Update GUI Options settings.vala

public class SettingsWindow : Gtk.Window {
    private Gtk.CheckButton cache_option;
    private bool use_cache;

    public SettingsWindow() {
        // Initialize the settings window
        this.title = "Settings";
        this.set_default_size(400, 300);

        // Create a vertical box to hold the settings
        var vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 10);
        this.add(vbox);

        // Create a checkbox for the cache option
        cache_option = new Gtk.CheckButton.with_label("Store avatars in .cache");

        // Load the current setting (default to false if not set)
        use_cache = load_use_cache_setting();
        cache_option.set_active(use_cache);

        // Connect the toggle signal to update the storage path
        cache_option.signal_toggled().connect(() => {
            use_cache = cache_option.get_active();
            update_avatar_storage_path(use_cache);
            save_use_cache_setting(use_cache);
        });

        // Add the checkbox to the settings window
        vbox.append(cache_option);

        // Add a button to close the settings window
        var close_button = new Gtk.Button.with_label("Close");
        close_button.signal_clicked().connect(() => {
            this.close();
        });
        vbox.append(close_button);
    }

    private bool load_use_cache_setting() {
        // Load the setting from a configuration file or use a default value
        // This is a placeholder; implement actual loading logic
        return false; // Default to false (not using cache)
    }

    private void save_use_cache_setting(bool use_cache) {
        // Save the setting to a configuration file
        // This is a placeholder; implement actual saving logic
    }

    private void update_avatar_storage_path(bool use_cache) {
        if (use_cache) {
            AvatarManager.set_storage_path(GLib.get_home_dir() + "/.cache/dino/avatars");
        } else {
            AvatarManager.set_storage_path(GLib.get_home_dir() + "/.local/share/dino/avatars");
        }
    }
}

(or like this):

public class SettingsWindow : Gtk.Window {
    private Gtk.CheckButton cache_option;

    public SettingsWindow() {
        // Create a checkbox for the cache option
        cache_option = new Gtk.CheckButton.with_label("Store avatars in .cache");
        cache_option.set_active(true); // Default to true

        // Connect the toggle signal to update the storage path
        cache_option.signal_toggled().connect(() => {
            if (cache_option.get_active()) {
                // Update the avatar manager to use .cache
                AvatarManager.set_storage_path(GLib.get_home_dir() + "/.cache/dino/avatars");
            } else {
                // Revert to the default path
                AvatarManager.set_storage_path(GLib.get_home_dir() + "/.local/share/dino/avatars");
            }
        });

        // Add the checkbox to the settings window
        this.add(cache_option);
    }
}
eerielili commented 19 hours ago

I like the idea of separating media files in ~/.local/share and avatars (less important) in .cache. It also does shorten the path to rm -rf in case of an evil user putting illegal material in his profile picture and distributing it via caching. Nuke ~/.cache and it's done.