Files
FairEmail/app/src/main/java/eu/faircode/email/ViewModelBrowse.java

355 lines
15 KiB
Java
Raw Normal View History

2018-10-20 16:05:43 +00:00
package eu.faircode.email;
2018-10-29 08:09:56 +00:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-10-29 08:09:56 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-10-29 08:09:56 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-10-29 08:09:56 +00:00
*/
2018-10-20 16:05:43 +00:00
import android.content.Context;
2019-01-22 13:31:27 +00:00
import com.sun.mail.iap.Argument;
import com.sun.mail.iap.Response;
2018-10-20 16:05:43 +00:00
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;
2019-01-22 13:31:27 +00:00
import com.sun.mail.imap.protocol.IMAPProtocol;
import com.sun.mail.imap.protocol.IMAPResponse;
2018-10-20 16:05:43 +00:00
2018-11-04 08:59:09 +00:00
import java.io.IOException;
2019-01-17 21:41:00 +00:00
import java.util.ArrayList;
2018-10-20 16:05:43 +00:00
import java.util.Arrays;
2018-11-04 08:59:09 +00:00
import java.util.List;
2018-10-20 16:05:43 +00:00
import java.util.Properties;
import javax.mail.FetchProfile;
2018-11-26 12:46:02 +01:00
import javax.mail.Flags;
2018-10-20 16:05:43 +00:00
import javax.mail.Folder;
import javax.mail.FolderClosedException;
2018-10-20 16:05:43 +00:00
import javax.mail.Message;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.UIDFolder;
import javax.mail.search.BodyTerm;
2018-11-26 12:46:02 +01:00
import javax.mail.search.FlagTerm;
2018-10-20 16:05:43 +00:00
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.RecipientStringTerm;
2018-12-27 10:36:42 +00:00
import javax.mail.search.SearchTerm;
2018-10-20 16:05:43 +00:00
import javax.mail.search.SubjectTerm;
import androidx.lifecycle.ViewModel;
public class ViewModelBrowse extends ViewModel {
2018-11-14 07:39:17 +01:00
private State currentState = null;
2018-10-20 16:05:43 +00:00
2018-11-14 07:39:17 +01:00
private class State {
private Context context;
private long fid;
private String search;
private int pageSize;
2018-10-21 06:39:38 +00:00
2018-11-14 07:39:17 +01:00
int local = 0;
List<Long> messages = null;
IMAPStore istore = null;
IMAPFolder ifolder = null;
Message[] imessages = null;
2018-10-20 16:05:43 +00:00
2018-11-14 07:39:17 +01:00
int index = -1;
2019-01-22 09:08:12 +00:00
boolean error = false;
2018-10-20 16:05:43 +00:00
}
2018-11-14 07:39:17 +01:00
void set(Context context, long folder, String search, int pageSize) {
currentState = new State();
currentState.context = context;
currentState.fid = folder;
currentState.search = search;
currentState.pageSize = pageSize;
currentState.index = -1;
2019-01-22 09:08:12 +00:00
currentState.error = false;
2018-10-21 14:07:45 +00:00
}
2018-12-26 09:14:53 +00:00
boolean isSearching() {
State state = currentState;
return (state != null && state.search != null);
}
2018-11-04 08:59:09 +00:00
void load() throws MessagingException, IOException {
2019-01-22 09:08:12 +00:00
final State state = currentState;
if (state == null || state.error)
2018-11-14 07:39:17 +01:00
return;
2018-11-04 08:59:09 +00:00
2018-11-14 07:39:17 +01:00
DB db = DB.getInstance(state.context);
2019-01-23 09:31:31 +00:00
final List<EntityFolder> folders = db.folder().getBrowsableFolders(
2019-01-22 16:28:13 +00:00
state.fid < 0 ? null : state.fid, state.search != null);
Log.i("Search fid=" + (state.fid < 0 ? null : state.fid) + " search=" + (state.search == null) + " count=" + folders.size());
2019-01-22 14:29:45 +00:00
if (folders.size() == 0)
2018-12-20 18:18:10 +01:00
return;
2018-11-14 07:39:17 +01:00
if (state.search != null)
2018-11-04 08:59:09 +00:00
try {
db.beginTransaction();
2019-01-22 14:29:45 +00:00
if (state.messages == null) {
2019-01-23 10:57:52 +00:00
List<Long> fids = new ArrayList<>();
2019-01-22 14:29:45 +00:00
for (EntityFolder folder : folders)
2019-01-23 10:57:52 +00:00
fids.add(folder.id);
state.messages = db.message().getMessageByFolders(fids);
2019-01-22 14:29:45 +00:00
Log.i("Messages=" + state.messages.size());
}
2018-11-04 08:59:09 +00:00
int matched = 0;
2018-11-14 07:39:17 +01:00
for (int i = state.local; i < state.messages.size() && matched < state.pageSize; i++) {
state.local = i + 1;
2018-11-04 08:59:09 +00:00
boolean match = false;
2018-11-14 07:39:17 +01:00
String find = state.search.toLowerCase();
EntityMessage message = db.message().getMessage(state.messages.get(i));
String body = null;
if (message.content)
try {
2019-01-21 16:45:05 +00:00
body = Helper.readText(EntityMessage.getFile(state.context, message.id));
} catch (IOException ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
}
2018-11-04 08:59:09 +00:00
if (message.from != null)
for (int j = 0; j < message.from.length && !match; j++)
match = message.from[j].toString().toLowerCase().contains(find);
if (message.to != null)
for (int j = 0; j < message.to.length && !match; j++)
match = message.to[j].toString().toLowerCase().contains(find);
if (message.subject != null && !match)
match = message.subject.toLowerCase().contains(find);
if (!match && message.content)
2018-11-05 18:49:04 +00:00
match = body.toLowerCase().contains(find);
2018-11-04 08:59:09 +00:00
2018-12-06 11:59:57 +01:00
if (match)
db.message().setMessageFound(message.account, message.thread);
2018-11-04 08:59:09 +00:00
}
db.setTransactionSuccessful();
2018-11-14 07:39:17 +01:00
if (++matched >= state.pageSize)
2018-11-04 08:59:09 +00:00
return;
} finally {
db.endTransaction();
}
2018-10-21 06:39:38 +00:00
2019-01-22 14:29:45 +00:00
if (folders.size() > 1)
return;
2018-11-04 08:59:09 +00:00
2019-01-22 14:29:45 +00:00
final EntityFolder folder = folders.get(0);
if (state.imessages == null) {
2018-11-04 08:59:09 +00:00
EntityAccount account = db.account().getAccount(folder.account);
2018-12-13 09:48:39 +01:00
try {
2019-01-10 18:24:20 +00:00
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.realm, account.insecure);
2018-12-13 09:48:39 +01:00
Session isession = Session.getInstance(props, null);
2019-01-22 09:08:12 +00:00
isession.setDebug(true);
2018-12-13 09:48:39 +01:00
2018-12-24 12:27:45 +00:00
Log.i("Boundary connecting account=" + account.name);
2018-12-13 09:48:39 +01:00
state.istore = (IMAPStore) isession.getStore(account.starttls ? "imap" : "imaps");
Helper.connect(state.context, state.istore, account);
2018-12-24 12:27:45 +00:00
Log.i("Boundary opening folder=" + folder.name);
2018-12-13 09:48:39 +01:00
state.ifolder = (IMAPFolder) state.istore.getFolder(folder.name);
state.ifolder.open(Folder.READ_WRITE);
2018-12-24 12:27:45 +00:00
Log.i("Boundary searching=" + state.search);
2018-12-13 09:48:39 +01:00
if (state.search == null)
state.imessages = state.ifolder.getMessages();
2018-12-27 10:36:42 +00:00
else {
Object result = state.ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
@Override
public Object doCommand(IMAPProtocol protocol) {
try {
if (protocol.supportsUtf8()) {
2019-01-22 13:31:27 +00:00
// SEARCH OR OR FROM "x" TO "x" OR SUBJECT "x" BODY "x" ALL
// SEARCH OR OR OR FROM "x" TO "x" OR SUBJECT "x" BODY "x" (KEYWORD x) ALL
Argument arg = new Argument();
if (folder.keywords.length > 0)
arg.writeAtom("OR");
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("SUBJECT");
arg.writeBytes(state.search.getBytes());
arg.writeAtom("BODY");
arg.writeBytes(state.search.getBytes());
if (folder.keywords.length > 0) {
arg.writeAtom("KEYWORD");
arg.writeBytes(state.search.getBytes());
}
arg.writeAtom("ALL");
Response[] responses = protocol.command("SEARCH", arg);
2019-01-22 13:31:27 +00:00
int msgnum;
List<Integer> msgnums = new ArrayList<>();
for (int i = 0; i < responses.length; i++) {
if (responses[i] instanceof IMAPResponse) {
IMAPResponse ir = (IMAPResponse) responses[i];
if (ir.keyEquals("SEARCH")) {
while ((msgnum = ir.readNumber()) != -1)
msgnums.add(msgnum);
}
} else {
if (responses[i].isOK())
Log.i(folder.name + " response=" + responses[i]);
else
throw new MessagingException(responses[i].toString());
}
}
Message[] imessages = new Message[msgnums.size()];
for (int i = 0; i < msgnums.size(); i++)
imessages[i] = state.ifolder.getMessage(msgnums.get(i));
return imessages;
} else {
SearchTerm term = new OrTerm(
new OrTerm(
new FromStringTerm(state.search),
new RecipientStringTerm(Message.RecipientType.TO, state.search)
),
new OrTerm(
new SubjectTerm(state.search),
new BodyTerm(state.search)
)
);
if (folder.keywords.length > 0)
term = new OrTerm(term, new FlagTerm(
new Flags(Helper.sanitizeKeyword(state.search)), true));
return state.ifolder.search(term);
2019-01-22 13:31:27 +00:00
}
} catch (MessagingException ex) {
Log.e(ex);
return ex;
2019-01-22 13:31:27 +00:00
}
}
});
2019-01-22 13:31:27 +00:00
if (result instanceof MessagingException)
throw (MessagingException) result;
2019-01-22 13:31:27 +00:00
state.imessages = (Message[]) result;
2018-12-27 10:36:42 +00:00
}
2018-12-24 12:27:45 +00:00
Log.i("Boundary found messages=" + state.imessages.length);
2018-12-13 09:48:39 +01:00
state.index = state.imessages.length - 1;
} catch (Throwable ex) {
2019-01-22 09:08:12 +00:00
state.error = true;
2018-12-13 09:48:39 +01:00
if (ex instanceof FolderClosedException)
2018-12-24 12:27:45 +00:00
Log.w("Search", ex);
2018-12-13 09:48:39 +01:00
else {
2018-12-24 12:27:45 +00:00
Log.e("Search", ex);
2018-12-13 09:48:39 +01:00
throw ex;
}
}
2018-10-21 06:39:38 +00:00
}
2018-10-20 16:05:43 +00:00
2018-10-21 06:39:38 +00:00
int count = 0;
2018-11-14 07:39:17 +01:00
while (state.index >= 0 && count < state.pageSize && currentState != null) {
2018-12-24 12:27:45 +00:00
Log.i("Boundary index=" + state.index);
2018-11-14 07:39:17 +01:00
int from = Math.max(0, state.index - (state.pageSize - count) + 1);
Message[] isub = Arrays.copyOfRange(state.imessages, from, state.index + 1);
state.index -= (state.pageSize - count);
2018-10-21 06:39:38 +00:00
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
fp.add(UIDFolder.FetchProfileItem.UID);
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
fp.add(FetchProfile.Item.SIZE);
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
2018-11-14 07:39:17 +01:00
state.ifolder.fetch(isub, fp);
2018-10-21 06:39:38 +00:00
try {
db.beginTransaction();
for (int j = isub.length - 1; j >= 0; j--)
try {
2018-11-14 07:39:17 +01:00
long uid = state.ifolder.getUID(isub[j]);
2018-12-24 12:27:45 +00:00
Log.i("Boundary sync uid=" + uid);
2019-01-22 14:29:45 +00:00
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
2018-10-21 06:39:38 +00:00
if (message == null) {
2018-12-28 08:21:31 +00:00
message = ServiceSynchronize.synchronizeMessage(state.context,
2019-01-18 08:23:53 +00:00
folder, state.ifolder, (IMAPMessage) isub[j],
true,
new ArrayList<EntityRule>());
2018-10-20 17:56:09 +00:00
count++;
2018-10-20 16:05:43 +00:00
}
2018-12-06 11:59:57 +01:00
db.message().setMessageFound(message.account, message.thread);
2018-12-21 11:17:18 +01:00
} catch (MessageRemovedException ex) {
2018-12-24 12:27:45 +00:00
Log.w(folder.name + " boundary", ex);
2018-12-21 11:17:18 +01:00
} catch (FolderClosedException ex) {
throw ex;
} catch (IOException ex) {
if (ex.getCause() instanceof MessagingException) {
2018-12-24 12:27:45 +00:00
Log.w(folder.name + " boundary", ex);
if (!(ex.getCause() instanceof MessageRemovedException))
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
} else
throw ex;
2018-10-21 06:39:38 +00:00
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(folder.name + " boundary", ex);
2018-12-21 11:17:18 +01:00
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
2018-10-21 06:39:38 +00:00
} finally {
((IMAPMessage) isub[j]).invalidateHeaders();
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2018-10-20 16:05:43 +00:00
}
}
2018-10-21 06:39:38 +00:00
2018-12-24 12:27:45 +00:00
Log.i("Boundary done");
2018-10-20 16:05:43 +00:00
}
void clear() {
2018-11-14 07:39:17 +01:00
State state = currentState;
if (state == null)
return;
currentState = null;
2018-12-24 12:27:45 +00:00
Log.i("Boundary clear");
2018-10-20 16:05:43 +00:00
try {
2018-11-14 07:39:17 +01:00
if (state.istore != null)
state.istore.close();
2018-10-20 16:05:43 +00:00
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e("Boundary", ex);
2018-10-20 16:05:43 +00:00
} finally {
2018-11-14 07:39:17 +01:00
state.context = null;
state.messages = null;
state.istore = null;
state.ifolder = null;
state.imessages = null;
2018-10-20 16:05:43 +00:00
}
}
}