001package jmri.util.prefs;
002
003import java.io.File;
004import java.io.IOException;
005import jmri.profile.AuxiliaryConfiguration;
006import jmri.profile.Profile;
007import jmri.profile.ProfileUtils;
008import jmri.util.FileUtil;
009import jmri.util.node.NodeIdentity;
010
011/**
012 *
013 * @author Randall Wood
014 */
015public abstract class AbstractConfigurationProvider {
016
017    protected final Profile project;
018    private boolean privateBackedUp = false;
019    private boolean sharedBackedUp = false;
020
021    public AbstractConfigurationProvider(Profile project) {
022        this.project = project;
023    }
024
025    /**
026     * Get the {@link jmri.profile.AuxiliaryConfiguration}.
027     *
028     * @return The AuxiliaryConfiguration.
029     */
030    protected abstract AuxiliaryConfiguration getConfiguration();
031
032    protected abstract File getConfigurationFile(boolean shared);
033
034    public File getConfigurationDirectory(boolean shared) {
035        File dir;
036        if (this.project == null) {
037            dir = new File(FileUtil.getPreferencesPath(), "preferences"); // NOI18N
038        } else {
039            dir = new File(this.project.getPath(), Profile.PROFILE);
040            if (!shared) {
041                File nodeDir = new File(dir, NodeIdentity.storageIdentity());
042                if (!nodeDir.exists()) {
043                    try {
044                        if (!ProfileUtils.copyPrivateContentToCurrentIdentity(project)) {
045                            log.debug("Starting profile with new private configuration.");
046                        }
047                    } catch (IOException ex) {
048                        log.debug("Copying existing private configuration failed.");
049                    }
050                }
051                dir = new File(dir, NodeIdentity.storageIdentity());
052            }
053        }
054        FileUtil.createDirectory(dir);
055        return dir;
056    }
057
058    /**
059     * @return the privateBackedUp
060     */
061    protected boolean isPrivateBackedUp() {
062        return privateBackedUp;
063    }
064
065    /**
066     * @param privateBackedUp the privateBackedUp to set
067     */
068    protected void setPrivateBackedUp(boolean privateBackedUp) {
069        this.privateBackedUp = privateBackedUp;
070    }
071
072    /**
073     * @return the sharedBackedUp
074     */
075    protected boolean isSharedBackedUp() {
076        return sharedBackedUp;
077    }
078
079    /**
080     * @param sharedBackedUp the sharedBackedUp to set
081     */
082    protected void setSharedBackedUp(boolean sharedBackedUp) {
083        this.sharedBackedUp = sharedBackedUp;
084    }
085
086    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractConfigurationProvider.class);
087}