2019-05-17 17:43:06 +02:00
|
|
|
"""
|
|
|
|
Additional SQL helper for searching messages.
|
|
|
|
Used by :mod:`.bitmessageqt`.
|
|
|
|
"""
|
2019-10-09 13:00:12 +02:00
|
|
|
|
2019-12-30 13:10:02 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
2019-10-09 13:00:12 +02:00
|
|
|
from helper_sql import sqlQuery
|
2019-05-17 17:43:06 +02:00
|
|
|
from tr import _translate
|
2016-02-14 19:56:05 +01:00
|
|
|
|
2019-10-09 13:00:12 +02:00
|
|
|
|
2019-05-17 17:43:06 +02:00
|
|
|
def search_sql(
|
|
|
|
xAddress='toaddress', account=None, folder='inbox', where=None,
|
|
|
|
what=None, unreadOnly=False
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Search for messages from given account and folder having search term
|
|
|
|
in one of it's fields.
|
2019-10-09 13:00:12 +02:00
|
|
|
|
2019-05-17 17:43:06 +02:00
|
|
|
:param str xAddress: address field checked
|
|
|
|
('fromaddress', 'toaddress' or 'both')
|
|
|
|
:param account: the account which is checked
|
|
|
|
:type account: :class:`.bitmessageqt.account.BMAccount`
|
|
|
|
instance
|
|
|
|
:param str folder: the folder which is checked
|
|
|
|
:param str where: message field which is checked ('toaddress',
|
|
|
|
'fromaddress', 'subject' or 'message'), by default check any field
|
|
|
|
:param str what: the search term
|
|
|
|
:param bool unreadOnly: if True, search only for unread messages
|
|
|
|
:return: all messages where <where> field contains <what>
|
|
|
|
:rtype: list[list]
|
|
|
|
"""
|
2019-10-09 14:03:51 +02:00
|
|
|
# pylint: disable=too-many-arguments, too-many-branches
|
2019-05-17 17:43:06 +02:00
|
|
|
if what:
|
|
|
|
what = '%' + what + '%'
|
|
|
|
if where == _translate("MainWindow", "To"):
|
|
|
|
where = 'toaddress'
|
|
|
|
elif where == _translate("MainWindow", "From"):
|
|
|
|
where = 'fromaddress'
|
|
|
|
elif where == _translate("MainWindow", "Subject"):
|
|
|
|
where = 'subject'
|
|
|
|
elif where == _translate("MainWindow", "Message"):
|
|
|
|
where = 'message'
|
2016-02-14 19:56:05 +01:00
|
|
|
else:
|
2019-05-17 17:43:06 +02:00
|
|
|
where = 'toaddress || fromaddress || subject || message'
|
2016-02-14 19:56:05 +01:00
|
|
|
|
2019-05-17 17:43:06 +02:00
|
|
|
sqlStatementBase = 'SELECT toaddress, fromaddress, subject, ' + (
|
|
|
|
'status, ackdata, lastactiontime FROM sent ' if folder == 'sent'
|
|
|
|
else 'folder, msgid, received, read FROM inbox '
|
|
|
|
)
|
2016-02-14 19:56:05 +01:00
|
|
|
|
|
|
|
sqlStatementParts = []
|
|
|
|
sqlArguments = []
|
|
|
|
if account is not None:
|
2016-03-12 09:58:16 +01:00
|
|
|
if xAddress == 'both':
|
2019-05-17 17:43:06 +02:00
|
|
|
sqlStatementParts.append('(fromaddress = ? OR toaddress = ?)')
|
2016-03-12 09:58:16 +01:00
|
|
|
sqlArguments.append(account)
|
|
|
|
sqlArguments.append(account)
|
|
|
|
else:
|
2019-05-17 17:43:06 +02:00
|
|
|
sqlStatementParts.append(xAddress + ' = ? ')
|
2016-03-12 09:58:16 +01:00
|
|
|
sqlArguments.append(account)
|
2016-02-14 19:56:05 +01:00
|
|
|
if folder is not None:
|
2019-05-17 17:43:06 +02:00
|
|
|
if folder == 'new':
|
|
|
|
folder = 'inbox'
|
2016-02-14 19:56:05 +01:00
|
|
|
unreadOnly = True
|
2019-05-17 17:43:06 +02:00
|
|
|
sqlStatementParts.append('folder = ? ')
|
2016-02-14 19:56:05 +01:00
|
|
|
sqlArguments.append(folder)
|
|
|
|
else:
|
2019-05-17 17:43:06 +02:00
|
|
|
sqlStatementParts.append('folder != ?')
|
|
|
|
sqlArguments.append('trash')
|
|
|
|
if what:
|
|
|
|
sqlStatementParts.append('%s LIKE ?' % (where))
|
2016-02-14 19:56:05 +01:00
|
|
|
sqlArguments.append(what)
|
|
|
|
if unreadOnly:
|
2019-05-17 17:43:06 +02:00
|
|
|
sqlStatementParts.append('read = 0')
|
2019-10-09 14:03:51 +02:00
|
|
|
if sqlStatementParts:
|
2019-05-17 17:43:06 +02:00
|
|
|
sqlStatementBase += 'WHERE ' + ' AND '.join(sqlStatementParts)
|
|
|
|
if folder == 'sent':
|
|
|
|
sqlStatementBase += ' ORDER BY lastactiontime'
|
2016-02-14 19:56:05 +01:00
|
|
|
return sqlQuery(sqlStatementBase, sqlArguments)
|
|
|
|
|
2019-10-09 13:00:12 +02:00
|
|
|
|
2019-05-17 17:43:06 +02:00
|
|
|
def check_match(
|
|
|
|
toAddress, fromAddress, subject, message, where=None, what=None):
|
|
|
|
"""
|
|
|
|
Check if a single message matches a filter (used when new messages
|
|
|
|
are added to messagelists)
|
|
|
|
"""
|
2019-10-09 14:03:51 +02:00
|
|
|
# pylint: disable=too-many-arguments
|
2019-05-17 17:43:06 +02:00
|
|
|
if not what:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if where in (
|
|
|
|
_translate("MainWindow", "To"), _translate("MainWindow", "All")
|
|
|
|
):
|
|
|
|
if what.lower() not in toAddress.lower():
|
|
|
|
return False
|
|
|
|
elif where in (
|
|
|
|
_translate("MainWindow", "From"), _translate("MainWindow", "All")
|
|
|
|
):
|
|
|
|
if what.lower() not in fromAddress.lower():
|
|
|
|
return False
|
|
|
|
elif where in (
|
|
|
|
_translate("MainWindow", "Subject"),
|
|
|
|
_translate("MainWindow", "All")
|
|
|
|
):
|
|
|
|
if what.lower() not in subject.lower():
|
|
|
|
return False
|
|
|
|
elif where in (
|
|
|
|
_translate("MainWindow", "Message"),
|
|
|
|
_translate("MainWindow", "All")
|
|
|
|
):
|
|
|
|
if what.lower() not in message.lower():
|
|
|
|
return False
|
2016-02-14 19:56:05 +01:00
|
|
|
return True
|