home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1397 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.7 KB  |  161 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, pyqtSignal, 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.test_button.clicked.connect(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, *args):
  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.     changed_signal = pyqtSignal()
  53.     
  54.     def __init__(self, parent = None):
  55.         QWidget.__init__(self, parent)
  56.         self.setupUi(self)
  57.  
  58.     
  59.     def initialize(self, preferred_to_address):
  60.         self.preferred_to_address = preferred_to_address
  61.         opts = smtp_prefs().parse()
  62.         self.smtp_opts = opts
  63.         if opts.from_:
  64.             self.email_from.setText(opts.from_)
  65.         
  66.         self.email_from.textChanged.connect(self.changed)
  67.         if opts.relay_host:
  68.             self.relay_host.setText(opts.relay_host)
  69.         
  70.         self.relay_host.textChanged.connect(self.changed)
  71.         self.relay_port.setValue(opts.relay_port)
  72.         self.relay_port.valueChanged.connect(self.changed)
  73.         if opts.relay_username:
  74.             self.relay_username.setText(opts.relay_username)
  75.         
  76.         self.relay_username.textChanged.connect(self.changed)
  77.         if opts.relay_password:
  78.             self.relay_password.setText(unhexlify(opts.relay_password))
  79.         
  80.         self.relay_password.textChanged.connect(self.changed)
  81.         None if opts.encryption == 'TLS' else self.relay_ssl.setChecked(True)
  82.         self.relay_tls.toggled.connect(self.changed)
  83.         self.relay_use_gmail.clicked.connect(self.create_gmail_relay)
  84.         (self.relay_show_password.stateChanged.connect,)((lambda state: None(self.relay_password.setEchoMode if state == 0 else self.relay_password.Normal)))
  85.         self.test_email_button.clicked.connect(self.test_email)
  86.  
  87.     
  88.     def changed(self, *args):
  89.         self.changed_signal.emit()
  90.  
  91.     
  92.     def test_email(self, *args):
  93.         pa = self.preferred_to_address()
  94.         to_set = pa is not None
  95.         if self.set_email_settings(to_set):
  96.             TestEmail(pa, self).exec_()
  97.         
  98.  
  99.     
  100.     def test_email_settings(self, to):
  101.         opts = smtp_prefs().parse()
  102.         sendmail = sendmail
  103.         create_mail = create_mail
  104.         import calibre.utils.smtp
  105.         buf = cStringIO.StringIO()
  106.         oout = sys.stdout
  107.         oerr = sys.stderr
  108.         sys.stdout = sys.stderr = buf
  109.         tb = None
  110.         
  111.         try:
  112.             msg = create_mail(opts.from_, to, 'Test mail from calibre', 'Test mail from calibre')
  113.             sendmail(msg, from_ = opts.from_, to = [
  114.                 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)
  115.         except:
  116.             import traceback
  117.             tb = traceback.format_exc()
  118.             tb += '\n\nLog:\n' + buf.getvalue()
  119.         finally:
  120.             sys.stdout = oout
  121.             sys.stderr = oerr
  122.  
  123.         return tb
  124.  
  125.     
  126.     def create_gmail_relay(self, *args):
  127.         self.relay_username.setText('@gmail.com')
  128.         self.relay_password.setText('')
  129.         self.relay_host.setText('smtp.gmail.com')
  130.         self.relay_port.setValue(587)
  131.         self.relay_tls.setChecked(True)
  132.         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_()
  133.         self.relay_username.setFocus(Qt.OtherFocusReason)
  134.         self.relay_username.setCursorPosition(0)
  135.  
  136.     
  137.     def set_email_settings(self, to_set):
  138.         from_ = unicode(self.email_from.text()).strip()
  139.         if to_set and not from_:
  140.             error_dialog(self, _('Bad configuration'), _('You must set the From email address')).exec_()
  141.             return False
  142.         username = unicode(self.relay_username.text()).strip()
  143.         password = unicode(self.relay_password.text()).strip()
  144.         host = unicode(self.relay_host.text()).strip()
  145.         if host:
  146.             if username:
  147.                 pass
  148.             if not password:
  149.                 error_dialog(self, _('Bad configuration'), _('You must set the username and password for the mail server.')).exec_()
  150.                 return False
  151.             conf = smtp_prefs()
  152.             conf.set('from_', from_)
  153.         not password(conf.set, 'relay_host' if host else None)
  154.         conf.set('relay_port', self.relay_port.value())
  155.         not from_(conf.set, 'relay_username' if username else None)
  156.         conf.set('relay_password', hexlify(password))
  157.         None(conf.set, 'encryption' if self.relay_tls.isChecked() else 'SSL')
  158.         return True
  159.  
  160.  
  161.