[polari/wip/fmuellner/paste-confirmation: 12/16] pasteManager: Handle file drops



commit feb3de777edc9280ae4738321e98eee94f16ec5c
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Feb 12 03:13:01 2016 +0100

    pasteManager: Handle file drops

 src/pasteManager.js |   60 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 50 insertions(+), 10 deletions(-)
---
diff --git a/src/pasteManager.js b/src/pasteManager.js
index b591524..15f6a5a 100644
--- a/src/pasteManager.js
+++ b/src/pasteManager.js
@@ -19,6 +19,16 @@ const DndTargetType = {
     IMAGE: 3,
 };
 
+function _getTargetForContentType(contentType) {
+    if (Gio.content_type_is_a(contentType, 'text/plain'))
+        return DndTargetType.TEXT;
+    else if (Gio.content_type_is_a(contentType, 'image/*'))
+        return DndTargetType.IMAGE;
+    else
+       return 0;
+}
+
+
 const PasteManager = new Lang.Class({
     Name: 'PasteManager',
 
@@ -35,9 +45,48 @@ const PasteManager = new Lang.Class({
             Utils.gpaste(content, title, callback);
         } else if (content instanceof GdkPixbuf.Pixbuf) {
             Utils.imgurPaste(content, title, callback);
+        } else if (content.query_info_async) {
+            this._pasteFile(content, title, callback);
         } else {
             throw new Error('Unhandled content type');
         }
+    },
+
+    _pasteFile: function(file, title, callback) {
+        file.query_info_async(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                              Gio.FileQueryInfoFlags.NONE,
+                              GLib.PRIORITY_DEFAULT, null,
+                              Lang.bind(this, this._onFileQueryFinish, title, callback));
+    },
+
+    _onFileQueryFinish: function(file, res, title, callback) {
+        let fileInfo = null;
+        try {
+            fileInfo = file.query_info_finish(res);
+        } catch(e) {
+            return;
+        }
+
+        let contentType = fileInfo.get_content_type();
+        let targetType = _getTargetForContentType(contentType);
+
+        if (targetType == DndTargetType.TEXT) {
+            file.load_contents_async(null, Lang.bind(this,
+                function(f, res) {
+                    let [, contents, ,] = f.load_contents_finish(res);
+                    Utils.gpaste(contents.toString(), title, callback);
+                }));
+        } else if (targetType == DndTargetType.IMAGE) {
+            file.read_async(GLib.PRIORITY_DEFAULT, null, Lang.bind(this,
+                function(f, res) {
+                    let stream = f.read_finish(res);
+                    GdkPixbuf.Pixbuf.new_from_stream_async(stream, null,
+                        Lang.bind(this, function(stream, res) {
+                            let pixbuf = GdkPixbuf.Pixbuf.new_from_stream_finish(res);
+                            Utils.imgurPaste(pixbuf, title, callback);
+                        }));
+                }));
+        }
     }
 });
 
@@ -153,15 +202,6 @@ const DropTargetIface = new Lang.Interface({
         }
     },
 
-    _getTargetForContentType: function(contentType) {
-        if (Gio.content_type_is_a(contentType, 'text/plain'))
-            return DndTargetType.TEXT;
-        else if (Gio.content_type_is_a(contentType, 'image/*'))
-            return DndTargetType.IMAGE;
-        else
-           return 0;
-    },
-
     _lookupFileInfo: function(file, callback) {
         file.query_info_async(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
                               Gio.FileQueryInfoFlags.NONE,
@@ -177,7 +217,7 @@ const DropTargetIface = new Lang.Interface({
                 }
 
                 let contentType = fileInfo.get_content_type();
-                callback(this._getTargetForContentType(contentType));
+                callback(_getTargetForContentType(contentType));
             }))
     }
 });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]