py3: Port email preferences widget

This commit is contained in:
Kovid Goyal 2019-04-17 19:51:29 +05:30
parent 23f22906b3
commit 11f2b80063
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,23 +1,21 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
# License: GPLv3 Copyright: 2010, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import textwrap, re import re
import textwrap
from PyQt5.Qt import QAbstractTableModel, QFont, Qt from PyQt5.Qt import QAbstractTableModel, QFont, Qt
from calibre.gui2 import gprefs
from calibre.gui2.preferences import ConfigWidgetBase, test_widget, \ from calibre.gui2.preferences import AbortCommit, ConfigWidgetBase, test_widget
AbortCommit
from calibre.gui2.preferences.email_ui import Ui_Form from calibre.gui2.preferences.email_ui import Ui_Form
from calibre.utils.config import ConfigProxy from calibre.utils.config import ConfigProxy
from calibre.utils.icu import numeric_sort_key from calibre.utils.icu import numeric_sort_key
from calibre.gui2 import gprefs
from calibre.utils.smtp import config as smtp_prefs from calibre.utils.smtp import config as smtp_prefs
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, map, as_unicode
class EmailAccounts(QAbstractTableModel): # {{{ class EmailAccounts(QAbstractTableModel): # {{{
@ -29,14 +27,14 @@ class EmailAccounts(QAbstractTableModel): # {{{
self.aliases = aliases self.aliases = aliases
self.tags = tags self.tags = tags
self.sorted_on = (0, True) self.sorted_on = (0, True)
self.account_order = self.accounts.keys() self.account_order = list(self.accounts)
self.do_sort() self.do_sort()
self.headers = map(unicode_type, [_('Email'), _('Formats'), _('Subject'), self.headers = [_('Email'), _('Formats'), _('Subject'),
_('Auto send'), _('Alias'), _('Auto send only tags')]) _('Auto send'), _('Alias'), _('Auto send only tags')]
self.default_font = QFont() self.default_font = QFont()
self.default_font.setBold(True) self.default_font.setBold(True)
self.default_font = (self.default_font) self.default_font = (self.default_font)
self.tooltips =[None] + list(map(unicode_type, map(textwrap.fill, self.tooltips =[None] + list(map(textwrap.fill,
[_('Formats to email. The first matching format will be sent.'), [_('Formats to email. The first matching format will be sent.'),
_('Subject of the email to use when sending. When left blank ' _('Subject of the email to use when sending. When left blank '
'the title will be used for the subject. Also, the same ' 'the title will be used for the subject. Also, the same '
@ -50,7 +48,7 @@ class EmailAccounts(QAbstractTableModel): # {{{
' this email address. All news downloads have their title as a' ' this email address. All news downloads have their title as a'
' tag, so you can use this to easily control which news downloads' ' tag, so you can use this to easily control which news downloads'
' are sent to this email address.') ' are sent to this email address.')
]))) ]))
def do_sort(self): def do_sort(self):
col = self.sorted_on[0] col = self.sorted_on[0]
@ -65,7 +63,7 @@ class EmailAccounts(QAbstractTableModel): # {{{
return numeric_sort_key(self.subjects.get(account_key) or '') return numeric_sort_key(self.subjects.get(account_key) or '')
elif col == 3: elif col == 3:
def key(account_key): def key(account_key):
return numeric_sort_key(unicode_type(self.accounts[account_key][0]) or '') return numeric_sort_key(as_unicode(self.accounts[account_key][0]) or '')
elif col == 4: elif col == 4:
def key(account_key): def key(account_key):
return numeric_sort_key(self.aliases.get(account_key) or '') return numeric_sort_key(self.aliases.get(account_key) or '')
@ -137,21 +135,21 @@ class EmailAccounts(QAbstractTableModel): # {{{
if col == 3: if col == 3:
self.accounts[account][1] ^= True self.accounts[account][1] ^= True
elif col == 2: elif col == 2:
self.subjects[account] = unicode_type(value or '') self.subjects[account] = as_unicode(value or '')
elif col == 4: elif col == 4:
self.aliases.pop(account, None) self.aliases.pop(account, None)
aval = unicode_type(value or '').strip() aval = as_unicode(value or '').strip()
if aval: if aval:
self.aliases[account] = aval self.aliases[account] = aval
elif col == 5: elif col == 5:
self.tags.pop(account, None) self.tags.pop(account, None)
aval = unicode_type(value or '').strip() aval = as_unicode(value or '').strip()
if aval: if aval:
self.tags[account] = aval self.tags[account] = aval
elif col == 1: elif col == 1:
self.accounts[account][0] = re.sub(',+', ',', re.sub(r'\s+', ',', unicode_type(value or '').upper())) self.accounts[account][0] = re.sub(',+', ',', re.sub(r'\s+', ',', as_unicode(value or '').upper()))
elif col == 0: elif col == 0:
na = unicode_type(value or '') na = as_unicode(value or '')
from email.utils import parseaddr from email.utils import parseaddr
addr = parseaddr(na)[-1] addr = parseaddr(na)[-1]
if not addr: if not addr:
@ -180,12 +178,12 @@ class EmailAccounts(QAbstractTableModel): # {{{
c = 0 c = 0
while y in self.accounts: while y in self.accounts:
c += 1 c += 1
y = x + str(c) y = x + unicode_type(c)
auto_send = len(self.accounts) < 1 auto_send = len(self.accounts) < 1
self.beginResetModel() self.beginResetModel()
self.accounts[y] = ['MOBI, EPUB', auto_send, self.accounts[y] = ['MOBI, EPUB', auto_send,
len(self.account_order) == 0] len(self.account_order) == 0]
self.account_order = self.accounts.keys() self.account_order = list(self.accounts)
self.do_sort() self.do_sort()
self.endResetModel() self.endResetModel()
return self.index(self.account_order.index(y), 0) return self.index(self.account_order.index(y), 0)
@ -195,7 +193,7 @@ class EmailAccounts(QAbstractTableModel): # {{{
row = index.row() row = index.row()
account = self.account_order[row] account = self.account_order[row]
self.accounts.pop(account) self.accounts.pop(account)
self.account_order = sorted(self.accounts.keys()) self.account_order = sorted(self.accounts)
has_default = False has_default = False
for account in self.account_order: for account in self.account_order:
if self.accounts[account][2]: if self.accounts[account][2]: