Changeset 13007 in josm


Ignore:
Timestamp:
2017-10-16T21:45:44+02:00 (7 years ago)
Author:
bastiK
Message:

fixed #15436 - Store cache data of plugins in cache location

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/Plugin.java

    r12627 r13007  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.plugins;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.io.File;
     
    1921import org.openstreetmap.josm.gui.download.DownloadSelection;
    2022import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
     23import org.openstreetmap.josm.spi.preferences.Config;
     24import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
     25import org.openstreetmap.josm.tools.Logging;
    2126import org.openstreetmap.josm.tools.Utils;
    2227
     
    5358    private PluginInformation info;
    5459
     60    private final IBaseDirectories pluginBaseDirectories = new PluginBaseDirectories();
     61
     62    private class PluginBaseDirectories implements IBaseDirectories {
     63        private File preferencesDir;
     64        private File cacheDir;
     65        private File userdataDir;
     66
     67        @Override
     68        public File getPreferencesDirectory(boolean createIfMissing) {
     69            if (preferencesDir == null) {
     70                preferencesDir = Config.getDirs().getPreferencesDirectory(createIfMissing).toPath()
     71                        .resolve("plugins").resolve(info.name).toFile();
     72            }
     73            if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) {
     74                Logging.error(tr("Failed to create missing plugin preferences directory: {0}", preferencesDir.getAbsoluteFile()));
     75            }
     76            return preferencesDir;
     77        }
     78
     79        @Override
     80        public File getUserDataDirectory(boolean createIfMissing) {
     81            if (userdataDir == null) {
     82                userdataDir = Config.getDirs().getUserDataDirectory(createIfMissing).toPath()
     83                        .resolve("plugins").resolve(info.name).toFile();
     84            }
     85            if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) {
     86                Logging.error(tr("Failed to create missing plugin user data directory: {0}", userdataDir.getAbsoluteFile()));
     87            }
     88            return userdataDir;
     89        }
     90
     91        @Override
     92        public File getCacheDirectory(boolean createIfMissing) {
     93            if (cacheDir == null) {
     94                cacheDir = Config.getDirs().getCacheDirectory(createIfMissing).toPath()
     95                        .resolve("plugins").resolve(info.name).toFile();
     96            }
     97            if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) {
     98                Logging.error(tr("Failed to create missing plugin cache directory: {0}", cacheDir.getAbsoluteFile()));
     99            }
     100            return cacheDir;
     101        }
     102    }
     103
    55104    /**
    56105     * Creates the plugin
     
    81130
    82131    /**
     132     * Get the directories where this plugin can store various files.
     133     * @return the directories where this plugin can store files
     134     * @since 13007
     135     */
     136    public IBaseDirectories getPluginDirs() {
     137        return pluginBaseDirectories;
     138    }
     139
     140    /**
    83141     * @return The directory for the plugin to store all kind of stuff.
    84      */
     142     * @deprecated (since 13007) to get the same directory as this method, use {@code getPluginDirectories().getUserDataDirectory(false)}.
     143     * However, for files that can be characterized as cache or preferences, you are encouraged to use the appropriate
     144     * {@link IBaseDirectories} method from {@link #getPluginDirectories()}.
     145     */
     146    @Deprecated
    85147    public String getPluginDir() {
    86148        return new File(Main.pref.getPluginsDirectory(), info.name).getPath();
Note: See TracChangeset for help on using the changeset viewer.