# viewattach.py
#
# Allow displaying of attachments
import utils
import gtk
import tempfile
import os
import ptk.progresswin
import ptk.misc_widgets
import re
from pyneheaders import *
def view_attachment(msg, index, user, parent_win):
"""
Display the attachment.
"""
attachment_info = msg.get_attachment_info(index)
content_type = attachment_info[0]
orig_name = attachment_info[1]
#attach_size = attachment_into[2]
#content_transfer_encoding = attachment_info[3]
# Get a temporary filename
tempfilename = tempfile.mktemp(orig_name.replace(" ",""))
# Decode attachment to this file
msg.save_attachment(index, tempfilename)
if content_type == "text/html":
user.open_url ("file://"+tempfilename)
return
# See if there are any special mime type handlers
for mimehandler in user.mime_types:
if mimehandler[0] == "":
continue
if re.match(mimehandler[0], content_type) == None:
continue
# Internal pyne handler requested
if mimehandler[2] == 1:
break
else:
# Register temp file
user.tempfiles.append(tempfilename)
args = mimehandler[1].split()
args.append(tempfilename)
pid = os.fork ()
if pid == 0:
try:
os.execvp(args[0], args)
except OSError:
print "Pyne: Could not find external program '%s'" % args[0]
os._exit(0)
else:
user.register_child (pid)
return
if content_type[:4] == "text":
# Non-image formats...
# Read in temporary file
f = open(tempfilename, "r")
s = f.read()
f.close()
# Show file in a dodgy textual manner...
pager = ptk.progresswin.ProgressWin(_("Viewing attachment ")+orig_name, user)
pager.msg_print(utils._u(s))
# Remove temporary file
os.remove(tempfilename)
return
def _key_press(w, event):
if event.string == 'q':
w.destroy()
# Assume others are all images for the moment
im = gtk.Image()
im.set_from_file(tempfilename)
win = gtk.Window()
win.set_title(attachment_info[1])
win.add(im)
win.connect("key_press_event", _key_press)
im.show()
win.show()
# Remove temporary file
os.remove(tempfilename)
return
syntax highlighted by Code2HTML, v. 0.9.1