2
import gtk.glade, urllib, StringIO, os.path
3
import exporter, html_exporter, pdf_exporter
4
from gourmet import gglobals
5
from gettext import gettext as _
6
import gourmet.gtk_extras.dialog_extras as de
7
from gourmet.gdebug import debug
9
class StringIOfaker (StringIO.StringIO):
10
def __init__ (self, *args, **kwargs):
11
StringIO.StringIO.__init__(self, *args, **kwargs)
13
def close (self, *args):
16
def close_really (self):
17
StringIO.StringIO.close(self)
20
def __init__ (self, emailaddress=None, subject=None, body=None, attachments=[]):
21
self.emailaddress=None
24
self.attachments=attachments
25
self.connector_string = "?"
27
def send_email (self):
29
if self.emailaddress: self.url += self.emailaddress
31
self.url_append('subject',self.subject)
33
self.url_append('body',self.body)
34
for a in self.attachments:
35
self.url_append('attachment',a)
36
debug('launching URL %s'%self.url,0)
37
gglobals.launch_url(self.url)
39
def url_append (self, attr, value):
40
self.url += "%s%s=%s"%(self.connector(),attr,urllib.quote(value.encode('utf-8','replace')))
43
retval = self.connector_string
44
self.connector_string = "&"
47
class RecipeEmailer (Emailer):
48
def __init__ (self, recipes, rd, conv=None, change_units=True):
49
Emailer.__init__(self)
50
self.recipes = recipes
53
self.change_units=change_units
55
self.subject = _("Recipes")
57
self.subject = recipes[0].title
59
def write_email_text (self):
62
e=exporter.ExporterMultirec(self.rd,
66
padding="\n\n-----\n")
68
if not self.body: self.body=""
69
self.body += s.getvalue()
72
def write_email_html (self):
73
for r in self.recipes:
74
fi = os.path.join(gglobals.tmpdir,"%s.htm"%r.title)
76
e=html_exporter.html_exporter(self.rd,
83
self.attachments.append(fi)
85
self.attachments.append(i)
87
def write_email_pdf (self):
88
prefs = pdf_exporter.get_pdf_prefs()
89
for r in self.recipes:
90
fi = os.path.join(gglobals.tmpdir,"%s.pdf"%r.title)
92
e = pdf_exporter.PdfExporter(self.rd,
96
change_units=self.change_units,
99
self.attachments.append(fi)
101
def send_email_html (self, emailaddress=None, include_plain_text=True):
102
if include_plain_text: self.write_email_text()
103
else: self.body = None
104
if emailaddress: self.emailaddress=emailaddress
105
self.write_email_html()
108
def send_email_text (self, emailaddress=None):
109
if emailaddress: self.emailaddress=emailaddress
110
self.write_email_text()
113
class EmailerDialog (RecipeEmailer):
114
def __init__ (self, recipes, rd, prefs, conv=None):
115
RecipeEmailer.__init__(self, recipes, rd, conv=conv, change_units=prefs.get('readableUnits',True))
117
self.option_list = {'':''}
119
_('Include Recipe in Body of E-mail (A good idea no matter what)'):('email_include_body',True),
120
_('E-mail Recipe as HTML Attachment'):('email_include_html',False),
121
_('E-mail Recipe as PDF Attachment'):('email_include_pdf',True),
123
self.option_list = []
124
self.email_options = {}
125
for k,v in self.options.items():
126
self.email_options[v[0]]=apply(self.prefs.get,v)
127
self.option_list.append([k,self.email_options[v[0]]])
129
def dont_ask_cb (self, widget, *args):
130
if widget.get_active():
131
self.prefs['emailer_dont_ask']=True
133
self.prefs['emailer_dont_ask']=False
135
def setup_dialog (self, force = False):
136
if force or not self.prefs.get('emailer_dont_ask',False):
137
d=de.PreferencesDialog(options=self.option_list,
138
option_label=_("Email Options"),
140
dont_ask_cb=self.dont_ask_cb,
141
dont_ask_custom_text=_("Don't ask before sending e-mail."))
147
pref = self.options[k][0]
148
self.email_options[pref]=v
151
def email (self, address=None):
152
if address: self.emailaddress=address
153
if self.email_options['email_include_body']:
154
self.write_email_text()
155
if self.email_options['email_include_html']:
156
self.write_email_html()
157
if self.email_options['email_include_pdf']:
158
self.write_email_pdf()
159
if not self.email_options['email_include_body'] and not self.email_options['email_include_body']:
160
de.show_message(_("E-mail not sent"),
161
sublabel=_("You have not chosen to include the recipe in the body of the message or as an attachment.")
167
if __name__ == '__main__':
168
import gourmet.recipeManager
169
rd = gourmet.recipeManager.default_rec_manager()
170
rec = rd.fetch_one(rd.recipe_table)
171
ed = EmailerDialog([rec],rd,{})
175
#e.write_email_text()
177
#e.write_email_html()