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.daemon.messages;
019
020import java.util.logging.Level;
021
022import org.syncany.operations.daemon.EventUserInteractionListener;
023import org.syncany.operations.daemon.messages.api.ManagementRequest;
024import org.syncany.operations.daemon.messages.api.ManagementRequestHandler;
025import org.syncany.operations.daemon.messages.api.Response;
026import org.syncany.operations.init.InitOperation;
027import org.syncany.operations.init.InitOperationResult;
028
029public class InitManagementRequestHandler extends ManagementRequestHandler {
030        public InitManagementRequestHandler() {
031                // Nothing
032        }
033
034        @Override
035        public Response handleRequest(final ManagementRequest request) {
036                final InitManagementRequest concreteRequest = (InitManagementRequest) request;          
037                logger.log(Level.SEVERE, "Executing InitOperation for folder " + concreteRequest.getOptions().getLocalDir() + " ...");
038
039                Thread initThread = new Thread(new Runnable() {
040                        @Override
041                        public void run() {
042                                try {
043                                        InitOperation initOperation = new InitOperation(concreteRequest.getOptions(), new EventUserInteractionListener());
044                                        InitOperationResult operationResult = initOperation.execute();
045
046                                        switch (operationResult.getResultCode()) {
047                                        case OK:
048                                                eventBus.post(new InitManagementResponse(InitManagementResponse.OK, operationResult, request.getId()));
049                                                break;
050
051                                        case NOK_TEST_FAILED:
052                                                eventBus.post(new InitManagementResponse(InitManagementResponse.NOK_FAILED_TEST, operationResult, request.getId()));
053                                                break;
054
055                                        default:
056                                                eventBus.post(new InitManagementResponse(InitManagementResponse.NOK_FAILED_UNKNOWN, operationResult, request.getId()));
057                                                break;
058                                        }
059                                }
060                                catch (Exception e) {
061                                        logger.log(Level.WARNING, "Error adding watch to daemon config.", e);
062                                        eventBus.post(new InitManagementResponse(InitManagementResponse.NOK_OPERATION_FAILED, new InitOperationResult(), request.getId()));
063                                }
064                        }
065                }, "IntRq/" + concreteRequest.getOptions().getLocalDir().getName());
066
067                initThread.start();
068                
069                return null;
070        }
071}