Search improvements

- selecting a new folder will apply search
- newly arrived message will consider search
- fixes #166
This commit is contained in:
Peter Šurda 2016-02-14 19:56:05 +01:00
parent 218bdf38e1
commit db84d699db
2 changed files with 125 additions and 80 deletions

View File

@ -59,6 +59,7 @@ import subprocess
import string import string
import datetime import datetime
from helper_sql import * from helper_sql import *
import helper_search
import l10n import l10n
import openclpow import openclpow
import types import types
@ -1081,20 +1082,6 @@ class MyForm(settingsmixin.SMainWindow):
# Load Sent items from database # Load Sent items from database
def loadSent(self, tableWidget, account, where="", what=""): def loadSent(self, tableWidget, account, where="", 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"
else:
where = "toaddress || fromaddress || subject || message"
tableWidget.setSortingEnabled(False)
if tableWidget == self.ui.tableWidgetInboxChans or tableWidget == self.ui.tableWidgetInboxSubscriptions: if tableWidget == self.ui.tableWidgetInboxChans or tableWidget == self.ui.tableWidgetInboxSubscriptions:
tableWidget.setColumnHidden(0, True) tableWidget.setColumnHidden(0, True)
tableWidget.setColumnHidden(1, False) tableWidget.setColumnHidden(1, False)
@ -1103,16 +1090,11 @@ class MyForm(settingsmixin.SMainWindow):
tableWidget.setColumnHidden(0, False) tableWidget.setColumnHidden(0, False)
tableWidget.setColumnHidden(1, True) tableWidget.setColumnHidden(1, True)
xAddress = 'fromaddress' xAddress = 'fromaddress'
sqlStatement = '''
SELECT toaddress, fromaddress, subject, status, ackdata, lastactiontime
FROM sent WHERE ''' + xAddress + '''=? AND folder="sent" AND %s LIKE ?
ORDER BY lastactiontime
''' % (where,)
tableWidget.setSortingEnabled(False)
tableWidget.setRowCount(0) tableWidget.setRowCount(0)
acct = None queryreturn = helper_search.search_sql(xAddress, account, "sent", where, what, False)
queryreturn = sqlQuery(sqlStatement, account, what)
for row in queryreturn: for row in queryreturn:
toAddress, fromAddress, subject, status, ackdata, lastactiontime = row toAddress, fromAddress, subject, status, ackdata, lastactiontime = row
self.addMessageListItemSent(tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime) self.addMessageListItemSent(tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime)
@ -1127,52 +1109,21 @@ class MyForm(settingsmixin.SMainWindow):
self.loadSent(tableWidget, account, where, what) self.loadSent(tableWidget, account, where, what)
return return
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"
else:
where = "toaddress || fromaddress || subject || message"
else:
what = None
if tableWidget == self.ui.tableWidgetInboxSubscriptions: if tableWidget == self.ui.tableWidgetInboxSubscriptions:
xAddress = "fromaddress" xAddress = "fromaddress"
else: else:
xAddress = "toaddress" xAddress = "toaddress"
sqlStatementBase = '''SELECT folder, msgid, toaddress, fromaddress, subject, received, read
FROM inbox '''
sqlStatementParts = []
sqlArguments = []
if account is not None:
sqlStatementParts.append(xAddress + " = ? ")
sqlArguments.append(account)
if folder is not None:
sqlStatementParts.append("folder = ? ")
sqlArguments.append(folder)
if what is not None:
sqlStatementParts.append("%s LIKE ?" % (where))
sqlArguments.append(what)
if unreadOnly:
sqlStatementParts.append("read = 0")
if len(sqlStatementParts) > 0:
sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts)
queryreturn = sqlQuery(sqlStatementBase, sqlArguments)
tableWidget.setRowCount(0)
if account is not None: if account is not None:
tableWidget.setColumnHidden(0, True) tableWidget.setColumnHidden(0, True)
tableWidget.setColumnHidden(1, False) tableWidget.setColumnHidden(1, False)
else: else:
tableWidget.setColumnHidden(0, False) tableWidget.setColumnHidden(0, False)
tableWidget.setColumnHidden(1, False) tableWidget.setColumnHidden(1, False)
tableWidget.setSortingEnabled(False) tableWidget.setSortingEnabled(False)
tableWidget.setRowCount(0)
queryreturn = helper_search.search_sql(xAddress, account, folder, where, what, unreadOnly)
for row in queryreturn: for row in queryreturn:
msgfolder, msgid, toAddress, fromAddress, subject, received, read = row msgfolder, msgid, toAddress, fromAddress, subject, received, read = row
@ -2336,7 +2287,11 @@ class MyForm(settingsmixin.SMainWindow):
def displayNewSentMessage(self, toAddress, toLabel, fromAddress, subject, message, ackdata): def displayNewSentMessage(self, toAddress, toLabel, fromAddress, subject, message, ackdata):
acct = accountClass(fromAddress) acct = accountClass(fromAddress)
acct.parseMessage(toAddress, fromAddress, subject, message) acct.parseMessage(toAddress, fromAddress, subject, message)
tab = -1
for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]: for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]:
tab += 1
if tab == 1:
tab = 2
treeWidget = self.widgetConvert(sent) treeWidget = self.widgetConvert(sent)
if self.getCurrentFolder(treeWidget) != "sent": if self.getCurrentFolder(treeWidget) != "sent":
continue continue
@ -2344,6 +2299,8 @@ class MyForm(settingsmixin.SMainWindow):
continue continue
elif treeWidget in [self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans] and self.getCurrentAccount(treeWidget) != toAddress: elif treeWidget in [self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans] and self.getCurrentAccount(treeWidget) != toAddress:
continue continue
elif not helper_search.check_match(toAddress, fromAddress, subject, message, self.getCurrentSearchOption(tab), self.getCurrentSearchLine(tab)):
continue
self.addMessageListItemSent(sent, toAddress, fromAddress, subject, "msgqueued", ackdata, time.time()) self.addMessageListItemSent(sent, toAddress, fromAddress, subject, "msgqueued", ackdata, time.time())
self.getAccountTextedit(acct).setPlainText(unicode(message, 'utf-8)', 'replace')) self.getAccountTextedit(acct).setPlainText(unicode(message, 'utf-8)', 'replace'))
@ -2356,8 +2313,14 @@ class MyForm(settingsmixin.SMainWindow):
acct = accountClass(toAddress) acct = accountClass(toAddress)
inbox = self.getAccountMessagelist(acct) inbox = self.getAccountMessagelist(acct)
ret = None ret = None
tab = -1
for treeWidget in [self.ui.treeWidgetYourIdentities, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans]: for treeWidget in [self.ui.treeWidgetYourIdentities, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans]:
tab += 1
if tab == 1:
tab = 2
tableWidget = self.widgetConvert(treeWidget) tableWidget = self.widgetConvert(treeWidget)
if not helper_search.check_match(toAddress, fromAddress, subject, message, self.getCurrentSearchOption(tab), self.getCurrentSearchLine(tab)):
continue
if tableWidget == inbox and self.getCurrentAccount(treeWidget) == acct.address and self.getCurrentFolder(treeWidget) in ["inbox", None]: if tableWidget == inbox and self.getCurrentAccount(treeWidget) == acct.address and self.getCurrentFolder(treeWidget) in ["inbox", None]:
ret = self.addMessageListItemInbox(inbox, "inbox", inventoryHash, toAddress, fromAddress, subject, time.time(), 0) ret = self.addMessageListItemInbox(inbox, "inbox", inventoryHash, toAddress, fromAddress, subject, time.time(), 0)
elif treeWidget == self.ui.treeWidgetYourIdentities and self.getCurrentAccount(treeWidget) is None: elif treeWidget == self.ui.treeWidgetYourIdentities and self.getCurrentAccount(treeWidget) is None:
@ -3518,8 +3481,9 @@ class MyForm(settingsmixin.SMainWindow):
except: except:
return self.ui.textEditInboxMessage return self.ui.textEditInboxMessage
def getCurrentSearchLine(self): def getCurrentSearchLine(self, currentIndex = None):
currentIndex = self.ui.tabWidget.currentIndex(); if currentIndex is None:
currentIndex = self.ui.tabWidget.currentIndex();
messagelistList = [ messagelistList = [
self.ui.inboxSearchLineEdit, self.ui.inboxSearchLineEdit,
False, False,
@ -3527,12 +3491,13 @@ class MyForm(settingsmixin.SMainWindow):
self.ui.inboxSearchLineEditChans, self.ui.inboxSearchLineEditChans,
] ]
if currentIndex >= 0 and currentIndex < len(messagelistList): if currentIndex >= 0 and currentIndex < len(messagelistList):
return messagelistList[currentIndex] return messagelistList[currentIndex].text().toUtf8().data()
else: else:
return False return None
def getCurrentSearchOption(self): def getCurrentSearchOption(self, currentIndex = None):
currentIndex = self.ui.tabWidget.currentIndex(); if currentIndex is None:
currentIndex = self.ui.tabWidget.currentIndex();
messagelistList = [ messagelistList = [
self.ui.inboxSearchOption, self.ui.inboxSearchOption,
False, False,
@ -3542,7 +3507,7 @@ class MyForm(settingsmixin.SMainWindow):
if currentIndex >= 0 and currentIndex < len(messagelistList): if currentIndex >= 0 and currentIndex < len(messagelistList):
return messagelistList[currentIndex].currentText().toUtf8().data() return messagelistList[currentIndex].currentText().toUtf8().data()
else: else:
return False return None
# Group of functions for the Your Identities dialog box # Group of functions for the Your Identities dialog box
def getCurrentItem(self, treeWidget = None): def getCurrentItem(self, treeWidget = None):
@ -3842,26 +3807,26 @@ class MyForm(settingsmixin.SMainWindow):
def inboxSearchLineEditPressed(self): def inboxSearchLineEditPressed(self):
searchLine = self.getCurrentSearchLine() searchLine = self.getCurrentSearchLine()
searchOption = self.getCurrentSearchOption() searchOption = self.getCurrentSearchOption()
if searchLine: messageTextedit = self.getCurrentMessageTextedit()
searchKeyword = searchLine.text().toUtf8().data() if messageTextedit:
messageTextedit = self.getCurrentMessageTextedit() messageTextedit.setPlainText(QString(""))
if messageTextedit:
messageTextedit.setPlainText(QString(""))
messagelist = self.getCurrentMessagelist()
if messagelist:
account = self.getCurrentAccount()
folder = self.getCurrentFolder()
self.loadMessagelist(messagelist, account, folder, searchOption, searchKeyword)
def treeWidgetItemClicked(self):
messagelist = self.getCurrentMessagelist() messagelist = self.getCurrentMessagelist()
if messagelist: if messagelist:
account = self.getCurrentAccount() account = self.getCurrentAccount()
folder = self.getCurrentFolder() folder = self.getCurrentFolder()
if folder == "new": self.loadMessagelist(messagelist, account, folder, searchOption, searchLine)
self.loadMessagelist(messagelist, account, None, unreadOnly = True)
else: def treeWidgetItemClicked(self):
self.loadMessagelist(messagelist, account, folder) searchLine = self.getCurrentSearchLine()
searchOption = self.getCurrentSearchOption()
messageTextedit = self.getCurrentMessageTextedit()
if messageTextedit:
messageTextedit.setPlainText(QString(""))
messagelist = self.getCurrentMessagelist()
if messagelist:
account = self.getCurrentAccount()
folder = self.getCurrentFolder()
self.loadMessagelist(messagelist, account, folder, searchOption, searchLine)
def treeWidgetItemChanged(self, item, column): def treeWidgetItemChanged(self, item, column):
# only for manual edits. automatic edits (setText) are ignored # only for manual edits. automatic edits (setText) are ignored

80
src/helper_search.py Normal file
View File

@ -0,0 +1,80 @@
#!/usr/bin/python2.7
from helper_sql import *
try:
from PyQt4 import QtCore, QtGui
haveQt = True
except:
haveQt = False
def search_translate (context, text):
if haveQt:
return QtGui.QApplication.translate(context, text)
else:
return text.lower()
def search_sql(xAddress = "toaddress", account = None, folder = "inbox", where = None, what = None, unreadOnly = False):
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 = '''
SELECT toaddress, fromaddress, subject, status, ackdata, lastactiontime
FROM sent '''
else:
sqlStatementBase = '''SELECT folder, msgid, toaddress, fromaddress, subject, received, read
FROM inbox '''
sqlStatementParts = []
sqlArguments = []
if account is not None:
sqlStatementParts.append(xAddress + " = ? ")
sqlArguments.append(account)
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")
if len(sqlStatementParts) > 0:
sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts)
if folder == "sent":
sqlStatementBase += " ORDER BY lastactiontime"
return sqlQuery(sqlStatementBase, sqlArguments)
def check_match(toAddress, fromAddress, subject, message, where = None, what = None):
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