view dwt/dnd/URLTransfer.d @ 255:5a30aa9820f3

removed tango.stdc.stringz imports and allow null for arrays and string arguments.
author Frank Benoit <benoit@tionex.de>
date Sun, 15 Jun 2008 22:32:20 +0200
parents ce446666f5a2
children c0d810de7093
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.dnd.URLTransfer;

import dwt.internal.gtk.OS;

import dwt.dnd.ByteArrayTransfer;
import dwt.dnd.TransferData;
import dwt.dnd.DND;

import dwt.dwthelper.utils;

/**
 * The class <code>URLTransfer</code> provides a platform specific mechanism
 * for converting text in URL format represented as a java <code>String</code>
 * to a platform specific representation of the data and vice versa.  See
 * <code>Transfer</code> for additional information. The string
 * must be a fully specified url.
 *
 * <p>An example of a java <code>String[]</code> containing a URL is shown
 * below:</p>
 *
 * <code><pre>
 *     String urlData = "http://www.eclipse.org";
 * </code></pre>
 */
public class URLTransfer : ByteArrayTransfer {

    static URLTransfer _instance;
    private static const String TEXT_UNICODE = "text/unicode"; //$NON-NLS-1$
    private static const String TEXT_XMOZURL = "text/x-moz-url"; //$NON-NLS-1$
    private static int TEXT_UNICODE_ID;
    private static int TEXT_XMOZURL_ID;

static this(){
    TEXT_UNICODE_ID = registerType(TEXT_UNICODE);
    TEXT_XMOZURL_ID = registerType(TEXT_XMOZURL);
    _instance = new URLTransfer();
}

private this() {}

/**
 * Returns the singleton instance of the URLTransfer class.
 *
 * @return the singleton instance of the URLTransfer class
 */
public static URLTransfer getInstance () {
    return _instance;
}

/**
 * This implementation of <code>javaToNative</code> converts a URL
 * represented by a java <code>String</code> to a platform specific representation.
 * For additional information see <code>Transfer#javaToNative</code>.
 *
 * @param object a java <code>String</code> containing a URL
 * @param transferData an empty <code>TransferData</code> object; this
 *  object will be filled in on return with the platform specific format of the data
 */
public void javaToNative (Object object, TransferData transferData){
    transferData.result = 0;
    if (!checkURL(object) || !isSupportedType(transferData)) {
        DND.error(DND.ERROR_INVALID_DATA);
    }
    String string = (cast(ArrayWrapperString)object).array;
    char* pValue = cast(char*)OS.g_malloc(string.length+1);
    if (pValue is null) return;
    pValue[ 0 .. string.length ] = string[];
    pValue[ string.length ] = '\0';
    transferData.length = string.length;
    transferData.format = 8;
    transferData.pValue = pValue;
    transferData.result = 1;
}

/**
 * This implementation of <code>nativeToJava</code> converts a platform specific
 * representation of a URL <code>String</code>.
 * For additional information see <code>Transfer#nativeToJava</code>.
 *
 * @param transferData the platform specific representation of the data to be
 * converted
 * @return a java <code>String</code> containing a URL if the
 * conversion was successful; otherwise null
 */
public Object nativeToJava(TransferData transferData){
    if (!isSupportedType(transferData) ||  transferData.pValue is null) return null;
    int size = (transferData.format * transferData.length / 8);
    if (size <= 0) return null;
    String string = fromStringz(cast(char*)transferData.pValue).dup;
    int end = string.indexOf('\0');
    return new ArrayWrapperString((end is -1) ? string : string.substring(0, end));
}

protected int[] getTypeIds(){
    return [TEXT_XMOZURL_ID, TEXT_UNICODE_ID];
}

protected String[] getTypeNames(){
    return [TEXT_XMOZURL, TEXT_UNICODE];
}

bool checkURL(Object object) {
    return object !is null && (null !is cast(ArrayWrapperString)object) && (cast(ArrayWrapperString)object).array.length > 0;
}

protected bool validate(Object object) {
    return checkURL(object);
}
}