home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1338 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.3 KB  |  150 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import cStringIO
  9. import sys
  10. from binascii import hexlify, unhexlify
  11. from PyQt4.Qt import QWidget, SIGNAL, QDialog, Qt
  12. from calibre.gui2.wizard.send_email_ui import Ui_Form
  13. from calibre.utils.smtp import config as smtp_prefs
  14. from calibre.gui2.dialogs.test_email_ui import Ui_Dialog as TE_Dialog
  15. from calibre.gui2 import error_dialog, info_dialog
  16.  
  17. class TestEmail(QDialog, TE_Dialog):
  18.     
  19.     def __init__(self, pa, parent):
  20.         QDialog.__init__(self, parent)
  21.         TE_Dialog.__init__(self)
  22.         self.setupUi(self)
  23.         opts = smtp_prefs().parse()
  24.         self.test_func = parent.test_email_settings
  25.         self.connect(self.test_button, SIGNAL('clicked(bool)'), self.test)
  26.         self.from_.setText(unicode(self.from_.text()) % opts.from_)
  27.         if pa:
  28.             self.to.setText(pa)
  29.         
  30.         if opts.relay_host:
  31.             self.label.setText(_('Using: %s:%s@%s:%s and %s encryption') % (opts.relay_username, unhexlify(opts.relay_password), opts.relay_host, opts.relay_port, opts.encryption))
  32.         
  33.  
  34.     
  35.     def test(self):
  36.         self.log.setPlainText(_('Sending...'))
  37.         self.test_button.setEnabled(False)
  38.         
  39.         try:
  40.             tb = self.test_func(unicode(self.to.text()))
  41.             if not tb:
  42.                 tb = _('Mail successfully sent')
  43.             
  44.             self.log.setPlainText(tb)
  45.         finally:
  46.             self.test_button.setEnabled(True)
  47.  
  48.  
  49.  
  50.  
  51. class SendEmail(QWidget, Ui_Form):
  52.     
  53.     def __init__(self, parent = None):
  54.         QWidget.__init__(self, parent)
  55.         self.setupUi(self)
  56.  
  57.     
  58.     def initialize(self, preferred_to_address):
  59.         self.preferred_to_address = preferred_to_address
  60.         opts = smtp_prefs().parse()
  61.         self.smtp_opts = opts
  62.         if opts.from_:
  63.             self.email_from.setText(opts.from_)
  64.         
  65.         if opts.relay_host:
  66.             self.relay_host.setText(opts.relay_host)
  67.         
  68.         self.relay_port.setValue(opts.relay_port)
  69.         if opts.relay_username:
  70.             self.relay_username.setText(opts.relay_username)
  71.         
  72.         if opts.relay_password:
  73.             self.relay_password.setText(unhexlify(opts.relay_password))
  74.         
  75.         None if opts.encryption == 'TLS' else self.relay_ssl.setChecked(True)
  76.         self.connect(self.relay_use_gmail, SIGNAL('clicked(bool)'), self.create_gmail_relay)
  77.         self.connect(self.relay_show_password, (SIGNAL('stateChanged(int)'),), (lambda state: None(self.relay_password.setEchoMode if state == 0 else self.relay_password.Normal)))
  78.         self.connect(self.test_email_button, SIGNAL('clicked(bool)'), self.test_email)
  79.  
  80.     
  81.     def test_email(self, *args):
  82.         pa = self.preferred_to_address()
  83.         to_set = pa is not None
  84.         if self.set_email_settings(to_set):
  85.             TestEmail(pa, self).exec_()
  86.         
  87.  
  88.     
  89.     def test_email_settings(self, to):
  90.         opts = smtp_prefs().parse()
  91.         sendmail = sendmail
  92.         create_mail = create_mail
  93.         import calibre.utils.smtp
  94.         buf = cStringIO.StringIO()
  95.         oout = sys.stdout
  96.         oerr = sys.stderr
  97.         sys.stdout = sys.stderr = buf
  98.         tb = None
  99.         
  100.         try:
  101.             msg = create_mail(opts.from_, to, 'Test mail from calibre', 'Test mail from calibre')
  102.             sendmail(msg, from_ = opts.from_, to = [
  103.                 to], verbose = 3, timeout = 30, relay = opts.relay_host, username = opts.relay_username, password = unhexlify(opts.relay_password), encryption = opts.encryption, port = opts.relay_port)
  104.         except:
  105.             import traceback
  106.             tb = traceback.format_exc()
  107.             tb += '\n\nLog:\n' + buf.getvalue()
  108.         finally:
  109.             sys.stdout = oout
  110.             sys.stderr = oerr
  111.  
  112.         return tb
  113.  
  114.     
  115.     def create_gmail_relay(self, *args):
  116.         self.relay_username.setText('@gmail.com')
  117.         self.relay_password.setText('')
  118.         self.relay_host.setText('smtp.gmail.com')
  119.         self.relay_port.setValue(587)
  120.         self.relay_tls.setChecked(True)
  121.         info_dialog(self, _('Finish gmail setup'), _('Dont forget to enter your gmail username and password. You can sign up for a free gmail account at http://gmail.com')).exec_()
  122.         self.relay_username.setFocus(Qt.OtherFocusReason)
  123.         self.relay_username.setCursorPosition(0)
  124.  
  125.     
  126.     def set_email_settings(self, to_set):
  127.         from_ = unicode(self.email_from.text()).strip()
  128.         if to_set and not from_:
  129.             error_dialog(self, _('Bad configuration'), _('You must set the From email address')).exec_()
  130.             return False
  131.         username = unicode(self.relay_username.text()).strip()
  132.         password = unicode(self.relay_password.text()).strip()
  133.         host = unicode(self.relay_host.text()).strip()
  134.         if host:
  135.             if username:
  136.                 pass
  137.             if not password:
  138.                 error_dialog(self, _('Bad configuration'), _('You must set the username and password for the mail server.')).exec_()
  139.                 return False
  140.             conf = smtp_prefs()
  141.             conf.set('from_', from_)
  142.         not password(conf.set, 'relay_host' if host else None)
  143.         conf.set('relay_port', self.relay_port.value())
  144.         not from_(conf.set, 'relay_username' if username else None)
  145.         conf.set('relay_password', hexlify(password))
  146.         None(conf.set, 'encryption' if self.relay_tls.isChecked() else 'SSL')
  147.         return True
  148.  
  149.  
  150.