| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
|---|
| 3 |
* All rights reserved. This program and the accompanying materials |
|---|
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|---|
| 5 |
* which accompanies this distribution, and is available at |
|---|
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
|---|
| 7 |
* |
|---|
| 8 |
* Contributors: |
|---|
| 9 |
* IBM Corporation - initial API and implementation |
|---|
| 10 |
* Port to the D programming language: |
|---|
| 11 |
* Frank Benoit <benoit@tionex.de> |
|---|
| 12 |
*******************************************************************************/ |
|---|
| 13 |
module dwt.dnd.FileTransfer; |
|---|
| 14 |
|
|---|
| 15 |
import dwt.internal.ole.win32.COM; |
|---|
| 16 |
import dwt.internal.ole.win32.OBJIDL; |
|---|
| 17 |
import dwt.internal.win32.OS; |
|---|
| 18 |
|
|---|
| 19 |
import dwt.dnd.ByteArrayTransfer; |
|---|
| 20 |
import dwt.dnd.TransferData; |
|---|
| 21 |
import dwt.dnd.DND; |
|---|
| 22 |
|
|---|
| 23 |
import dwt.dwthelper.utils; |
|---|
| 24 |
static import tango.text.Text; |
|---|
| 25 |
alias tango.text.Text.Text!(char) StringBuffer; |
|---|
| 26 |
|
|---|
| 27 |
/** |
|---|
| 28 |
* The class <code>FileTransfer</code> provides a platform specific mechanism |
|---|
| 29 |
* for converting a list of files represented as a java <code>String[]</code> to a |
|---|
| 30 |
* platform specific representation of the data and vice versa. |
|---|
| 31 |
* Each <code>String</code> in the array contains the absolute path for a single |
|---|
| 32 |
* file or directory. |
|---|
| 33 |
* |
|---|
| 34 |
* <p>An example of a java <code>String[]</code> containing a list of files is shown |
|---|
| 35 |
* below:</p> |
|---|
| 36 |
* |
|---|
| 37 |
* <code><pre> |
|---|
| 38 |
* File file1 = new File("C:\temp\file1"); |
|---|
| 39 |
* File file2 = new File("C:\temp\file2"); |
|---|
| 40 |
* String[] fileData = new String[2]; |
|---|
| 41 |
* fileData[0] = file1.getAbsolutePath(); |
|---|
| 42 |
* fileData[1] = file2.getAbsolutePath(); |
|---|
| 43 |
* </code></pre> |
|---|
| 44 |
* |
|---|
| 45 |
* @see Transfer |
|---|
| 46 |
*/ |
|---|
| 47 |
public class FileTransfer : ByteArrayTransfer { |
|---|
| 48 |
|
|---|
| 49 |
private static FileTransfer _instance; |
|---|
| 50 |
private static final String CF_HDROP = "CF_HDROP "; //$NON-NLS-1$ |
|---|
| 51 |
private static final int CF_HDROPID = COM.CF_HDROP; |
|---|
| 52 |
private static final String CF_HDROP_SEPARATOR = "\0"; //$NON-NLS-1$ |
|---|
| 53 |
|
|---|
| 54 |
private this() {} |
|---|
| 55 |
|
|---|
| 56 |
/** |
|---|
| 57 |
* Returns the singleton instance of the FileTransfer class. |
|---|
| 58 |
* |
|---|
| 59 |
* @return the singleton instance of the FileTransfer class |
|---|
| 60 |
*/ |
|---|
| 61 |
public static FileTransfer getInstance () { |
|---|
| 62 |
if( _instance is null ){ |
|---|
| 63 |
synchronized { |
|---|
| 64 |
if( _instance is null ){ |
|---|
| 65 |
_instance = new FileTransfer(); |
|---|
| 66 |
} |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
return _instance; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
/** |
|---|
| 73 |
* This implementation of <code>javaToNative</code> converts a list of file names |
|---|
| 74 |
* represented by a java <code>String[]</code> to a platform specific representation. |
|---|
| 75 |
* Each <code>String</code> in the array contains the absolute path for a single |
|---|
| 76 |
* file or directory. |
|---|
| 77 |
* |
|---|
| 78 |
* @param object a java <code>String[]</code> containing the file names to be converted |
|---|
| 79 |
* @param transferData an empty <code>TransferData</code> object that will |
|---|
| 80 |
* be filled in on return with the platform specific format of the data |
|---|
| 81 |
* |
|---|
| 82 |
* @see Transfer#nativeToJava |
|---|
| 83 |
*/ |
|---|
| 84 |
public void javaToNative(Object object, TransferData transferData) { |
|---|
| 85 |
if (!checkFile(object) || !isSupportedType(transferData)) { |
|---|
| 86 |
DND.error(DND.ERROR_INVALID_DATA); |
|---|
| 87 |
} |
|---|
| 88 |
String[] fileNames; |
|---|
| 89 |
if( auto strs = cast(ArrayWrapperString2) object ){ |
|---|
| 90 |
fileNames = strs.array; |
|---|
| 91 |
} |
|---|
| 92 |
StringBuffer allFiles = new StringBuffer(); |
|---|
| 93 |
for (int i = 0; i < fileNames.length; i++) { |
|---|
| 94 |
allFiles.append(fileNames[i]); |
|---|
| 95 |
allFiles.append(CF_HDROP_SEPARATOR); // each name is null terminated |
|---|
| 96 |
} |
|---|
| 97 |
TCHAR[] buffer = StrToTCHARs(0, allFiles.toString(), true); // there is an extra null terminator at the very end |
|---|
| 98 |
DROPFILES dropfiles; |
|---|
| 99 |
dropfiles.pFiles = DROPFILES.sizeof; |
|---|
| 100 |
dropfiles.pt.x = dropfiles.pt.y = 0; |
|---|
| 101 |
dropfiles.fNC = 0; |
|---|
| 102 |
dropfiles.fWide = OS.IsUnicode ? 1 : 0; |
|---|
| 103 |
// Allocate the memory because the caller (DropTarget) has not handed it in |
|---|
| 104 |
// The caller of this method must release the data when it is done with it. |
|---|
| 105 |
int byteCount = buffer.length * TCHAR.sizeof; |
|---|
| 106 |
auto newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, DROPFILES.sizeof + byteCount); |
|---|
| 107 |
OS.MoveMemory(newPtr, &dropfiles, DROPFILES.sizeof); |
|---|
| 108 |
OS.MoveMemory(newPtr + DROPFILES.sizeof, buffer.ptr, byteCount); |
|---|
| 109 |
transferData.stgmedium = new STGMEDIUM(); |
|---|
| 110 |
transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 111 |
transferData.stgmedium.unionField = newPtr; |
|---|
| 112 |
transferData.stgmedium.pUnkForRelease = null; |
|---|
| 113 |
transferData.result = COM.S_OK; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
/** |
|---|
| 117 |
* This implementation of <code>nativeToJava</code> converts a platform specific |
|---|
| 118 |
* representation of a list of file names to a java <code>String[]</code>. |
|---|
| 119 |
* Each String in the array contains the absolute path for a single file or directory. |
|---|
| 120 |
* |
|---|
| 121 |
* @param transferData the platform specific representation of the data to be converted |
|---|
| 122 |
* @return a java <code>String[]</code> containing a list of file names if the conversion |
|---|
| 123 |
* was successful; otherwise null |
|---|
| 124 |
* |
|---|
| 125 |
* @see Transfer#javaToNative |
|---|
| 126 |
*/ |
|---|
| 127 |
public Object nativeToJava(TransferData transferData) { |
|---|
| 128 |
if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null; |
|---|
| 129 |
|
|---|
| 130 |
// get file names from IDataObject |
|---|
| 131 |
IDataObject dataObject = transferData.pIDataObject; |
|---|
| 132 |
dataObject.AddRef(); |
|---|
| 133 |
FORMATETC* formatetc = new FORMATETC(); |
|---|
| 134 |
formatetc.cfFormat = COM.CF_HDROP; |
|---|
| 135 |
formatetc.ptd = null; |
|---|
| 136 |
formatetc.dwAspect = COM.DVASPECT_CONTENT; |
|---|
| 137 |
formatetc.lindex = -1; |
|---|
| 138 |
formatetc.tymed = COM.TYMED_HGLOBAL; |
|---|
| 139 |
STGMEDIUM* stgmedium = new STGMEDIUM(); |
|---|
| 140 |
stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 141 |
transferData.result = getData(dataObject, formatetc, stgmedium); |
|---|
| 142 |
dataObject.Release(); |
|---|
| 143 |
if (transferData.result !is COM.S_OK) return null; |
|---|
| 144 |
// How many files are there? |
|---|
| 145 |
int count = OS.DragQueryFile(stgmedium.unionField, 0xFFFFFFFF, null, 0); |
|---|
| 146 |
String[] fileNames = new String[](count); |
|---|
| 147 |
for (int i = 0; i < count; i++) { |
|---|
| 148 |
// How long is the name ? |
|---|
| 149 |
int size = OS.DragQueryFile(stgmedium.unionField, i, null, 0) + 1; |
|---|
| 150 |
TCHAR[] lpszFile = NewTCHARs(0, size); |
|---|
| 151 |
// Get file name and append it to string |
|---|
| 152 |
OS.DragQueryFile(stgmedium.unionField, i, lpszFile.ptr, size); |
|---|
| 153 |
fileNames[i] = TCHARzToStr(lpszFile.ptr); |
|---|
| 154 |
} |
|---|
| 155 |
OS.DragFinish(stgmedium.unionField); // frees data associated with HDROP data |
|---|
| 156 |
return new ArrayWrapperString2(fileNames); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
protected int[] getTypeIds(){ |
|---|
| 160 |
return [CF_HDROPID]; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
protected String[] getTypeNames(){ |
|---|
| 164 |
return [CF_HDROP]; |
|---|
| 165 |
} |
|---|
| 166 |
bool checkFile(Object object) { |
|---|
| 167 |
String[] strings; |
|---|
| 168 |
if( auto strs = cast(ArrayWrapperString2)object ){ |
|---|
| 169 |
strings = strs.array; |
|---|
| 170 |
} |
|---|
| 171 |
if (!strings) return false; |
|---|
| 172 |
for (int i = 0; i < strings.length; i++) { |
|---|
| 173 |
if (strings[i] is null || strings[i].length is 0) return false; |
|---|
| 174 |
} |
|---|
| 175 |
return true; |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
protected bool validate(Object object) { |
|---|
| 179 |
return checkFile(object); |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|