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.operations.init;
019
020import java.util.logging.Level;
021import java.util.logging.Logger;
022
023import org.syncany.config.Config;
024import org.syncany.config.ConfigHelper;
025import org.syncany.config.to.ConfigTO;
026
027/**
028 * This operation generates a link which can be shared among users to connect to
029 * a repository. The operation is used by other initializing operations, e.g. connect
030 * and init.
031 *
032 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
033 */
034public class GenlinkOperation extends AbstractInitOperation {
035        private static final Logger logger = Logger.getLogger(GenlinkOperation.class.getSimpleName());
036        
037        private GenlinkOperationOptions options;
038        private ConfigTO configTO;
039
040        public GenlinkOperation(Config config, GenlinkOperationOptions options) {
041                super(config, null);
042                this.options = options;
043        }
044
045        public GenlinkOperation(ConfigTO configTO, GenlinkOperationOptions options) {
046                this((Config) null, options);
047                this.configTO = configTO;
048        }
049
050        @Override
051        public GenlinkOperationResult execute() throws Exception {
052                logger.log(Level.INFO, "");
053                logger.log(Level.INFO, "Running 'GenLink'");
054                logger.log(Level.INFO, "--------------------------------------------");
055
056                if (configTO == null) {
057                        configTO = ConfigHelper.loadConfigTO(config.getLocalDir());
058                }
059
060                ApplicationLink applicationLink = new ApplicationLink(configTO.getTransferSettings(), options.isShortUrl());
061
062                if (configTO.getMasterKey() != null) {  
063                        String encryptedLinkStr = applicationLink.createEncryptedLink(configTO.getMasterKey());
064                        return new GenlinkOperationResult(encryptedLinkStr, true);
065                }
066                else {
067                        String plaintextLinkStr = applicationLink.createPlaintextLink();
068                        return new GenlinkOperationResult(plaintextLinkStr, false);
069                }
070        }
071}