~vorlon/ubuntu/saucy/gourmet/trunk

« back to all changes in this revision

Viewing changes to src/lib/exporters/recipe_emailer.py

  • Committer: Bazaar Package Importer
  • Author(s): Rolf Leggewie
  • Date: 2008-07-26 13:29:41 UTC
  • Revision ID: james.westby@ubuntu.com-20080726132941-6ldd73qmacrzz0bn
Tags: upstream-0.14.0
ImportĀ upstreamĀ versionĀ 0.14.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
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
 
8
 
 
9
class StringIOfaker (StringIO.StringIO):
 
10
    def __init__ (self, *args, **kwargs):
 
11
        StringIO.StringIO.__init__(self, *args, **kwargs)
 
12
 
 
13
    def close (self, *args):
 
14
        pass
 
15
 
 
16
    def close_really (self):
 
17
        StringIO.StringIO.close(self)
 
18
 
 
19
class Emailer:
 
20
    def __init__ (self, emailaddress=None, subject=None, body=None, attachments=[]):
 
21
        self.emailaddress=None
 
22
        self.subject=subject
 
23
        self.body=body
 
24
        self.attachments=attachments
 
25
        self.connector_string = "?"
 
26
 
 
27
    def send_email (self):
 
28
        self.url = "mailto:"
 
29
        if self.emailaddress: self.url += self.emailaddress
 
30
        if self.subject:
 
31
            self.url_append('subject',self.subject)
 
32
        if self.body:
 
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)
 
38
 
 
39
    def url_append (self, attr, value):
 
40
        self.url += "%s%s=%s"%(self.connector(),attr,urllib.quote(value.encode('utf-8','replace')))
 
41
                                                                   
 
42
    def connector (self):
 
43
        retval = self.connector_string
 
44
        self.connector_string = "&"
 
45
        return retval
 
46
 
 
47
class RecipeEmailer (Emailer):
 
48
    def __init__ (self, recipes, rd, conv=None, change_units=True):
 
49
        Emailer.__init__(self)
 
50
        self.recipes = recipes
 
51
        self.rd = rd
 
52
        self.conv = conv
 
53
        self.change_units=change_units
 
54
        if len(recipes) > 1:
 
55
            self.subject = _("Recipes")
 
56
        elif recipes:
 
57
            self.subject = recipes[0].title
 
58
 
 
59
    def write_email_text (self):
 
60
        s = StringIOfaker()
 
61
        first = True
 
62
        e=exporter.ExporterMultirec(self.rd,
 
63
                                    self.recipes,
 
64
                                    s,
 
65
                                    conv=self.conv,
 
66
                                    padding="\n\n-----\n")
 
67
        e.run()
 
68
        if not self.body: self.body=""
 
69
        self.body += s.getvalue()
 
70
        s.close_really()
 
71
 
 
72
    def write_email_html (self):
 
73
        for r in self.recipes:
 
74
            fi = os.path.join(gglobals.tmpdir,"%s.htm"%r.title)
 
75
            ofi = open(fi,'w')
 
76
            e=html_exporter.html_exporter(self.rd,
 
77
                                          r,
 
78
                                          ofi,
 
79
                                          conv=self.conv,
 
80
                                          embed_css=True,
 
81
                                          imagedir="")
 
82
            ofi.close()
 
83
            self.attachments.append(fi)
 
84
            for i in e.images:
 
85
                self.attachments.append(i)
 
86
 
 
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)
 
91
            ofi = open(fi,'w')
 
92
            e = pdf_exporter.PdfExporter(self.rd,
 
93
                                         r,
 
94
                                         ofi,
 
95
                                         conv=self.conv,
 
96
                                         change_units=self.change_units,
 
97
                                         pdf_args=prefs)
 
98
            ofi.close()
 
99
            self.attachments.append(fi)
 
100
 
 
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()
 
106
        self.send_email()
 
107
 
 
108
    def send_email_text (self, emailaddress=None):
 
109
        if emailaddress: self.emailaddress=emailaddress
 
110
        self.write_email_text()
 
111
        self.send_email()
 
112
            
 
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))
 
116
        self.prefs = prefs
 
117
        self.option_list = {'':''}
 
118
        self.options = {
 
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),
 
122
            }
 
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]]])
 
128
 
 
129
    def dont_ask_cb (self, widget, *args):
 
130
        if widget.get_active():
 
131
            self.prefs['emailer_dont_ask']=True
 
132
        else:
 
133
            self.prefs['emailer_dont_ask']=False
 
134
 
 
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"),
 
139
                                   value_label="",
 
140
                                   dont_ask_cb=self.dont_ask_cb,
 
141
                                   dont_ask_custom_text=_("Don't ask before sending e-mail."))
 
142
            retlist = d.run()
 
143
            if retlist:
 
144
                for o in retlist:
 
145
                    k = o[0]
 
146
                    v = o[1]
 
147
                    pref = self.options[k][0]
 
148
                    self.email_options[pref]=v
 
149
                    self.prefs[pref]=v
 
150
 
 
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.")
 
162
                            )
 
163
        else:
 
164
            self.send_email()
 
165
            
 
166
 
 
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,{})
 
172
    ed.setup_dialog()
 
173
    ed.email()
 
174
    #ed.run()
 
175
    #e.write_email_text()
 
176
    #e.write_email_pdf()
 
177
    #e.write_email_html()
 
178
    #e.send_email()
 
179