001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com>
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.config.to;
019
020import java.io.File;
021import java.util.Map;
022import java.util.TreeMap;
023
024import org.simpleframework.xml.Element;
025import org.simpleframework.xml.ElementMap;
026import org.simpleframework.xml.Root;
027import org.simpleframework.xml.convert.AnnotationStrategy;
028import org.simpleframework.xml.convert.Convert;
029import org.simpleframework.xml.core.Persister;
030import org.syncany.config.ConfigException;
031import org.syncany.crypto.SaltedSecretKey;
032import org.syncany.crypto.SaltedSecretKeyConverter;
033
034/**
035 * The user config transfer object is a helper data structure that allows storing
036 * a user's global system settings such as system properties.
037 *
038 * <p>It uses the Simple framework for XML serialization, and its corresponding
039 * annotation-based configuration.
040 *
041 * @see <a href="http://simple.sourceforge.net/">Simple framework</a>
042 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
043 */
044@Root(name = "userConfig", strict = false)
045public class UserConfigTO {
046        @ElementMap(name = "systemProperties", entry = "property", key = "name", required = false, attribute = true)
047        private TreeMap<String, String> systemProperties;
048
049        @Element(name = "preventStandby", required = false)
050        private boolean preventStandby;
051
052        @Element(name = "configEncryptionKey", required = true)
053        @Convert(SaltedSecretKeyConverter.class)
054        private SaltedSecretKey configEncryptionKey;
055
056        public UserConfigTO() {
057                this.systemProperties = new TreeMap<String, String>();
058                this.preventStandby = false;
059        }
060
061        public Map<String, String> getSystemProperties() {
062                return systemProperties;
063        }
064
065        public boolean isPreventStandby() {
066                return preventStandby;
067        }
068        
069        public void setPreventStandby(boolean preventStandby) {
070                this.preventStandby = preventStandby;
071        }
072
073        public SaltedSecretKey getConfigEncryptionKey() {
074                return configEncryptionKey;
075        }
076
077        public void setConfigEncryptionKey(SaltedSecretKey configEncryptionKey) {
078                this.configEncryptionKey = configEncryptionKey;
079        }
080
081        public static UserConfigTO load(File file) throws ConfigException {
082                try {
083                        return new Persister(new AnnotationStrategy()).read(UserConfigTO.class, file);
084                }
085                catch (Exception e) {
086                        throw new ConfigException("User config file cannot be read or is invalid: " + file, e);
087                }
088        }
089
090        public void save(File file) throws ConfigException {
091                try {
092                        new Persister(new AnnotationStrategy()).write(this, file);
093                }
094                catch (Exception e) {
095                        throw new ConfigException("Cannot write user config to file " + file, e);
096                }
097        }
098}