home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1343 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  8.2 KB  |  209 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. from PyQt4.Qt import QAbstractTableModel, QVariant, QFont, Qt
  8. from calibre.gui2.preferences import ConfigWidgetBase, test_widget, AbortCommit
  9. from calibre.gui2.preferences.email_ui import Ui_Form
  10. from calibre.utils.config import ConfigProxy
  11. from calibre.gui2 import NONE
  12. from calibre.utils.smtp import config as smtp_prefs
  13.  
  14. class EmailAccounts(QAbstractTableModel):
  15.     
  16.     def __init__(self, accounts):
  17.         QAbstractTableModel.__init__(self)
  18.         self.accounts = accounts
  19.         self.account_order = sorted(self.accounts.keys())
  20.         self.headers = map(QVariant, [
  21.             _('Email'),
  22.             _('Formats'),
  23.             _('Auto send')])
  24.         self.default_font = QFont()
  25.         self.default_font.setBold(True)
  26.         self.default_font = QVariant(self.default_font)
  27.         self.tooltips = [
  28.             NONE] + map(QVariant, [
  29.             _('Formats to email. The first matching format will be sent.'),
  30.             '<p>' + _('If checked, downloaded news will be automatically mailed <br>to this email address (provided it is in one of the listed formats).')])
  31.  
  32.     
  33.     def rowCount(self, *args):
  34.         return len(self.account_order)
  35.  
  36.     
  37.     def columnCount(self, *args):
  38.         return 3
  39.  
  40.     
  41.     def headerData(self, section, orientation, role):
  42.         if role == Qt.DisplayRole and orientation == Qt.Horizontal:
  43.             return self.headers[section]
  44.         return NONE
  45.  
  46.     
  47.     def data(self, index, role):
  48.         row = index.row()
  49.         col = index.column()
  50.         if row < 0 or row >= self.rowCount():
  51.             return NONE
  52.         account = self.account_order[row]
  53.         if role == Qt.UserRole:
  54.             return (account, self.accounts[account])
  55.         if role == Qt.ToolTipRole:
  56.             return self.tooltips[col]
  57.         if role == Qt.FontRole and self.accounts[account][2]:
  58.             return self.default_font
  59.         if role == Qt.CheckStateRole and col == 2:
  60.             return self.accounts[account][2](QVariant if self.accounts[account][1] else Qt.Unchecked)
  61.         return NONE
  62.  
  63.     
  64.     def flags(self, index):
  65.         if index.column() == 2:
  66.             return QAbstractTableModel.flags(self, index) | Qt.ItemIsUserCheckable
  67.         return QAbstractTableModel.flags(self, index) | Qt.ItemIsEditable
  68.  
  69.     
  70.     def setData(self, index, value, role):
  71.         if not index.isValid():
  72.             return False
  73.         row = index.row()
  74.         col = index.column()
  75.         account = self.account_order[row]
  76.         if col == 2:
  77.             self.accounts[account][1] ^= True
  78.         elif col == 1:
  79.             self.accounts[account][0] = unicode(value.toString()).upper()
  80.         else:
  81.             na = unicode(value.toString())
  82.             parseaddr = parseaddr
  83.             import email.utils
  84.             addr = parseaddr(na)[-1]
  85.             if not addr:
  86.                 return False
  87.             self.accounts[na] = self.accounts.pop(account)
  88.             self.account_order[row] = na
  89.             if '@kindle.com' in addr:
  90.                 self.accounts[na][0] = 'AZW, MOBI, TPZ, PRC, AZW1'
  91.             
  92.         self.dataChanged.emit(self.index(index.row(), 0), self.index(index.row(), 2))
  93.         return True
  94.  
  95.     
  96.     def make_default(self, index):
  97.         if index.isValid():
  98.             row = index.row()
  99.             for x in self.accounts.values():
  100.                 x[2] = False
  101.             
  102.             self.accounts[self.account_order[row]][2] = True
  103.             self.reset()
  104.         
  105.  
  106.     
  107.     def add(self):
  108.         x = _('new email address')
  109.         y = x
  110.         c = 0
  111.         while y in self.accounts:
  112.             c += 1
  113.             y = x + str(c)
  114.         auto_send = len(self.accounts) < 1
  115.         self.accounts[y] = [
  116.             'MOBI, EPUB',
  117.             auto_send,
  118.             len(self.account_order) == 0]
  119.         self.account_order = sorted(self.accounts.keys())
  120.         self.reset()
  121.         return self.index(self.account_order.index(y), 0)
  122.  
  123.     
  124.     def remove(self, index):
  125.         if index.isValid():
  126.             row = index.row()
  127.             account = self.account_order[row]
  128.             self.accounts.pop(account)
  129.             self.account_order = sorted(self.accounts.keys())
  130.             has_default = False
  131.             for account in self.account_order:
  132.                 if self.accounts[account][2]:
  133.                     has_default = True
  134.                     break
  135.                     continue
  136.             
  137.             if not has_default and self.account_order:
  138.                 self.accounts[self.account_order[0]][2] = True
  139.             
  140.             self.reset()
  141.         
  142.  
  143.  
  144.  
  145. class ConfigWidget(ConfigWidgetBase, Ui_Form):
  146.     supports_restoring_to_defaults = False
  147.     
  148.     def genesis(self, gui):
  149.         self.gui = gui
  150.         self.proxy = ConfigProxy(smtp_prefs())
  151.         self.send_email_widget.initialize(self.preferred_to_address)
  152.         self.send_email_widget.changed_signal.connect(self.changed_signal.emit)
  153.         opts = self.send_email_widget.smtp_opts
  154.         self._email_accounts = EmailAccounts(opts.accounts)
  155.         (self._email_accounts.dataChanged.connect,)((lambda x, y: self.changed_signal.emit()))
  156.         self.email_view.setModel(self._email_accounts)
  157.         self.email_add.clicked.connect(self.add_email_account)
  158.         self.email_make_default.clicked.connect(self.make_default)
  159.         self.email_view.resizeColumnsToContents()
  160.         self.email_remove.clicked.connect(self.remove_email_account)
  161.  
  162.     
  163.     def preferred_to_address(self):
  164.         if self._email_accounts.account_order:
  165.             return self._email_accounts.account_order[0]
  166.  
  167.     
  168.     def initialize(self):
  169.         ConfigWidgetBase.initialize(self)
  170.  
  171.     
  172.     def restore_defaults(self):
  173.         ConfigWidgetBase.restore_defaults(self)
  174.  
  175.     
  176.     def commit(self):
  177.         to_set = bool(self._email_accounts.accounts)
  178.         if not self.send_email_widget.set_email_settings(to_set):
  179.             raise AbortCommit('abort')
  180.         self.send_email_widget.set_email_settings(to_set)
  181.         self.proxy['accounts'] = self._email_accounts.accounts
  182.         return ConfigWidgetBase.commit(self)
  183.  
  184.     
  185.     def make_default(self, *args):
  186.         self._email_accounts.make_default(self.email_view.currentIndex())
  187.         self.changed_signal.emit()
  188.  
  189.     
  190.     def add_email_account(self, *args):
  191.         index = self._email_accounts.add()
  192.         self.email_view.setCurrentIndex(index)
  193.         self.email_view.resizeColumnsToContents()
  194.         self.email_view.edit(index)
  195.         self.changed_signal.emit()
  196.  
  197.     
  198.     def remove_email_account(self, *args):
  199.         idx = self.email_view.currentIndex()
  200.         self._email_accounts.remove(idx)
  201.         self.changed_signal.emit()
  202.  
  203.  
  204. if __name__ == '__main__':
  205.     from PyQt4.Qt import QApplication
  206.     app = QApplication([])
  207.     test_widget('Sharing', 'Email')
  208.  
  209.