Code On Fire Passionate About Cool Code

28Sep/090

Drag Server Side File to Local Explorer in Air

Before we complicate things by discussing the actual file download, let’s take a quick look at how the drag & drop functionality works.

Drag & Drop Model
When dragging and dropping items between different windows, whether it be in Windows, Mac or Linux, a standardized process seems to be in place to handle it.

First, it would be a breach in the application security sandbox if an application could access the drop event of something that was drag off itself and onto another window. For this reason, if you drag an element from the Air application window to any other window on the OS, you will only be able to access the onDragStart event and will never have access to the actual drop event.

The method for communicating with the application on the receiving end of the drag drop operation is via the clipboard. As soon as a drag event off the air window is detected, one should populate the clipboard with a set of information you wish to send to the receiving application.

File Copy Clipboard Process
Next we take a look at what the general process is whenever files are copied from one window to another on most platforms. The clipboard must be populated with an array of file location objects (that actually exist on the machine). When dropped, the receiving application will perform a copy or move operation on the array of file locations sitting in wait in the clipboard.

The issue that becomes immediately obvious from this is that when dragging a file or a set of files from an air application to the desktop/explorer, the file might not be available locally as it might represent files or folders residing on the server side. Since we cannot capture the drop event on the receiving application, and since the files don’t reside locally at the moment the drag event starts, there is no information we can put in the clipboard that will cause the receiving application to download the files being dragged.

Clipboard data handler function
The clipboard handler, fortunately allows one to provide a function rather than actual data when setting it. The clipboard will then execute the function and take the result of that function and place it in the clipboard. This is what saves our butts here.

The idea is to create a function that will:

  1. Asynchronously get each file in the drag list from the server and save it to a local, temporary location.
  2. Generating a list of these now local files in the temporary folder after all the downloads have been completed.
  3. Return this file list as a clipboard object and specify the move rather than the copy method.

This function should then be passed as the clipboard data handler function.

There are still some issues surrounding this method (a period during which the application might freeze if you opt for using synchronous file downloading) that could be dealt with by displaying information about the progress of the file download using the same function passed to the clipboard.

Onwards  & Upwards!

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment



No trackbacks yet.