[gnome-keysign: 3/16] Add Ability to DnD from Evolution



commit 05f9e1e680ebe32c410a5fd938951b0b68c98090
Author: RyuzakiKK <aasonykk gmail com>
Date:   Wed Nov 14 18:24:41 2018 +0100

    Add Ability to DnD from Evolution
    
    Now it's possible to DnD a single attachment or even an entire email.

 keysign/gpgmeh.py |  5 ++---
 keysign/send.py   | 16 +++++++++++++++-
 keysign/util.py   | 12 ++++++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)
---
diff --git a/keysign/gpgmeh.py b/keysign/gpgmeh.py
index a15a0e4..dbdd9ca 100755
--- a/keysign/gpgmeh.py
+++ b/keysign/gpgmeh.py
@@ -473,11 +473,10 @@ def sign_keydata_and_encrypt(keydata, error_cb=None, homedir=None):
                 yield (UID.from_gpgme(uid), ciphertext)
 
 
-def import_signature(filename, homedir=None):
+def import_signature(signature, homedir=None):
     # ctx = gpg.Context()
     ctx = DirectoryContext(homedir)
-    with open(filename, "rb") as fh:
-        decrypted = ctx.decrypt(fh)
+    decrypted = ctx.decrypt(signature)
     ctx.op_import(decrypted[0])
     result = ctx.op_import_result()
     if len(result.imports) < 0:
diff --git a/keysign/send.py b/keysign/send.py
index 036d7d4..b1bdb07 100644
--- a/keysign/send.py
+++ b/keysign/send.py
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 
 import logging
+import mailbox
 import os
 import signal
 
@@ -32,6 +33,7 @@ if  __name__ == "__main__" and __package__ is None:
 from .keylistwidget import KeyListWidget
 from .KeyPresent import KeyPresentWidget
 from .offer import Offer
+from .util import get_attachments
 from . import gpgmeh
 # We import i18n to have the locale set up for Glade
 from .i18n import _
@@ -103,13 +105,25 @@ class SendApp:
         self.label.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)
         self.label.drag_dest_set_target_list(None)
         self.label.drag_dest_add_text_targets()
+        self.label.drag_dest_add_uri_targets()
 
     def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
         filename = data.get_text()
+        # If we don't have a filename it means that the user maybe dropped
+        # an attachment or an entire email.
+        if not filename:
+            filename = data.get_data().decode("utf-8")
         filename = filename[7:].strip('\r\n\x00')  # remove file://, \r\n and NULL
         log.info("Received file: %s" % filename)
+        signatures = get_attachments(filename)
+        if not signatures:
+            with open(filename, "rb") as si:
+                signatures.append(si.read())
+            si.close()
+
         try:
-            gpgmh.import_signature(filename)
+            for signature in signatures:
+                gpgmeh.import_signature(signature)
         except errors.GPGMEError as e:
             log.error(e)
 
diff --git a/keysign/util.py b/keysign/util.py
index 076d45b..4807664 100755
--- a/keysign/util.py
+++ b/keysign/util.py
@@ -21,6 +21,7 @@ import hashlib
 import hmac
 import json
 import logging
+import mailbox
 import os
 import shutil
 from subprocess import call
@@ -455,3 +456,14 @@ def _get_available_bt():
             bt = '/'.join((object_path, child.attrib['name']))
             available_bt.append(bt)
     return available_bt
+
+
+def get_attachments(filename):
+    mbox = mailbox.mbox(filename)
+    attachments = []
+    for message in mbox:
+        # Check if there are attachments
+        if message.is_multipart():
+            for attach in message.get_payload()[1:]:
+                attachments.append(attach.get_payload(decode=True))
+    return attachments


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