001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.preferences;
020
021import java.util.ArrayList;
022
023/**
024 * @author Stephane
025 */
026public class PluginPreferences
027{
028    /**
029     * pref id
030     */
031    private static final String PREF_ID = "plugin";
032
033    /**
034     * id
035     */
036    private static final String ID_ALLOW_BETA = "allowBeta";
037    private static final String ID_AUTO_UPDATE = "autoUpdate";
038    private static final String ID_INACTIVES_DAEMON = "inactivesdaemon";
039
040    /**
041     * preferences
042     */
043    private static XMLPreferences preferences;
044
045    public static void load()
046    {
047        // load preference
048        preferences = ApplicationPreferences.getPreferences().node(PREF_ID);
049    }
050
051    /**
052     * @return the preferences
053     */
054    public static XMLPreferences getPreferences()
055    {
056        return preferences;
057    }
058
059    public static boolean getAutomaticUpdate()
060    {
061        return preferences.getBoolean(ID_AUTO_UPDATE, true);
062    }
063
064    public static boolean getAllowBeta()
065    {
066        return preferences.getBoolean(ID_ALLOW_BETA, false);
067    }
068
069    public static ArrayList<String> getInactiveDaemons()
070    {
071        final ArrayList<String> result = new ArrayList<String>();
072
073        if (preferences.nodeExists(ID_INACTIVES_DAEMON))
074        {
075            final XMLPreferences node = preferences.node(ID_INACTIVES_DAEMON);
076
077            for (String name : node.keys())
078                if (node.getBoolean(name, false))
079                    result.add(name);
080        }
081
082        return result;
083    }
084
085    public static void setAutomaticUpdate(boolean value)
086    {
087        preferences.putBoolean(ID_AUTO_UPDATE, value);
088    }
089
090    public static void setAllowBeta(boolean value)
091    {
092        preferences.putBoolean(ID_ALLOW_BETA, value);
093    }
094
095    public static void setInactiveDaemons(ArrayList<String> names)
096    {
097        final ArrayList<String> inactives = getInactiveDaemons();
098
099        // no modification --> nothing to do
100        if ((inactives.size() == names.size()) && inactives.containsAll(names))
101            return;
102
103        final XMLPreferences node = preferences.node(ID_INACTIVES_DAEMON);
104
105        node.clear();
106        for (String name : names)
107            node.putBoolean(name, true);
108
109        // clean up all non element nodes
110        node.clean();
111    }
112
113}