Improved search

pull/152/head
M66B 6 years ago
parent e2eaef058d
commit 0759c66774

@ -197,78 +197,64 @@ public class ViewModelBrowse extends ViewModel {
} }
try { try {
if (protocol.supportsUtf8()) { // https://tools.ietf.org/html/rfc3501#section-6.4.4
// SEARCH OR OR FROM "x" TO "x" OR SUBJECT "x" BODY "x" ALL Log.i("Boundary UTF8 search=" + state.search);
// SEARCH OR OR OR FROM "x" TO "x" OR SUBJECT "x" BODY "x" (KEYWORD x) ALL Argument arg = new Argument();
// https://tools.ietf.org/html/rfc3501#section-6.4.4 if (!protocol.supportsUtf8()) {
Log.i("Boundary UTF8 search=" + state.search); arg.writeAtom("CHARSET");
Argument arg = new Argument(); arg.writeAtom("UTF-8");
if (keywords) }
arg.writeAtom("OR"); if (keywords)
arg.writeAtom("OR");
arg.writeAtom("OR");
arg.writeAtom("FROM");
arg.writeBytes(state.search.getBytes());
arg.writeAtom("TO");
arg.writeBytes(state.search.getBytes());
arg.writeAtom("OR"); arg.writeAtom("OR");
arg.writeAtom("SUBJECT"); arg.writeAtom("TEXT");
arg.writeBytes(state.search.getBytes());
if (keywords) {
arg.writeAtom("KEYWORD");
arg.writeBytes(state.search.getBytes()); arg.writeBytes(state.search.getBytes());
arg.writeAtom("BODY"); }
arg.writeBytes(state.search.getBytes()); arg.writeAtom("ALL");
if (keywords) { Response[] responses = protocol.command("SEARCH", arg);
arg.writeAtom("KEYWORD");
arg.writeBytes(state.search.getBytes()); List<Integer> msgnums = new ArrayList<>();
} for (Response response : responses)
arg.writeAtom("ALL"); if (response.isOK())
Response[] responses = protocol.command("SEARCH", arg); Log.i(folder.name + " response=" + response);
else if (((IMAPResponse) response).keyEquals("SEARCH")) {
int msgnum; int msgnum;
List<Integer> msgnums = new ArrayList<>(); while ((msgnum = response.readNumber()) != -1)
for (int i = 0; i < responses.length; i++) { msgnums.add(msgnum);
if (responses[i] instanceof IMAPResponse) { } else {
IMAPResponse ir = (IMAPResponse) responses[i]; Log.w(folder.name + " response=" + response);
if (ir.keyEquals("SEARCH")) {
while ((msgnum = ir.readNumber()) != -1) // Assume no UTF-8 support
msgnums.add(msgnum); String search = state.search.replace("ß", "ss"); // Eszett
} search = Normalizer.normalize(search, Normalizer.Form.NFD)
} else { .replaceAll("[^\\p{ASCII}]", "");
if (responses[i].isOK())
Log.i(folder.name + " response=" + responses[i]); Log.i("Boundary ASCII search=" + search);
else SearchTerm term = new OrTerm(
throw new MessagingException(responses[i].toString()); new OrTerm(
} new FromStringTerm(search),
new RecipientStringTerm(Message.RecipientType.TO, search)
),
new OrTerm(
new SubjectTerm(search),
new BodyTerm(search)
)
);
if (keywords)
term = new OrTerm(term, new FlagTerm(
new Flags(Helper.sanitizeKeyword(search)), true));
return state.ifolder.search(term);
} }
Message[] imessages = new Message[msgnums.size()]; Message[] imessages = new Message[msgnums.size()];
for (int i = 0; i < msgnums.size(); i++) for (int i = 0; i < msgnums.size(); i++)
imessages[i] = state.ifolder.getMessage(msgnums.get(i)); imessages[i] = state.ifolder.getMessage(msgnums.get(i));
return imessages; return imessages;
} else {
// No UTF-8 support
String search = state.search.replace("ß", "ss"); // Eszett
search = Normalizer.normalize(search, Normalizer.Form.NFD)
.replaceAll("[^\\p{ASCII}]", "");
Log.i("Boundary ASCII search=" + search);
SearchTerm term = new OrTerm(
new OrTerm(
new FromStringTerm(search),
new RecipientStringTerm(Message.RecipientType.TO, search)
),
new OrTerm(
new SubjectTerm(search),
new BodyTerm(search)
)
);
if (keywords)
term = new OrTerm(term, new FlagTerm(
new Flags(Helper.sanitizeKeyword(search)), true));
return state.ifolder.search(term);
}
} catch (MessagingException ex) { } catch (MessagingException ex) {
Log.e(ex); Log.e(ex);
return ex; return ex;

Loading…
Cancel
Save