Mercurial > projects > dwt-mac
comparison dwt/dnd/FileTransfer.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 1a8b3cb347e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 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 * Outhink - support for typeFileURL | |
11 *******************************************************************************/ | |
12 module dwt.dnd; | |
13 | |
14 import java.io.*; | |
15 import dwt.internal.carbon.*; | |
16 | |
17 /** | |
18 * The class <code>FileTransfer</code> provides a platform specific mechanism | |
19 * for converting a list of files represented as a java <code>String[]</code> to a | |
20 * platform specific representation of the data and vice versa. | |
21 * Each <code>String</code> in the array contains the absolute path for a single | |
22 * file or directory. | |
23 * See <code>Transfer</code> for additional information. | |
24 * | |
25 * <p>An example of a java <code>String[]</code> containing a list of files is shown | |
26 * below:</p> | |
27 * | |
28 * <code><pre> | |
29 * File file1 = new File("C:\temp\file1"); | |
30 * File file2 = new File("C:\temp\file2"); | |
31 * String[] fileData = new String[2]; | |
32 * fileData[0] = file1.getAbsolutePath(); | |
33 * fileData[1] = file2.getAbsolutePath(); | |
34 * </code></pre> | |
35 */ | |
36 public class FileTransfer : ByteArrayTransfer { | |
37 | |
38 static FileTransfer _instance = new FileTransfer(); | |
39 static final String HFS = "hfs "; //$NON-NLS-1$ | |
40 static final String FURL = "furl"; //$NON-NLS-1$ | |
41 static final int HFSID = registerType(HFS); | |
42 static final int FURLID = registerType(FURL); | |
43 | |
44 FileTransfer() {} | |
45 | |
46 /** | |
47 * Returns the singleton instance of the FileTransfer class. | |
48 * | |
49 * @return the singleton instance of the FileTransfer class | |
50 */ | |
51 public static FileTransfer getInstance () { | |
52 return _instance; | |
53 } | |
54 | |
55 /** | |
56 * This implementation of <code>javaToNative</code> converts a list of file names | |
57 * represented by a java <code>String[]</code> to a platform specific representation. | |
58 * Each <code>String</code> in the array contains the absolute path for a single | |
59 * file or directory. For additional information see | |
60 * <code>Transfer#javaToNative</code>. | |
61 * | |
62 * @param object a java <code>String[]</code> containing the file names to be | |
63 * converted | |
64 * @param transferData an empty <code>TransferData</code> object; this | |
65 * object will be filled in on return with the platform specific format of the data | |
66 */ | |
67 public void javaToNative(Object object, TransferData transferData) { | |
68 if (!checkFile(object) || !isSupportedType(transferData)) { | |
69 DND.error(DND.ERROR_INVALID_DATA); | |
70 } | |
71 String[] files = (String[])object; | |
72 transferData.result = -1; | |
73 byte[][] data = new byte[files.length][]; | |
74 for (int i = 0; i < data.length; i++) { | |
75 File file = new File(files[i]); | |
76 bool isDirectory = file.isDirectory(); | |
77 String fileName = files[i]; | |
78 char [] chars = new char [fileName.length ()]; | |
79 fileName.getChars (0, chars.length, chars, 0); | |
80 int cfString = OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, chars, chars.length); | |
81 if (cfString is 0) return; | |
82 try { | |
83 int url = OS.CFURLCreateWithFileSystemPath(OS.kCFAllocatorDefault, cfString, OS.kCFURLPOSIXPathStyle, isDirectory); | |
84 if (url is 0) return; | |
85 try { | |
86 if (transferData.type is HFSID) { | |
87 byte[] fsRef = new byte[80]; | |
88 if (!OS.CFURLGetFSRef(url, fsRef)) return; | |
89 byte[] fsSpec = new byte[70]; | |
90 if (OS.FSGetCatalogInfo(fsRef, 0, null, null, fsSpec, null) !is OS.noErr) return; | |
91 byte[] hfsflavor = new byte[10 + fsSpec.length]; | |
92 byte[] finfo = new byte[16]; | |
93 OS.FSpGetFInfo(fsSpec, finfo); | |
94 System.arraycopy(finfo, 0, hfsflavor, 0, 10); | |
95 System.arraycopy(fsSpec, 0, hfsflavor, 10, fsSpec.length); | |
96 data[i] = hfsflavor; | |
97 } | |
98 if (transferData.type is FURLID) { | |
99 int encoding = OS.CFStringGetSystemEncoding(); | |
100 int theData = OS.CFURLCreateData(OS.kCFAllocatorDefault, url, encoding, true); | |
101 if (theData is 0) return; | |
102 try { | |
103 int length = OS.CFDataGetLength(theData); | |
104 byte[] buffer = new byte[length]; | |
105 CFRange range = new CFRange(); | |
106 range.length = length; | |
107 OS.CFDataGetBytes(theData, range, buffer); | |
108 data[i] = buffer; | |
109 } finally { | |
110 OS.CFRelease(theData); | |
111 } | |
112 } | |
113 } finally { | |
114 OS.CFRelease(url); | |
115 } | |
116 } finally { | |
117 OS.CFRelease(cfString); | |
118 } | |
119 } | |
120 transferData.data = data; | |
121 transferData.result = 0; | |
122 } | |
123 /** | |
124 * This implementation of <code>nativeToJava</code> converts a platform specific | |
125 * representation of a list of file names to a java <code>String[]</code>. | |
126 * Each String in the array contains the absolute path for a single file or directory. | |
127 * For additional information see <code>Transfer#nativeToJava</code>. | |
128 * | |
129 * @param transferData the platform specific representation of the data to be | |
130 * been converted | |
131 * @return a java <code>String[]</code> containing a list of file names if the | |
132 * conversion was successful; otherwise null | |
133 */ | |
134 public Object nativeToJava(TransferData transferData) { | |
135 if (!isSupportedType(transferData) || transferData.data is null) return null; | |
136 if (transferData.data.length is 0) return null; | |
137 int count = transferData.data.length; | |
138 String[] fileNames = new String[count]; | |
139 for (int i=0; i<count; i++) { | |
140 byte[] data = transferData.data[i]; | |
141 int url = 0; | |
142 if (transferData.type is HFSID) { | |
143 byte[] fsspec = new byte[data.length - 10]; | |
144 System.arraycopy(data, 10, fsspec, 0, fsspec.length); | |
145 byte[] fsRef = new byte[80]; | |
146 if (OS.FSpMakeFSRef(fsspec, fsRef) !is OS.noErr) return null; | |
147 url = OS.CFURLCreateFromFSRef(OS.kCFAllocatorDefault, fsRef); | |
148 if (url is 0) return null; | |
149 } | |
150 if (transferData.type is FURLID) { | |
151 int encoding = OS.kCFStringEncodingUTF8; | |
152 url = OS.CFURLCreateWithBytes(OS.kCFAllocatorDefault, data, data.length, encoding, 0); | |
153 if (url is 0) return null; | |
154 } | |
155 try { | |
156 int path = OS.CFURLCopyFileSystemPath(url, OS.kCFURLPOSIXPathStyle); | |
157 if (path is 0) return null; | |
158 try { | |
159 int length = OS.CFStringGetLength(path); | |
160 if (length is 0) return null; | |
161 char[] buffer= new char[length]; | |
162 CFRange range = new CFRange(); | |
163 range.length = length; | |
164 OS.CFStringGetCharacters(path, range, buffer); | |
165 fileNames[i] = new String(buffer); | |
166 } finally { | |
167 OS.CFRelease(path); | |
168 } | |
169 } finally { | |
170 OS.CFRelease(url); | |
171 } | |
172 } | |
173 return fileNames; | |
174 } | |
175 | |
176 protected int[] getTypeIds(){ | |
177 return new int[] {FURLID, HFSID}; | |
178 } | |
179 | |
180 protected String[] getTypeNames(){ | |
181 return new String[] {FURL, HFS}; | |
182 } | |
183 | |
184 bool checkFile(Object object) { | |
185 if (object is null || !(object instanceof String[]) || ((String[])object).length is 0) return false; | |
186 String[] Strings = (String[])object; | |
187 for (int i = 0; i < Strings.length; i++) { | |
188 if (Strings[i] is null || Strings[i].length() is 0) return false; | |
189 } | |
190 return true; | |
191 } | |
192 | |
193 protected bool validate(Object object) { | |
194 return checkFile(object); | |
195 } | |
196 } |