[polari] entryArea: Use pasteManager directly to handle pastes



commit e0b15c9d7d26f210bd045df6fe5c67b122c1221c
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Feb 11 20:37:33 2016 +0100

    entryArea: Use pasteManager directly to handle pastes
    
    Our current paste handling works pretty much in a fire-and-forget way:
    the pasted content is handed over to the pasteManager which will take
    care of uploading it and sending the resulting URL to the current channel.
    As a result, URLs are always inserted as standalone message with no way
    for users to add additional context like highlights.
    To address this, the entry will need access to the returned URL before
    the message is send - achieving this using actions that don't have
    a return value would be messy, so simply cut out the middle man and
    use the pasteManager singleton directly.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760315

 data/resources/entry-area.ui |    1 -
 src/application.js           |   18 ------------------
 src/entryArea.js             |   29 ++++++++++++++++++-----------
 src/pasteManager.js          |   13 +++++++++++++
 4 files changed, 31 insertions(+), 30 deletions(-)
---
diff --git a/data/resources/entry-area.ui b/data/resources/entry-area.ui
index 26b6b28..af10152 100644
--- a/data/resources/entry-area.ui
+++ b/data/resources/entry-area.ui
@@ -123,7 +123,6 @@
                 <property name="label" translatable="yes">_Paste</property>
                 <property name="visible">True</property>
                 <property name="receives-default">True</property>
-                <property name="action-name">app.paste-content</property>
                 <property name="use-underline">True</property>
                 <style>
                   <class name="suggested-action"/>
diff --git a/src/application.js b/src/application.js
index 706b53c..ad61436 100644
--- a/src/application.js
+++ b/src/application.js
@@ -67,9 +67,6 @@ const Application = new Lang.Class({
           { name: 'message-user',
             activate: Lang.bind(this, this._onMessageUser),
             parameter_type: GLib.VariantType.new('(sssu)') },
-          { name: 'paste-content',
-            activate: Lang.bind(this, this._onPasteContent),
-            parameter_type: GLib.VariantType.new('(ayi)') },
           { name: 'leave-room',
             activate: Lang.bind(this, this._onLeaveRoom),
             parameter_type: GLib.VariantType.new('(ss)') },
@@ -382,21 +379,6 @@ const Application = new Lang.Class({
         }));
     },
 
-    _onPasteContent: function(action, parameter) {
-        let [data, type] = parameter.deep_unpack();
-        switch (type) {
-            case PasteManager.DndTargetType.TEXT:
-                let text = data.toString();
-                this.pasteManager.pasteText(text);
-                break;
-            case PasteManager.DndTargetType.IMAGE:
-                this.pasteManager.pasteImage(data);
-                break;
-            default:
-                log('Unhandled paste content of type %d'.format(type));
-        }
-    },
-
     _onLeaveRoom: function(action, parameter) {
         let [roomId, message] = parameter.deep_unpack();
         let reason = Tp.ChannelGroupChangeReason.NONE;
diff --git a/src/entryArea.js b/src/entryArea.js
index 3e6718e..8c80aca 100644
--- a/src/entryArea.js
+++ b/src/entryArea.js
@@ -127,8 +127,8 @@ const EntryArea = new Lang.Class({
                 }
             }));
 
-        this._cancelButton.connect('clicked', Lang.bind(this, this._onButtonClicked));
-        this._pasteButton.connect('clicked', Lang.bind(this, this._onButtonClicked));
+        this._cancelButton.connect('clicked', Lang.bind(this, this._onCancelClicked));
+        this._pasteButton.connect('clicked', Lang.bind(this, this._onPasteClicked));
 
         this._pasteBox.connect_after('key-press-event', Lang.bind(this,
             function(w, event) {
@@ -216,7 +216,7 @@ const EntryArea = new Lang.Class({
     },
 
     _setPasteContent: function(content) {
-        this._pasteButton.action_target = content;
+        this._pasteContent = content;
 
         if (content) {
             this.visible_child_name = 'paste-confirmation';
@@ -233,20 +233,27 @@ const EntryArea = new Lang.Class({
             ngettext("Paste %s line of text to public paste service?",
                      "Paste %s lines of text to public paste service?",
                      nLines).format(nLines);
-        this._setPasteContent(new GLib.Variant('(ayi)', [text, PasteManager.DndTargetType.TEXT]));
+        this._setPasteContent(text);
     },
 
-    _onImagePasted: function(entry, data) {
+    _onImagePasted: function(entry, pixbuf) {
         this._confirmLabel.label = _("Upload image to public paste service?");
+        this._setPasteContent(pixbuf);
+    },
 
-        let [success, buffer] = data.save_to_bufferv('png',[],[]);
-        if (!success)
-            return;
-
-        this._setPasteContent(new GLib.Variant('(ayi)', [buffer, PasteManager.DndTargetType.IMAGE]));
+    _onPasteClicked: function() {
+        let app = Gio.Application.get_default();
+        try {
+            app.pasteManager.pasteContent(this._pasteContent);
+        } catch(e) {
+            let type = typeof this._pasteContent;
+            Utils.debug('Failed to paste content of type ' +
+                        (type == 'object' ? this._pasteContent.toString() : type));
+        }
+        this._setPasteContent(null);
     },
 
-    _onButtonClicked: function() {
+    _onCancelClicked: function() {
         this._setPasteContent(null);
     },
 
diff --git a/src/pasteManager.js b/src/pasteManager.js
index a6c5f44..b5b6398 100644
--- a/src/pasteManager.js
+++ b/src/pasteManager.js
@@ -68,6 +68,19 @@ const PasteManager = new Lang.Class({
         this._widgets.push(widget);
     },
 
+    pasteContent: function(content) {
+        if (typeof content == 'string') {
+            this.pasteText(content);
+        } else if (content instanceof GdkPixbuf.Pixbuf) {
+            let [success, buffer] = content.save_to_bufferv('png', [], []);
+            if (!success)
+                return;
+            this.pasteImage(buffer);
+        } else {
+            throw new Error('Unhandled content type');
+        }
+    },
+
     pasteText: function(text) {
         let room = this._roomManager.getActiveRoom();
         if (!room)


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