我可以将项目从Outlook拖到我的SWT应用程序中吗?

时间:2011-10-07 16:38:20

标签: drag-and-drop outlook swt

背景

我们的基于Eclipse RCP 3.6的应用程序允许人们将文件拖入存储/处理。从文件系统拖动文件时,这种方法很有效,但是当人们直接从Outlook拖动项目(邮件或附件)时则不行。

这似乎是因为Outlook希望通过FileGroupDescriptorWFileContents向我们的应用程序提供文件,但SWT仅包含FileTransfer类型。 (在FileTransfer中,只传递文件路径,假设接收者可以找到并读取它们。FileGroupDescriptorW / FileContents方法可以直接提供应用程序到应用程序的文件无需将临时文件写入磁盘。)

我们尝试生成一个可以接受ByteArrayTransferFileGroupDescriptorW的{​​{1}}子类。基于Web上的一些示例,我们能够接收和解析FileContents,其(顾名思义)描述了可用于传输的文件。 (参见下面的代码示意图。)但我们无法接受FileGroupDescriptorW

这似乎是因为Outlook仅将FileContents数据提供为FileContentsTYMED_ISTREAM,但SWT仅了解如何将数据作为TYMED_ISTORAGE进行交换。其中,TYMED_HGLOBAL似乎更为可取,因为不清楚TYMED_ISTORAGE如何提供对多个文件内容的访问权。

(我们也有一些担心SWT希望选择和转换只有一个TYMED_ISTREAM类型,因为我们需要处理两个,但我们认为我们可能会以某种方式破解Java:它似乎所有TransferData都可以在流程的其他方面获得。)

问题

我们是否在正确的轨道上?有没有人设法接受SWT中的TransferData了?我们是否有机会在不离开Java的情况下处理FileContents数据(即使通过创建基于片段的补丁或SWT的派生版本),或者我们是否必须构建一些新的本机支持代码太?

相关代码段

草图提取文件名的代码:

TYMED_ISTORAGE

在调试器中,我们看到 // THIS IS NOT PRODUCTION-QUALITY CODE - FOR ILLUSTRATION ONLY final Transfer transfer = new ByteArrayTransfer() { private final String[] typeNames = new String[] { "FileGroupDescriptorW", "FileContents" }; private final int[] typeIds = new int[] { registerType(typeNames[0]), registerType(typeNames[1]) }; @Override protected String[] getTypeNames() { return typeNames; } @Override protected int[] getTypeIds() { return typeIds; } @Override protected Object nativeToJava(TransferData transferData) { if (!isSupportedType(transferData)) return null; final byte[] buffer = (byte[]) super.nativeToJava(transferData); if (buffer == null) return null; try { final DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer)); long count = 0; for (int i = 0; i < 4; i++) { count += in.readUnsignedByte() << i; } for (int i = 0; i < count; i++) { final byte[] filenameBytes = new byte[260 * 2]; in.skipBytes(72); // probable architecture assumption(s) - may be wrong outside standard 32-bit Win XP in.read(filenameBytes); final String fileNameIncludingTrailingNulls = new String(filenameBytes, "UTF-16LE"); int stringLength = fileNameIncludingTrailingNulls.indexOf('\0'); if (stringLength == -1) stringLength = 260; final String fileName = fileNameIncludingTrailingNulls.substring(0, stringLength); System.out.println("File " + i + ": " + fileName); } in.close(); return buffer; } catch (final Exception e) { return null; } } }; 的{​​{1}}最终为ByteArrayTransfer返回isSupportedType(),因为未通过以下测试(因为其false }是FileContents):

tymed

TYMED_ISTREAM | TYMED_ISTORAGE的这段摘录让我们对一个简单的解决方案感到不那么希望了:

    if (format.cfFormat == types[i] &&
        (format.dwAspect & COM.DVASPECT_CONTENT) == COM.DVASPECT_CONTENT && 
        (format.tymed & COM.TYMED_HGLOBAL) == COM.TYMED_HGLOBAL  )
        return true;

感谢。

3 个答案:

答案 0 :(得分:2)

你看过https://bugs.eclipse.org/bugs/show_bug.cgi?id=132514吗?

附加到此bugzilla条目是一个可能感兴趣的补丁(针对相当旧的SWT版本)。

答案 1 :(得分:1)

即使

//public static final int TYMED_ISTREAM = 4;

尝试以下代码..它应该可以正常工作

package com.nagarro.jsag.poc.swtdrag;

imports ... 

public class MyTransfer extends ByteArrayTransfer {
private static int BYTES_COUNT = 592;
private static int SKIP_BYTES = 72;

private final String[] typeNames = new String[] { "FileGroupDescriptorW", "FileContents" };
private final int[] typeIds = new int[] { registerType(typeNames[0]), registerType(typeNames[1]) };

@Override
protected String[] getTypeNames() {
    return typeNames;
}

@Override
protected int[] getTypeIds() {
    return typeIds;
}

@Override
protected Object nativeToJava(TransferData transferData) {
    String[] result = null;

    if (!isSupportedType(transferData) || transferData.pIDataObject == 0)
        return null;

    IDataObject data = new IDataObject(transferData.pIDataObject);
    data.AddRef();
    // Check for descriptor format type
    try {
        FORMATETC formatetcFD = transferData.formatetc;
        STGMEDIUM stgmediumFD = new STGMEDIUM();
        stgmediumFD.tymed = COM.TYMED_HGLOBAL;
        transferData.result = data.GetData(formatetcFD, stgmediumFD);

        if (transferData.result == COM.S_OK) {
            // Check for contents format type
            long hMem = stgmediumFD.unionField;
            long fileDiscriptorPtr = OS.GlobalLock(hMem);
            int[] fileCount = new int[1];
            try {
                OS.MoveMemory(fileCount, fileDiscriptorPtr, 4);
                fileDiscriptorPtr += 4;
                result = new String[fileCount[0]];
                for (int i = 0; i < fileCount[0]; i++) {
                    String fileName = handleFile(fileDiscriptorPtr, data);
                    System.out.println("FileName : = " + fileName);
                    result[i] = fileName;
                    fileDiscriptorPtr += BYTES_COUNT;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                OS.GlobalFree(hMem);
            }
        }
    } finally {
        data.Release();
    }
    return result;
}

private String handleFile(long fileDiscriptorPtr, IDataObject data) throws Exception {

    // GetFileName
    char[] fileNameChars = new char[OS.MAX_PATH];
    byte[] fileNameBytes = new byte[OS.MAX_PATH];
    COM.MoveMemory(fileNameBytes, fileDiscriptorPtr, BYTES_COUNT);
    // Skip some bytes.
    fileNameBytes = Arrays.copyOfRange(fileNameBytes, SKIP_BYTES, fileNameBytes.length);
    String fileNameIncludingTrailingNulls = new String(fileNameBytes, "UTF-16LE");
    fileNameChars = fileNameIncludingTrailingNulls.toCharArray();
    StringBuilder builder = new StringBuilder(OS.MAX_PATH);
    for (int i = 0; fileNameChars[i] != 0 && i < fileNameChars.length; i++) {
        builder.append(fileNameChars[i]);
    }
    String name = builder.toString();

    try {
        File file = saveFileContent(name, data);
        if (file != null) {
            System.out.println("File Saved @ " + file.getAbsolutePath());
            ;
        }
    } catch (IOException e) {
        System.out.println("Count not save file content");
        ;
    }

    return name;
}

private File saveFileContent(String fileName, IDataObject data) throws IOException {
    File file = null;
    FORMATETC formatetc = new FORMATETC();
    formatetc.cfFormat = typeIds[1];
    formatetc.dwAspect = COM.DVASPECT_CONTENT;
    formatetc.lindex = 0;
    formatetc.tymed = 4; // content.

    STGMEDIUM stgmedium = new STGMEDIUM();
    stgmedium.tymed = 4;

    if (data.GetData(formatetc, stgmedium) == COM.S_OK) {
        file = new File(fileName);
        IStream iStream = new IStream(stgmedium.unionField);
        iStream.AddRef();

        try (FileOutputStream outputStream = new FileOutputStream(file)) {

            int increment = 1024 * 4;
            long pv = COM.CoTaskMemAlloc(increment);
            int[] pcbWritten = new int[1];
            while (iStream.Read(pv, increment, pcbWritten) == COM.S_OK && pcbWritten[0] > 0) {
                byte[] buffer = new byte[pcbWritten[0]];
                OS.MoveMemory(buffer, pv, pcbWritten[0]);
                outputStream.write(buffer);
            }
            COM.CoTaskMemFree(pv);

        } finally {
            iStream.Release();
        }
        return file;
    } else {
        return null;
    }
}
}

答案 2 :(得分:-1)

我遇到了同样的问题,并创建了一个小型库,为JAVA SWT提供了拖放拖放类。可以在这里找到:

https://github.com/HendrikHoetker/OutlookItemTransfer

当前,它支持将邮件项从Outlook拖放到Java SWT应用程序中,并将提供带有文件名和文件内容字节数组的OutlookItems列表。

所有内容都是纯Java且在内存中(没有临时文件)。

在SWT Java应用程序中的用法:

  if (OutlookItemTransfer.getInstance().isSupportedType(event.currentDataType)) {
      Object o = OutlookItemTransfer.getInstance().nativeToJava(event.currentDataType);
      if (o != null && o instanceof OutlookMessage[]) {
          OutlookMessage[] outlookMessages = (OutlookMessage[])o;
          for (OutlookMessage msg: outlookMessages) {
              //...
          }
      }
  }

然后,OutlookItem将提供两个元素:文件名作为字符串,文件内容作为字节数组。

从这里开始,可以将其写入文件或进一步处理字节数组。

对于上述问题: -在文件描述符中找到的是Outlook项目的文件名和指向IDataObject的指针 -可以解析IDataObject并将提供IStorage对象 -然后,IStorageObject将是一个根容器,提供类似于文件系统的更多子IStorageObject或IStream(目录= IStorage,文件= IStream

您可以在以下代码行中找到这些元素:

  • 获取文件内容,请参阅OutlookItemTransfer.java,方法nativeToJava:

    FORMATETC format = new FORMATETC();
    format.cfFormat = getTypeIds()[1];
    format.dwAspect = COM.DVASPECT_CONTENT;
    format.lindex = <fileIndex>;
    format.ptd = 0;
    format.tymed = TYMED_ISTORAGE | TYMED_ISTREAM | COM.TYMED_HGLOBAL;
    
    STGMEDIUM medium = new STGMEDIUM();
    
    if (data.GetData(format, medium) == COM.S_OK) {
        // medium.tymed will now contain TYMED_ISTORAGE
        // in medium.unionfield you will find the root IStorage
    }
    
  • 读取根目录IStorage,请参见CompoundStorage,方法readOutlookStorage:

        // open IStorage object
    IStorage storage = new IStorage(pIStorage);
    storage.AddRef();
    
    
    // walk through the content of the IStorage object
    long[] pEnumStorage = new long[1];
    if (storage.EnumElements(0, 0, 0, pEnumStorage) == COM.S_OK) {
    
        // get storage iterator
        IEnumSTATSTG enumStorage = new IEnumSTATSTG(pEnumStorage[0]);
        enumStorage.AddRef();
        enumStorage.Reset();
    
        // prepare statstg structure which tells about the object found by the iterator
        long pSTATSTG = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, STATSTG.sizeof);
        int[] fetched = new int[1];
    
        while (enumStorage.Next(1, pSTATSTG, fetched) == COM.S_OK && fetched[0] == 1) {
            // get the description of the the object found
            STATSTG statstg = new STATSTG();
            COM.MoveMemory(statstg, pSTATSTG, STATSTG.sizeof);
    
            // get the name of the object found
            String name = readPWCSName(statstg);
    
            // depending on type of object
            switch (statstg.type) {
                case COM.STGTY_STREAM: {    // load an IStream (=File)
                    long[] pIStream = new long[1];
    
                    // get the pointer to the IStream
                    if (storage.OpenStream(name, 0, COM.STGM_DIRECT | COM.STGM_READ | COM.STGM_SHARE_EXCLUSIVE, 0, pIStream) == COM.S_OK) {
                        // load the IStream
                    }
                }
    
                case COM.STGTY_STORAGE: {   // load an IStorage (=SubDirectory) - requires recursion to traverse the sub dies
    
                    }
    
                }
            }
        }
    
        // close the iterator
        enumStorage.Release();
    }
    
    // close the IStorage object
    storage.Release();