[polari/wip/kunaljain/image-paste-service: 4/5] Image paste service



commit 85f111a17f125b2e6ef2f788e20c71e010b95395
Author: Kunaal Jain <kunaalus gmail com>
Date:   Sat Jan 9 09:38:16 2016 +0530

    Image paste service

 src/entryArea.js    |   23 ++++++++++++++++++++++-
 src/pasteManager.js |    2 ++
 src/utils.js        |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletions(-)
---
diff --git a/src/entryArea.js b/src/entryArea.js
index 4e62326..e8b39e4 100644
--- a/src/entryArea.js
+++ b/src/entryArea.js
@@ -18,7 +18,8 @@ const ChatEntry = new Lang.Class({
     Name: 'ChatEntry',
     Extends: Gtk.Entry,
     Signals: { 'text-pasted': { param_types: [GObject.TYPE_STRING,
-                                              GObject.TYPE_INT] } },
+                                              GObject.TYPE_INT] },
+               'image-pasted': { param_types: [GObject.TYPE_OBJECT] } },
 
     _init: function(params) {
         this.parent(params);
@@ -46,6 +47,12 @@ const ChatEntry = new Lang.Class({
                 this.emit('paste-clipboard');
                 this._useDefaultHandler = false;
             }));
+
+        clipboard.request_image(Lang.bind(this,
+            function(clipboard, pixbuf) {
+                if(pixbuf==null) return;
+                this.emit('image-pasted', pixbuf);
+            }));
     },
 });
 
@@ -120,6 +127,7 @@ const EntryArea = new Lang.Class({
 
         this._entry = new ChatEntry({ hexpand: true, activates_default: true });
         this._entry.connect('text-pasted', Lang.bind(this, this._onTextPasted));
+        this._entry.connect('image-pasted', Lang.bind(this, this._onImagePasted));
         this._entry.connect('changed', Lang.bind(this, this._onEntryChanged));
         chatBox.add(this._entry);
 
@@ -234,6 +242,19 @@ const EntryArea = new Lang.Class({
         this._pasteButton.grab_focus();
     },
 
+    _onImagePasted: function(entry, data) {
+        this._multiLinelabel.label = "Paste image to imgur paste service?";
+
+        let [success, buffer] = data.save_to_bufferv('png',[],[]);
+        if (!success)
+            return;
+        let encoded_buffer = GLib.base64_encode(buffer);
+
+        this._pasteButton.action_target = new GLib.Variant('(si)', [encoded_buffer, 1]);
+        this.widget.visible_child_name = 'multiline';
+        this._pasteButton.grab_focus();
+    },
+
     _onButtonClicked: function() {
             this._entry.text = '';
             this.widget.visible_child_name = 'default';
diff --git a/src/pasteManager.js b/src/pasteManager.js
index 94dbb1d..9f8e1a8 100644
--- a/src/pasteManager.js
+++ b/src/pasteManager.js
@@ -78,6 +78,8 @@ const PasteManager = new Lang.Class({
         let n;
         if (type==0)
             n = new UploadNotification("text");
+        else
+            n = new UploadNotification("image");
 
         app.notificationQueue.addNotification(n);
 
diff --git a/src/utils.js b/src/utils.js
index 725aad4..bfbc729 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -32,6 +32,8 @@ const Signals = imports.signals;
 
 const GPASTE_TEXT_BASEURL = 'https://paste.gnome.org/';
 
+const IMGUR_CLIENT_ID = '4109e59177ec95e';
+
 // http://daringfireball.net/2010/07/improved_regex_for_matching_urls
 const _balancedParens = '\\((?:[^\\s()<>]+|(?:\\(?:[^\\s()<>]+\\)))*\\)';
 const _leadingJunk = '[\\s`(\\[{\'\\"<\u00AB\u201C\u2018]';
@@ -139,6 +141,7 @@ function openURL(url, timestamp) {
 
 function gpaste(data, datatype, title, callback) {
     if (datatype==0) pasteText(data, title, callback);
+    else pasteImage(data, title, callback);
 }
 
 function pasteText(text, title, callback) {
@@ -170,3 +173,33 @@ function pasteText(text, title, callback) {
                 callback(null);
         });
 }
+
+function pasteImage(data, title, callback) {
+    let params = {
+        title: title,
+        image: data
+    };
+
+    let session = new Soup.Session();
+    let createUrl = 'https://api.imgur.com/3/image';
+    let message = Soup.form_request_new_from_hash('POST', createUrl, params);
+
+    let requestHeaders = message.request_headers;
+    requestHeaders.append('Authorization', 'Client-ID ' + IMGUR_CLIENT_ID);
+    session.queue_message(message,
+        function(session, message) {
+            if (message.status_code != Soup.KnownStatusCode.OK)
+                callback(null);
+
+            let info = {};
+            try {
+                info = JSON.parse(message.response_body.data);
+            } catch(e) {
+                log(e.message);
+            }
+            if (info.success)
+                callback(info.data.link);
+            else
+                callback(null);
+        });
+}


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