[thotkeeper] r165 committed - Implement the ability to open and launch attachments....

1 view
Skip to first unread message

thotk...@googlecode.com

unread,
Nov 4, 2014, 11:42:22 PM11/4/14
to thotkee...@googlegroups.com
Revision: 165
Author: cmpi...@gmail.com
Date: Wed Nov 5 04:42:10 2014 UTC
Log: Implement the ability to open and launch attachments.

For issue #44 ("Add ability to attach images with captions to entries").

* lib/tk_data.py
(TKEntryAttachment.__init__): Add 'content_type' param.
(TKEntryAttachment.get_content_type): New method.
(TKDataParser.endElement): Notice and use the "content-type=", too.

* lib/tk_main.py
(ThotKeeper.OnInit): Register an event handler for activation of
attachments.
(ThotKeeper._SetEntryFormDate): Populate the attachments list widget.
(ThotKeeper._LaunchDefaultApplication,
ThotKeeper._AttachmentActivated): New methods.
https://code.google.com/p/thotkeeper/source/detail?r=165

Modified:
/trunk/lib/tk_data.py
/trunk/lib/tk_main.py

=======================================
--- /trunk/lib/tk_data.py Sun Jun 16 02:49:31 2013 UTC
+++ /trunk/lib/tk_data.py Wed Nov 5 04:42:10 2014 UTC
@@ -52,10 +52,12 @@


class TKEntryAttachment:
- def __init__(self, description='', filename=None, data=None):
+ def __init__(self, description='', filename=None, data=None,
+ content_type=None):
self.description = description
self.filename = filename
self.data = data
+ self.content_type = content_type

def get_data(self):
return self.data
@@ -66,6 +68,10 @@
def get_filename(self):
return self.filename

+ def get_content_type(self):
+ return self.content_type
+
+
class TKEntry:
def __init__(self, author='', subject='', text='',
year=None, month=None, day=None, id=None,
@@ -542,7 +548,8 @@
elif name == self.TKJ_TAG_ATTACHMENT:
attachment =
TKEntryAttachment(self.cur_attachment['description'],
self.cur_attachment['filename'],
- self.cur_attachment['data'])
+ self.cur_attachment['data'],
+
self.cur_attachment['content-type'])
self.cur_entry['attachments'].append(attachment)
self.cur_attachment = None

=======================================
--- /trunk/lib/tk_main.py Mon Sep 24 19:48:20 2012 UTC
+++ /trunk/lib/tk_main.py Wed Nov 5 04:42:10 2014 UTC
@@ -16,6 +16,7 @@
import time
import string
import tk_data
+import tempfile
import wx
import wx.calendar
import wx.xrc
@@ -700,6 +701,9 @@
wx.EVT_MENU(self, self.help_update_id, self._HelpUpdateMenu)
wx.EVT_MENU(self, self.help_about_id, self._HelpAboutMenu)

+ # Event handlers for the Attachments list.
+ wx.EVT_LIST_ITEM_ACTIVATED(self, self.attachments_id,
self._AttachmentActivated)
+
# Event handlers for the Tree widget.
wx.EVT_TREE_ITEM_ACTIVATED(self, self.datetree_id,
self._TreeActivated)
wx.EVT_RIGHT_DOWN(self.tree, self._TreePopup)
@@ -936,15 +940,19 @@
width=int(frame_width * 0.4))
attachments_list.InsertColumn(1, "Filename",
width=int(frame_width * 0.3))
+ attachments_list.InsertColumn(2, "Content-Type",
+ width=int(frame_width * 0.3))
if attachments:
attachments.reverse()
for attachment in attachments:
list_item = wx.ListItem()
idx = attachments_list.InsertItem(list_item)
- attachments_list.SetStringItem(idx, 0,
-
attachment.get_description())
- attachments_list.SetStringItem(idx, 1,
- attachment.get_filename())
+ att_desc = attachment.get_description()
+ att_name = attachment.get_filename()
+ att_type = attachment.get_content_type()
+ attachments_list.SetStringItem(idx, 0, att_desc)
+ attachments_list.SetStringItem(idx, 1, att_name)
+ attachments_list.SetStringItem(idx, 2, att_type)
self._NotifyEntryLoaded(entry and True or False)

def _NotifyEntryLoaded(self, is_loaded=True):
@@ -1198,7 +1206,22 @@
self._DeleteEntry(entry_year, entry_month, entry_day, entry_id,
skip_verify=True)
new_entries.enumerate_entries(_DeleteEntryCB)
-
+
+ def _LaunchDefaultApplication(self, filename):
+ try:
+ import subprocess
+ if sys.platform.startswith('darwin'):
+ subprocess.call(('open', filename))
+ elif os.name == 'nt':
+ os.startfile(filepath)
+ elif os.name == 'posix':
+ subprocess.call(('xdg-open', filename))
+ except:
+ wx.MessageBox("Failure launching default application.",
+ "Application Launch Failed",
+ wx.OK | wx.ICON_ERROR, self.frame)
+ return
+
### -----------------------------------------------------------------
### Tree Popup Menu Actions
### -----------------------------------------------------------------
@@ -1557,6 +1580,25 @@
return
self._SetEntryFormDate(data.year, data.month, data.day, data.id)

+ def _AttachmentActivated(self, event):
+ item = event.GetItem()
+ list = event.GetEventObject()
+ idx = event.GetIndex()
+ filename = list.GetItem(idx, 1).GetText()
+ entry = self.entries.get_entry(self.entry_form_key.year,
+ self.entry_form_key.month,
+ self.entry_form_key.day,
+ self.entry_form_key.id)
+ for attachment in entry.get_attachments():
+ if attachment.get_filename() == filename:
+ root, ext = os.path.splitext(filename)
+ handle, tmpfile = tempfile.mkstemp(suffix=ext)
+ fp = open(tmpfile, 'w')
+ fp.write(attachment.get_data())
+ fp.close()
+ self._LaunchDefaultApplication(tmpfile)
+ break
+
### -----------------------------------------------------------------
### Debugging Stuff
### -----------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages