Allow file drag to Gmail and other HTML5 targets #67

Closed
dstillman opened this Issue Feb 27, 2012 · 9 comments

Comments

Projects
None yet
4 participants
Owner

dstillman commented Feb 27, 2012

Not sure if this is possible, but seems like it should be if it's all within Firefox.

http://forums.zotero.org/discussion/22137/

Contributor

jgrigera commented Jul 18, 2012

I've made a patch that works fine with Gmail, Google Drive and also KDE desktop (dolphin). Once another pull request I've placed is done I will place a pull request for this, in the meantime you could have a look at the patch here.
The problem (both with KDE and Gmail) seems to be x-moz-file-promise. Only by placing x-moz-file on top (even before moz-url) and adding items to mozSetDataAt I was able to make it work. I do hope the patch wont break other platforms, since moz-file-promise is still there.

--- a/chrome/content/zotero/xpcom/itemTreeView.js
+++ b/chrome/content/zotero/xpcom/itemTreeView.js
@@ -2120,18 +2120,35 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
        if (!Zotero.isWin) {
                // If at least one file is a non-web-link attachment and can be found,
                // enable dragging to file system
+               var index = 0;
                for (var i=0; i item " + i);
                        if (items[i].isAttachment()
                                        && items[i].attachmentLinkMode
                                                != Zotero.Attachments.LINK_MODE_LINKED_URL
                                        && items[i].getFile()) {
+                               
+                               var file = items[i].getFile();
+                               var fph = Components.classes["@mozilla.org/network/protocol;1?name=file"]
+                                                       .createInstance(Components.interfaces.nsIFileProtocolHandler);
+                               var uri = fph.getURLSpecFromFile(file);
+                               event.dataTransfer.mozSetDataAt("application/x-moz-file", file, index);
+                               event.dataTransfer.mozSetDataAt("text/x-moz-url", uri, index);
+                               event.dataTransfer.mozSetDataAt("text/x-moz-url-data", uri, index);
+                               event.dataTransfer.mozSetDataAt("text/x-moz-url-desc", "Zotero_" + file.leafName, index);
+                               event.dataTransfer.mozSetDataAt("application/x-moz-file-promise-url", uri, index);
+                               event.dataTransfer.mozSetDataAt("application/x-moz-file-promise-filename", file.leafName, index);
+                               Zotero.debug("--------------> Adding file " + file.leafName + " index " + index);
+                               index++;
+
+                               event.dataTransfer.effectAllowed = 'all';
                                Zotero.debug("Adding file via x-moz-file-promise");
                                event.dataTransfer.mozSetDataAt(
                                        "application/x-moz-file-promise",
                                        new Zotero.ItemTreeView.fileDragDataProvider(),
                                        0
                                );
-                               break;
+//                             break;
                        }
                }
        }
Owner

dstillman commented Jul 20, 2012

Thanks for working on this.

Are all of these extra data flavors necessary for the various targets? It'd be good to remove any that aren't strictly necessary and add comments indicating which remaining ones are for which targets.

Also, does this work for dragging multiple files?

(We should continue here, but regarding pull requests, this is one reason it's better to create topic branches for each thing you're working on, since you can then push those to your repo here and create pull requests for those independently. Even now you could always create a new local branch based on the master from before your other changes, add this patch to that, push that branch to GitHub, and create a pull request. But you can hold off on that for now.)

Contributor

jgrigera commented Jul 20, 2012

Thanks Dan.

This does work for multiple files, yes. I can clear it a bit, I guess removing xmoz-file-promise-url and xmoz-file-promise-filename wont hurt these two targets. HTML5 Drag and Drop is not always what one expects, so I will try that first, add comments and resubmit a clearer patch by the weekend.

Thanks for clarifying how to make multiple pull requests, I'll do that in the future.

Contributor

jgrigera commented Jul 21, 2012

I've now cleared other unnecessary targets (left there while trying to make this work) and suggest the following patch that enables files to be drag and drop to Gmail (and other HTML5 targets probably), and KDE (havent tried with other desktops, but probably enables them too).

--- a/chrome/content/zotero/xpcom/itemTreeView.js
+++ b/chrome/content/zotero/xpcom/itemTreeView.js
@@ -2120,18 +2120,34 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
        if (!Zotero.isWin) {
                // If at least one file is a non-web-link attachment and can be found,
                // enable dragging to file system
+               var index = 0;
                for (var i=0; i item " + i);
                        if (items[i].isAttachment()
                                        && items[i].attachmentLinkMode
                                                != Zotero.Attachments.LINK_MODE_LINKED_URL
                                        && items[i].getFile()) {
+                               
+                               var file = items[i].getFile();
+                               var fph = Components.classes["@mozilla.org/network/protocol;1?name=file"]
+                                                       .createInstance(Components.interfaces.nsIFileProtocolHandler);                                                                          
+                               var uri = fph.getURLSpecFromFile(file);                                                                                                                         
+                               // Add x-moz-file first to allow dragging on KDE and Gmail, GDrive                                                                                              
+                               Zotero.debug("Adding files via x-moz-file");                                                                                                                    
+                               event.dataTransfer.mozSetDataAt("application/x-moz-file", file, index);                                                                                         
+                               event.dataTransfer.mozSetDataAt("text/x-moz-url", uri + '\n' + file, index);                                                                                    
+                               index++;                                                                                                                                                        
+
+                               event.dataTransfer.effectAllowed = 'all';
+                               // Not sure if these below are still needed, but I am not removing them to avoid breaking other possible targets
                                Zotero.debug("Adding file via x-moz-file-promise");
                                event.dataTransfer.mozSetDataAt(
                                        "application/x-moz-file-promise",
                                        new Zotero.ItemTreeView.fileDragDataProvider(),
                                        0
                                );
-                               break;
+//                             Dont break here, we need to iterate other items to allow multiple file drag
+//                             break;
                        }
                }
        }

This patch work very well on my system Zotero 4.0.19, Firefox 27.0.1, Fedora 20 and KDE (also work with KDE desktop and Gmail). Why it is not yet included in Zotero official branch?

Could we create a pull request?

I create a pull request on branch 4.0. Pull #465 .
I can create a pull request for branch master if necessary...

Thank you!

stragu commented May 28, 2014

So, will this work for attaching pdf of papers to emails for example?

Yes, on my Linux box (Kde desktop).

2014-05-28 3:36 GMT+02:00 chtfn notifications@github.com:

So, will this work for attaching pdf of papers to emails for example?


Reply to this email directly or view it on GitHubhttps://github.com/zotero/zotero/issues/67#issuecomment-44355995
.

Mario Santagiuliana
Tel.: +39 3405581172
www.marionline.it

Are there any news about this issue? I create a lot of time ago a patch...now I restart to use Zotero and this issue is still open...which is the matter?

Thank you.

dstillman closed this in fb15c9a Oct 25, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment