Is there a way to test the pythonfile within office? At the moment each time i kill soffice, restart writer and click Extras>Serienbrief>versenden. MessageBox.showMessage() actually doesnt react. May somebody have a look at that function? Do the nonfree software prefering people see the python output?
Am Freitag, den 08.04.2011, 14:22 +0100 schrieb Michael Meeks: > Hi Timo, > > On Fri, 2011-04-08 at 14:00 +0200, Timo wrote: > > I am applying for the GSoC-project dealing with conversion of > > helpfiles. > > Cool ! :-) good to see your submission. > > > Before I start that project I like to help with the task to make > > mailmerge debuggable.* What action exactly makes office display a > > message ? > > Riight :-) that is tricky; I think this needs to be a GUI thing, since > most of our errors seem to happen to end-users who need to be able to > debug the problem. > > > exit() did, raise Exception did, but print did not. > > Right; and of course we have lots of windows users, who tend to get > most of the problems here, and who have no console. > > > I added a Tk messagebox in the attatched version. This requires python-tk. > > Now it prints only the human-readable errormessage-text. What does a > > prefered output look like, that can be understood easily by a user? > > Well :-) I suggest we do two things. > > First - use the UNO toolkit/ APIs to create and throw up a dialog, and > encapsulate this nicely in a python function somewhere; > > There is some sample code to read before you write your version for > this here: > > http://www.oooforum.org/forum/viewtopic.phtml?t=9602 > > It is hard to use python-tk, since we don't bundle that with > LibreOffice, and our python is typically built-in. > > So - creating a dialog popup function that will show a message and an > ok button using the UNO toolkit API would be the first step. > > From then, I guess we need to work out which pieces are most likely to > fail :-) I suppose that testing is the key piece here, you know - > dis-connecting yourself while talking to the remote server etc. etc. but > I guess the key thing is to get that dialog goodness in there, so we can > expand its use over time. > > Does that make sense ? :-) > > Thanks, > > Michael. >
# Caolan McNamara caol...@redhat.com # a simple email mailmerge component # manual installation for hackers, not necessary for users # cp mailmerge.py /usr/lib/openoffice.org2.0/program # cd /usr/lib/openoffice.org2.0/program # ./unopkg add --shared mailmerge.py # edit ~/.openoffice.org2/user/registry/data/org/openoffice/Office/Writer.xcu # and change EMailSupported to as follows... # <prop oor:name="EMailSupported" oor:type="xs:boolean"> # <value>true</value> # </prop> import unohelper import uno import re #to implement com::sun::star::mail::XMailServiceProvider #and #to implement com.sun.star.mail.XMailMessage from com.sun.star.mail import XMailServiceProvider from com.sun.star.mail import XMailService from com.sun.star.mail import XSmtpService from com.sun.star.mail import XConnectionListener from com.sun.star.mail import XAuthenticator from com.sun.star.mail import XMailMessage from com.sun.star.mail.MailServiceType import SMTP from com.sun.star.mail.MailServiceType import POP3 from com.sun.star.mail.MailServiceType import IMAP from com.sun.star.uno import XCurrentContext from com.sun.star.lang import IllegalArgumentException from com.sun.star.lang import EventObject from com.sun.star.mail import SendMailMessageFailedException from email.MIMEBase import MIMEBase from email.Message import Message from email import Encoders from email.Header import Header from email.MIMEMultipart import MIMEMultipart from email.Utils import formatdate from email.Utils import parseaddr import sys, smtplib, imaplib, poplib import Tkinter, tkMessageBox dbg = False class MessageBox: '''Message box for OpenOffice.org, like the one in the Basic macro language. To specify a MsgBox type, use the named constants of this class or the equivalent numbers described in the StarBasic online help. Specify a parent window on initialization.''' # Named constants for ease of use: OK = 0 OK_CANCEL = 1 ABORT_RETRY_IGNORE = 2 YES_NO_CANCEL = 3 YES_NO = 4 RETRY_CANCEL = 5 ERROR = 16 QUERY = 32 WARN = 48 INFO = 64 DEFAULT_FIRST = 128 DEFAULT_SECOND = 256 DEFAULT_THIRD = 512 RESULT_OK = 1 RESULT_CANCEL = 2 RESULT_ABORT = 3 RESULT_RETRY = 4 RESULT_IGNORE = 5 RESULT_YES = 6 RESULT_NO = 7 # Mapping above StarBasic MsgBox constants to awt.MessageBoxButtons and icons: """dInput = { OK_CANCEL : BUTTONS_OK_CANCEL, # the following constant should be named BUTTONS_ABORT_RETRY_IGNORE: ABORT_RETRY_IGNORE : BUTTONS_ABORT_IGNORE_RETRY, YES_NO_CANCEL : BUTTONS_YES_NO_CANCEL, YES_NO : BUTTONS_YES_NO, RETRY_CANCEL : BUTTONS_RETRY_CANCEL, ERROR : 'errorbox', QUERY : 'querybox', WARN : 'warningbox', INFO : 'infobox', # info always shows one OK button alone! 129 : BUTTONS_OK_CANCEL + DEFAULT_BUTTON_OK, 130 : BUTTONS_ABORT_IGNORE_RETRY + DEFAULT_BUTTON_CANCEL, 131 : BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_YES, 132 : BUTTONS_YES_NO + DEFAULT_BUTTON_YES, 133 : BUTTONS_RETRY_CANCEL + DEFAULT_BUTTON_RETRY, 257 : BUTTONS_OK_CANCEL + DEFAULT_BUTTON_CANCEL, 258 : BUTTONS_ABORT_IGNORE_RETRY + DEFAULT_BUTTON_RETRY, # retry is 2nd! 259 : BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_NO, 260 : BUTTONS_YES_NO + DEFAULT_BUTTON_NO, 261 : BUTTONS_RETRY_CANCEL + DEFAULT_BUTTON_CANCEL, # DEFAULT_BUTTON_IGNORE doesn't work at all. Use retry in this case: # 514 : BUTTONS_ABORT_IGNORE_RETRY + DEFAULT_BUTTON_IGNORE, 514 : BUTTONS_ABORT_IGNORE_RETRY + DEFAULT_BUTTON_RETRY, 515 : BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_CANCEL, }""" dOutput = { 1 : RESULT_OK, 2 : RESULT_YES, 3 : RESULT_NO, 4 : RESULT_RETRY, 5 : RESULT_IGNORE, 0 : RESULT_CANCEL, # there is no constant for ABORT } def __init__(self, XParentWindow): '''Set needed objects on init''' try: self.Parent = XParentWindow self.Toolkit = XParentWindow.getToolkit() except: raise AttributeError, 'Did not get a valid parent window' def msgbox(self, message='', flag=0, title=''): '''Wrapper for com.sun.star.awt.XMessageBoxFactory.''' rect = uno.createUnoStruct('com.sun.star.awt.Rectangle') stype, buttons = self.getFlags(flag) box = self.Toolkit.createMessageBox(self.Parent, rect, stype, buttons, title, message) e = box.execute() # the result of execute() does not distinguish between Cancel and Abort: if (e == 0) and (buttons & BUTTONS_ABORT_IGNORE_RETRY == BUTTONS_ABORT_IGNORE_RETRY): r = self.RESULT_ABORT else: try: r = self.dOutput[e] except KeyError: raise KeyError, 'Lookup of message box result '+ str(e) +' failed' return r def getFlags(self, flag): s = self.dInput.get(flag & 112, 'messbox') try: b = self.dInput[flag & 903] except KeyError: b = self.dInput.get(flag & 7, BUTTONS_OK) # print 'B/D-Flag:', flag & 7, flag & 896, 'Return:', hex(b), s return s, b def showMessage(text,title): oLocalContext = uno.getComponentContext() oLocalResolver = oLocalContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", oLocalContext ) oContext = oLocalResolver.resolve( "uno:socket,host=" + "localhost" + ",port=" + "8100" + ";urp;StarOffice.ComponentContext" ) oServiceManager = oContext.ServiceManager oObj = oServiceManager.createInstance( "com.sun.star.frame.Desktop" ) StarDesktop = oObj oToolkit = StarDesktop.getActiveFrame().getContainerWindow() m = MessageBox(oToolkit) m.msgbox(message=text,title=title) class PyMailSMTPService(unohelper.Base, XSmtpService): def __init__( self, ctx ): self.ctx = ctx self.listeners = [] self.supportedtypes = ('Insecure', 'Ssl') self.server = None self.connectioncontext = None self.notify = EventObject() if dbg: print >> sys.stderr, "PyMailSMPTService init" def addConnectionListener(self, xListener): if dbg: print >> sys.stderr, "PyMailSMPTService addConnectionListener" self.listeners.append(xListener) def removeConnectionListener(self, xListener): if dbg: print >> sys.stderr, "PyMailSMPTService removeConnectionListener" self.listeners.remove(xListener) def getSupportedConnectionTypes(self): if dbg: print >> sys.stderr, "PyMailSMPTService getSupportedConnectionTypes" return self.supportedtypes def connect(self, xConnectionContext, xAuthenticator): self.connectioncontext = xConnectionContext if dbg: print >> sys.stderr, "PyMailSMPTService connect" server = xConnectionContext.getValueByName("ServerName") if dbg: print >> sys.stderr, server port = xConnectionContext.getValueByName("Port") if dbg: print >> sys.stderr, port self.server = smtplib.SMTP(server, port) if dbg: self.server.set_debuglevel(1) connectiontype = xConnectionContext.getValueByName("ConnectionType") if dbg: print >> sys.stderr, connectiontype if connectiontype == 'Ssl': self.server.ehlo() self.server.starttls() self.server.ehlo() user = xAuthenticator.getUserName().encode('ascii') password = xAuthenticator.getPassword().encode('ascii') if user != '': if dbg: print >> sys.stderr, 'Logging in, username of', user self.server.login(user, password) for listener in self.listeners: listener.connected(self.notify) def disconnect(self): if dbg: print >> sys.stderr, "PyMailSMPTService disconnect" if self.server: self.server.quit() self.server = None for listener in self.listeners: listener.disconnected(self.notify) def isConnected(self): if dbg: print >> sys.stderr, "PyMailSMPTService isConnected" return self.server != None def getCurrentConnectionContext(self): if dbg: print >> sys.stderr, "PyMailSMPTService getCurrentConnectionContext" return self.connectioncontext def sendMailMessage(self, xMailMessage): COMMASPACE = ', ' if dbg: print >> sys.stderr, "PyMailSMPTService sendMailMessage" recipients = xMailMessage.getRecipients() sendermail = xMailMessage.SenderAddress sendername = xMailMessage.SenderName subject = xMailMessage.Subject ccrecipients = xMailMessage.getCcRecipients() bccrecipients = xMailMessage.getBccRecipients() if dbg: print >> sys.stderr, "PyMailSMPTService subject", subject print >> sys.stderr, "PyMailSMPTService from", sendername.encode('utf-8') print >> sys.stderr, "PyMailSMTPService from", sendermail print >> sys.stderr, "PyMailSMPTService send to", recipients attachments = xMailMessage.getAttachments() textmsg = Message() content = xMailMessage.Body flavors = content.getTransferDataFlavors() if dbg: print >> sys.stderr, "PyMailSMPTService flavors len", len(flavors) #Use first flavor that's sane for an email body for flavor in flavors: if flavor.MimeType.find('text/html') != -1 or flavor.MimeType.find('text/plain') != -1: if dbg: print >> sys.stderr, "PyMailSMPTService mimetype is", flavor.MimeType textbody = content.getTransferData(flavor) try: textbody = textbody.value except: pass textbody = textbody.encode('utf-8') if len(textbody): mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType) if mimeEncoding.find('charset=UTF-8') == -1: mimeEncoding = mimeEncoding + "; charset=UTF-8" textmsg['Content-Type'] = mimeEncoding textmsg['MIME-Version'] = '1.0' textmsg.set_payload(textbody) break if (len(attachments)): msg = MIMEMultipart() msg.epilogue = '' msg.attach(textmsg) else: msg = textmsg hdr = Header(sendername, 'utf-8') hdr.append('<'+sendermail+'>','us-ascii') msg['Subject'] = subject msg['From'] = hdr msg['To'] = COMMASPACE.join(recipients) if len(ccrecipients): msg['Cc'] = COMMASPACE.join(ccrecipients) if xMailMessage.ReplyToAddress != '': msg['Reply-To'] = xMailMessage.ReplyToAddress mailerstring = "OpenOffice.org 2.0 via Caolan's mailmerge component" try: ctx = uno.getComponentContext() aConfigProvider = ctx.ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider") prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue') prop.Name = "nodepath" prop.Value = "/org.openoffice.Setup/Product" aSettings = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", (prop,)) mailerstring = aSettings.getByName("ooName") + " " + \ aSettings.getByName("ooSetupVersion") + " via Caolan's mailmerge component" except: pass msg['X-Mailer'] = mailerstring msg['Date'] = formatdate(localtime=True) for attachment in attachments: content = attachment.Data flavors = content.getTransferDataFlavors() flavor = flavors[0] ctype = flavor.MimeType maintype, subtype = ctype.split('/', 1) msgattachment = MIMEBase(maintype, subtype) data = content.getTransferData(flavor) msgattachment.set_payload(data) Encoders.encode_base64(msgattachment) msgattachment.add_header('Content-Disposition', 'attachment', \ filename=attachment.ReadableName) msg.attach(msgattachment) uniquer = {} for key in recipients: uniquer[key] = True if len(ccrecipients): for key in ccrecipients: uniquer[key] = True if len(bccrecipients): for key in bccrecipients: uniquer[key] = True truerecipients = uniquer.keys() if dbg: print >> sys.stderr, "PyMailSMPTService recipients are", truerecipients try: self.server.sendmail(sendermail, truerecipients, msg.as_string()) except smtplib.SMTPRecipientsRefused as inst: msg = "" for key,val in inst.recipients.iteritems(): msg+=val[1]+"\n" self.showError(msg) except Exception as inst: self.showError(str(inst)) #except smtplib.SMTPException: def showTK(self,text): sys.argv=["a"] # Bug-workaround print >> sys.stderr, text print text root = Tkinter.Tk() root.withdraw() root.title("Error") tkMessageBox.showwarning("Error",text) #exit() # Creates an exception shown by office def showError(self,text): self.showTK(text) MessageBox.showMessage(text,"Error") exit(1) class PyMailIMAPService(unohelper.Base, XMailService): def __init__( self, ctx ): self.ctx = ctx self.listeners = [] self.supportedtypes = ('Insecure', 'Ssl') self.server = None self.connectioncontext = None if dbg: print >> sys.stderr, "PyMailIMAPService init" def addConnectionListener(self, xListener): if dbg: print >> sys.stderr, "PyMailIMAPService addConnectionListener" self.listeners.append(xListener) def removeConnectionListener(self, xListener): if dbg: print >> sys.stderr, "PyMailIMAPService removeConnectionListener" self.listeners.remove(xListener) def getSupportedConnectionTypes(self): if dbg: print >> sys.stderr, "PyMailIMAPService getSupportedConnectionTypes" return self.supportedtypes def connect(self, xConnectionContext, xAuthenticator): if dbg: print >> sys.stderr, "PyMailIMAPService connect" self.connectioncontext = xConnectionContext server = xConnectionContext.getValueByName("ServerName") if dbg: print >> sys.stderr, server port = xConnectionContext.getValueByName("Port") if dbg: print >> sys.stderr, port connectiontype = xConnectionContext.getValueByName("ConnectionType") if dbg: print >> sys.stderr, connectiontype print >> sys.stderr, "BEFORE" if connectiontype == 'Ssl': self.server = imaplib.IMAP4_SSL(server, port) else: self.server = imaplib.IMAP4(server, port) print >> sys.stderr, "AFTER" user = xAuthenticator.getUserName().encode('ascii') password = xAuthenticator.getPassword().encode('ascii') if user != '': if dbg: print >> sys.stderr, 'Logging in, username of', user self.server.login(user, password) for listener in self.listeners: listener.connected(self.notify) def disconnect(self): if dbg: print >> sys.stderr, "PyMailIMAPService disconnect" if self.server: self.server.logout() self.server = None for listener in self.listeners: listener.disconnected(self.notify) def isConnected(self): if dbg: print >> sys.stderr, "PyMailIMAPService isConnected" return self.server != None def getCurrentConnectionContext(self): if dbg: print >> sys.stderr, "PyMailIMAPService getCurrentConnectionContext" return self.connectioncontext class PyMailPOP3Service(unohelper.Base, XMailService): def __init__( self, ctx ): self.ctx = ctx self.listeners = [] self.supportedtypes = ('Insecure', 'Ssl') self.server = None self.connectioncontext = None if dbg: print >> sys.stderr, "PyMailPOP3Service init" def addConnectionListener(self, xListener): if dbg: print >> sys.stderr, "PyMailPOP3Service addConnectionListener" self.listeners.append(xListener) def removeConnectionListener(self, xListener): if dbg: print >> sys.stderr, "PyMailPOP3Service removeConnectionListener" self.listeners.remove(xListener) def getSupportedConnectionTypes(self): if dbg: print >> sys.stderr, "PyMailPOP3Service getSupportedConnectionTypes" return self.supportedtypes def connect(self, xConnectionContext, xAuthenticator): if dbg: print >> sys.stderr, "PyMailPOP3Service connect" self.connectioncontext = xConnectionContext server = xConnectionContext.getValueByName("ServerName") if dbg: print >> sys.stderr, server port = xConnectionContext.getValueByName("Port") if dbg: print >> sys.stderr, port connectiontype = xConnectionContext.getValueByName("ConnectionType") if dbg: print >> sys.stderr, connectiontype print >> sys.stderr, "BEFORE" if connectiontype == 'Ssl': self.server = poplib.POP3_SSL(server, port) else: self.server = poplib.POP3(server, port) print >> sys.stderr, "AFTER" user = xAuthenticator.getUserName().encode('ascii') password = xAuthenticator.getPassword().encode('ascii') if dbg: print >> sys.stderr, 'Logging in, username of', user self.server.user(user) self.server.pass_(user, password) for listener in self.listeners: listener.connected(self.notify) def disconnect(self): if dbg: print >> sys.stderr, "PyMailPOP3Service disconnect" if self.server: self.server.quit() self.server = None for listener in self.listeners: listener.disconnected(self.notify) def isConnected(self): if dbg: print >> sys.stderr, "PyMailPOP3Service isConnected" return self.server != None def getCurrentConnectionContext(self): if dbg: print >> sys.stderr, "PyMailPOP3Service getCurrentConnectionContext" return self.connectioncontext class PyMailServiceProvider(unohelper.Base, XMailServiceProvider): def __init__( self, ctx ): if dbg: print >> sys.stderr, "PyMailServiceProvider init" self.ctx = ctx def create(self, aType): if dbg: print >> sys.stderr, "PyMailServiceProvider create with", aType if aType == SMTP: return PyMailSMTPService(self.ctx); elif aType == POP3: return PyMailPOP3Service(self.ctx); elif aType == IMAP: return PyMailIMAPService(self.ctx); else: print >> sys.stderr, "PyMailServiceProvider, unknown TYPE", aType class PyMailMessage(unohelper.Base, XMailMessage): def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ): if dbg: print >> sys.stderr, "PyMailMessage init" self.ctx = ctx self.recipients = sTo, self.ccrecipients = () self.bccrecipients = () self.aMailAttachments = () if aMailAttachment != None: self.aMailAttachments = aMailAttachment, self.SenderName, self.SenderAddress = parseaddr(sFrom) self.ReplyToAddress = sFrom self.Subject = Subject self.Body = Body if dbg: print >> sys.stderr, "post PyMailMessage init" def addRecipient( self, recipient ): if dbg: print >> sys.stderr, "PyMailMessage.addRecipient", recipient self.recipients = self.recipients, recipient def addCcRecipient( self, ccrecipient ): if dbg: print >> sys.stderr, "PyMailMessage.addCcRecipient", ccrecipient self.ccrecipients = self.ccrecipients, ccrecipient def addBccRecipient( self, bccrecipient ): if dbg: print >> sys.stderr, "PyMailMessage.addBccRecipient", bccrecipient self.bccrecipients = self.bccrecipients, bccrecipient def getRecipients( self ): if dbg: print >> sys.stderr, "PyMailMessage.getRecipients", self.recipients return self.recipients def getCcRecipients( self ): if dbg: print >> sys.stderr, "PyMailMessage.getCcRecipients", self.ccrecipients return self.ccrecipients def getBccRecipients( self ): if dbg: print >> sys.stderr, "PyMailMessage.getBccRecipients", self.bccrecipients return self.bccrecipients def addAttachment( self, aMailAttachment ): if dbg: print >> sys.stderr, "PyMailMessage.addAttachment" self.aMailAttachments = self.aMailAttachments, aMailAttachment def getAttachments( self ): if dbg: print >> sys.stderr, "PyMailMessage.getAttachments" return self.aMailAttachments # pythonloader looks for a static g_ImplementationHelper variable g_ImplementationHelper = unohelper.ImplementationHelper() g_ImplementationHelper.addImplementation( \ PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider", ("com.sun.star.mail.MailServiceProvider",),) g_ImplementationHelper.addImplementation( \ PyMailMessage, "org.openoffice.pyuno.MailMessage", ("com.sun.star.mail.MailMessage",),)
_______________________________________________ LibreOffice mailing list LibreOffice@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice