2019-10-05 11:52:28 +02:00
|
|
|
"""Additional SQL helper for searching messages"""
|
2019-10-09 13:00:12 +02:00
|
|
|
|
|
|
|
from helper_sql import sqlQuery
|
2016-02-14 19:56:05 +01:00
|
|
|
|
|
|
|
try:
|
2018-04-07 10:40:50 +02:00
|
|
|
from PyQt4 import QtGui
|
2016-02-14 19:56:05 +01:00
|
|
|
haveQt = True
|
2018-04-30 16:55:10 +02:00
|
|
|
except ImportError:
|
2016-02-14 19:56:05 +01:00
|
|
|
haveQt = False
|
|
|
|
|
2019-10-09 13:00:12 +02:00
|
|
|
|
|
|
|
def search_translate(context, text):
|
2019-10-09 14:03:51 +02:00
|
|
|
"""Translation wrapper"""
|
2016-02-14 19:56:05 +01:00
|
|
|
if haveQt:
|
|
|
|
return QtGui.QApplication.translate(context, text)
|
2019-10-09 14:03:51 +02:00
|
|
|
return text.lower()
|
2016-02-14 19:56:05 +01:00
|
|
|
|
2019-10-09 13:00:12 +02:00
|
|
|
|
|
|
|
def search_sql(xAddress="toaddress", account=None, folder="inbox", where=None, what=None, unreadOnly=False):
|
2019-10-09 14:03:51 +02:00
|
|
|
"""Perform a search in mailbox tables"""
|
|
|
|
# pylint: disable=too-many-arguments, too-many-branches
|
2016-02-14 19:56:05 +01:00
|
|
|
if what is not None and what != "":
|
|
|
|
what = "%" + what + "%"
|
|
|
|
if where == search_translate("MainWindow", "To"):
|
|
|
|
where = "toaddress"
|
|
|
|
elif where == search_translate("MainWindow", "From"):
|
|
|
|
where = "fromaddress"
|
|
|
|
elif where == search_translate("MainWindow", "Subject"):
|
|
|
|
where = "subject"
|
|
|
|
elif where == search_translate("MainWindow", "Message"):
|
|
|
|
where = "message"
|
|
|
|
else:
|
|
|
|
where = "toaddress || fromaddress || subject || message"
|
|
|
|
else:
|
|
|
|
what = None
|
|
|
|
|
|
|
|
if folder == "sent":
|
|
|
|
sqlStatementBase = '''
|
2019-10-09 13:00:12 +02:00
|
|
|
SELECT toaddress, fromaddress, subject, status, ackdata, lastactiontime
|
2016-02-14 19:56:05 +01:00
|
|
|
FROM sent '''
|
|
|
|
else:
|
|
|
|
sqlStatementBase = '''SELECT folder, msgid, toaddress, fromaddress, subject, received, read
|
|
|
|
FROM inbox '''
|
|
|
|
|
|
|
|
sqlStatementParts = []
|
|
|
|
sqlArguments = []
|
|
|
|
if account is not None:
|
2016-03-12 09:58:16 +01:00
|
|
|
if xAddress == 'both':
|
|
|
|
sqlStatementParts.append("(fromaddress = ? OR toaddress = ?)")
|
|
|
|
sqlArguments.append(account)
|
|
|
|
sqlArguments.append(account)
|
|
|
|
else:
|
|
|
|
sqlStatementParts.append(xAddress + " = ? ")
|
|
|
|
sqlArguments.append(account)
|
2016-02-14 19:56:05 +01:00
|
|
|
if folder is not None:
|
|
|
|
if folder == "new":
|
|
|
|
folder = "inbox"
|
|
|
|
unreadOnly = True
|
|
|
|
sqlStatementParts.append("folder = ? ")
|
|
|
|
sqlArguments.append(folder)
|
|
|
|
else:
|
|
|
|
sqlStatementParts.append("folder != ?")
|
|
|
|
sqlArguments.append("trash")
|
|
|
|
if what is not None:
|
|
|
|
sqlStatementParts.append("%s LIKE ?" % (where))
|
|
|
|
sqlArguments.append(what)
|
|
|
|
if unreadOnly:
|
|
|
|
sqlStatementParts.append("read = 0")
|
2019-10-09 14:03:51 +02:00
|
|
|
if sqlStatementParts:
|
2016-02-14 19:56:05 +01:00
|
|
|
sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts)
|
|
|
|
if folder == "sent":
|
|
|
|
sqlStatementBase += " ORDER BY lastactiontime"
|
|
|
|
return sqlQuery(sqlStatementBase, sqlArguments)
|
|
|
|
|
2019-10-09 13:00:12 +02:00
|
|
|
|
|
|
|
def check_match(toAddress, fromAddress, subject, message, where=None, what=None):
|
2019-10-09 14:03:51 +02:00
|
|
|
"""Check if a single message matches a filter (used when new messages are added to messagelists)"""
|
|
|
|
# pylint: disable=too-many-arguments
|
2016-02-14 19:56:05 +01:00
|
|
|
if what is not None and what != "":
|
|
|
|
if where in (search_translate("MainWindow", "To"), search_translate("MainWindow", "All")):
|
|
|
|
if what.lower() not in toAddress.lower():
|
|
|
|
return False
|
|
|
|
elif where in (search_translate("MainWindow", "From"), search_translate("MainWindow", "All")):
|
|
|
|
if what.lower() not in fromAddress.lower():
|
|
|
|
return False
|
|
|
|
elif where in (search_translate("MainWindow", "Subject"), search_translate("MainWindow", "All")):
|
|
|
|
if what.lower() not in subject.lower():
|
|
|
|
return False
|
|
|
|
elif where in (search_translate("MainWindow", "Message"), search_translate("MainWindow", "All")):
|
|
|
|
if what.lower() not in message.lower():
|
|
|
|
return False
|
|
|
|
return True
|