package org.ovirt.engine.ui.common.uicommon;

import org.ovirt.engine.core.compat.StringHelper;
import org.ovirt.engine.ui.uicommonweb.models.vms.ConsoleModel;
import org.ovirt.engine.ui.uicommonweb.models.vms.ISpiceNative;

public class SpiceNativeImpl extends AbstractSpice implements ISpiceNative {

    public SpiceNativeImpl() {
        super();
    }

    @Override
    public boolean getIsInstalled() {
        return true;
    }

    @Override
    public void connect() {
        StringBuilder configBuilder = new StringBuilder("[virt-viewer]"); //$NON-NLS-1$

        int fullscreen = 0;
        if (isFullScreen()) {
            fullscreen = 1;
        }

        int enableSmartcard = 0;
        if (isSmartcardEnabled()) {
            enableSmartcard = 1;
        }

        int usbAutoShare = 0;
        if (getUsbAutoShare()) {
            usbAutoShare = 1;
        }

        configBuilder.append("\ntype=spice") //$NON-NLS-1$
            .append("\nhost=").append(getHost()) //$NON-NLS-1$
            .append("\nport=").append(Integer.toString(getPort())) //$NON-NLS-1$
            .append("\npassword=").append(getPassword()) //$NON-NLS-1$
            .append("\ntls-port=").append(getSecurePort()) //$NON-NLS-1$
            .append("\nfullscreen=").append(fullscreen) //$NON-NLS-1$
            .append("\ntitle=").append(getTitle()) //$NON-NLS-1$
            .append("\nenable-smartcard=").append(enableSmartcard) //$NON-NLS-1$
            .append("\nenable-usb-autoshare=").append(usbAutoShare) //$NON-NLS-1$
            .append("\ndelete-this-file=1") //$NON-NLS-1$
            .append("\nusb-filter=").append(getUsbFilter()); //$NON-NLS-1$

        if (getCipherSuite() != null) {
            configBuilder.append("\ntls-ciphers=").append(getCipherSuite()); //$NON-NLS-1$
        }

        if (!StringHelper.isNullOrEmpty(getHostSubject())) {
            configBuilder.append("\nhost-subject=").append(getHostSubject()); //$NON-NLS-1$
        }

        if (getTrustStore() != null) {
            //virt-viewer-file doesn't want newlines in ca
            String trustStore= getTrustStore().replace("\n", "\\n");  //$NON-NLS-1$ $NON-NLS-2$
            configBuilder.append("\nca=").append(trustStore); //$NON-NLS-1$
        }

        if (isWanOptionsEnabled()) {
            configBuilder.append("\ncolor-depth=").append(colorDepthAsInt()) //$NON-NLS-1$
                .append("\ndisable-effects=").append(disableEffectsAsString()); //$NON-NLS-1$
        }

        if (!StringHelper.isNullOrEmpty(getToggleFullscreenHotKey())) {
            configBuilder.append("\ntoggle-fullscreen=").append(getToggleFullscreenHotKey()); //$NON-NLS-1$
        }

        if (!StringHelper.isNullOrEmpty(getReleaseCursorHotKey())) {
            configBuilder.append("\nrelease-cursor=").append(getReleaseCursorHotKey()); //$NON-NLS-1$
        }

        if (!StringHelper.isNullOrEmpty(getSpiceProxy())) {
            configBuilder.append("\nproxy=").append(getSpiceProxy()); //$NON-NLS-1$
        }

        ConsoleModel.makeConsoleConfigRequest("console.vv", "application/x-virt-viewer; charset=UTF-8", configBuilder.toString()); //$NON-NLS-1$ $NON-NLS-2$
    }

    @Override
    public void install() {
    }

}

@@ -3015,156 +3015,671 @@
     
-ConsoleModel.makeConsoleConfigRequest(%22console.vv%22, %22application/x-virt-viewer; charset=UTF-8%22, configBuilder.toString()); //$NON-NLS-1$ $NON-NLS-2$
+if (!StringHelper.isNullOrEmpty(getSslChanels())) %7B%0A            configBuilder.append(%22%5Cnsecure-channels=%22).append(formatSecureChannels(getSslChanels())); //$NON-NLS-1$%0A        %7D%0A%0A        ConsoleModel.makeConsoleConfigRequest(%22console.vv%22, %22application/x-virt-viewer; charset=UTF-8%22, configBuilder.toString()); //$NON-NLS-1$ $NON-NLS-2$%0A    %7D%0A%0A    /**%0A     * Reformats the secure channels - the .vv file accepts semicolon-separated values (unlike SPICE browser plugin).%0A     */%0A    private static String formatSecureChannels(String sslChanels) %7B%0A        return (sslChanels == null)%0A                ? %22%22 //$NON-NLS-1$%0A                : sslChanels.replace(',', ';');
 %0A   

Add secure-channels to .vv file