/ src / helper_search.py
helper_search.py
  1  """
  2  Additional SQL helper for searching messages.
  3  Used by :mod:`.bitmessageqt`.
  4  """
  5  
  6  from .helper_sql import sqlQuery
  7  from .tr import _translate
  8  
  9  
 10  def search_sql(
 11      xAddress='toaddress', account=None, folder='inbox', where=None,
 12      what=None, unreadOnly=False
 13  ):
 14      """
 15      Search for messages from given account and folder having search term
 16      in one of it's fields.
 17  
 18      :param str xAddress: address field checked
 19        ('fromaddress', 'toaddress' or 'both')
 20      :param account: the account which is checked
 21      :type account: :class:`.bitmessageqt.account.BMAccount`
 22        instance
 23      :param str folder: the folder which is checked
 24      :param str where: message field which is checked ('toaddress',
 25        'fromaddress', 'subject' or 'message'), by default check any field
 26      :param str what: the search term
 27      :param bool unreadOnly: if True, search only for unread messages
 28      :return: all messages where <where> field contains <what>
 29      :rtype: list[list]
 30      """
 31      # pylint: disable=too-many-branches
 32      if what:
 33          what = '%' + what + '%'
 34          if where == _translate("MainWindow", "To"):
 35              where = 'toaddress'
 36          elif where == _translate("MainWindow", "From"):
 37              where = 'fromaddress'
 38          elif where == _translate("MainWindow", "Subject"):
 39              where = 'subject'
 40          elif where == _translate("MainWindow", "Message"):
 41              where = 'message'
 42          else:
 43              where = 'toaddress || fromaddress || subject || message'
 44  
 45      sqlStatementBase = 'SELECT toaddress, fromaddress, subject, ' + (
 46          'status, ackdata, lastactiontime FROM sent ' if folder == 'sent'
 47          else 'folder, msgid, received, read FROM inbox '
 48      )
 49  
 50      sqlStatementParts = []
 51      sqlArguments = []
 52      if account is not None:
 53          if xAddress == 'both':
 54              sqlStatementParts.append('(fromaddress = ? OR toaddress = ?)')
 55              sqlArguments.append(account)
 56              sqlArguments.append(account)
 57          else:
 58              sqlStatementParts.append(xAddress + ' = ? ')
 59              sqlArguments.append(account)
 60      if folder is not None:
 61          if folder == 'new':
 62              folder = 'inbox'
 63              unreadOnly = True
 64          sqlStatementParts.append('folder = ? ')
 65          sqlArguments.append(folder)
 66      else:
 67          sqlStatementParts.append('folder != ?')
 68          sqlArguments.append('trash')
 69      if what:
 70          sqlStatementParts.append('%s LIKE ?' % (where))
 71          sqlArguments.append(what)
 72      if unreadOnly:
 73          sqlStatementParts.append('read = 0')
 74      if sqlStatementParts:
 75          sqlStatementBase += 'WHERE ' + ' AND '.join(sqlStatementParts)
 76      if folder == 'sent':
 77          sqlStatementBase += ' ORDER BY lastactiontime'
 78      return sqlQuery(sqlStatementBase, sqlArguments)
 79  
 80  
 81  def check_match(
 82          toAddress, fromAddress, subject, message, where=None, what=None):
 83      """
 84      Check if a single message matches a filter (used when new messages
 85      are added to messagelists)
 86      """
 87      if not what:
 88          return True
 89  
 90      if where in (
 91          _translate("MainWindow", "To"), _translate("MainWindow", "All")
 92      ):
 93          if what.lower() not in toAddress.lower():
 94              return False
 95      elif where in (
 96          _translate("MainWindow", "From"), _translate("MainWindow", "All")
 97      ):
 98          if what.lower() not in fromAddress.lower():
 99              return False
100      elif where in (
101          _translate("MainWindow", "Subject"),
102          _translate("MainWindow", "All")
103      ):
104          if what.lower() not in subject.lower():
105              return False
106      elif where in (
107          _translate("MainWindow", "Message"),
108          _translate("MainWindow", "All")
109      ):
110          if what.lower() not in message.lower():
111              return False
112      return True