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

246 lines
9.2 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
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-10-20 16:05:43 +00:00
import android.content.Context;
import android.util.Log;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.util.FolderClosedIOException;
2018-10-20 16:05:43 +00:00
2018-11-04 08:59:09 +00:00
import java.io.IOException;
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;
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;
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;
2018-10-21 14:07:45 +00:00
}
2018-11-04 08:59:09 +00:00
void load() throws MessagingException, IOException {
2018-11-14 07:39:17 +01:00
State state = currentState;
if (state == null)
return;
2018-11-04 08:59:09 +00:00
2018-11-14 07:39:17 +01:00
DB db = DB.getInstance(state.context);
EntityFolder folder = db.folder().getFolder(state.fid);
if (state.search != null)
2018-11-04 08:59:09 +00:00
try {
db.beginTransaction();
2018-11-14 07:39:17 +01:00
if (state.messages == null)
2018-12-06 11:59:57 +01:00
state.messages = db.message().getMessageByFolder(state.fid);
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 = (message.content ? message.read(state.context) : null);
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
2018-11-14 07:39:17 +01:00
if (state.imessages == null) {
2018-11-04 08:59:09 +00:00
if (folder.account == null) // outbox
return;
EntityAccount account = db.account().getAccount(folder.account);
2018-10-23 07:52:20 +00:00
Properties props = MessageHelper.getSessionProperties(account.auth_type, account.insecure);
2018-10-21 06:39:38 +00:00
props.setProperty("mail.imap.throwsearchexception", "true");
Session isession = Session.getInstance(props, null);
Log.i(Helper.TAG, "Boundary connecting account=" + account.name);
2018-11-14 07:39:17 +01:00
state.istore = (IMAPStore) isession.getStore(account.starttls ? "imap" : "imaps");
Helper.connect(state.context, state.istore, account);
2018-10-21 06:39:38 +00:00
Log.i(Helper.TAG, "Boundary opening folder=" + folder.name);
2018-11-14 07:39:17 +01:00
state.ifolder = (IMAPFolder) state.istore.getFolder(folder.name);
state.ifolder.open(Folder.READ_WRITE);
2018-10-21 06:39:38 +00:00
2018-11-14 07:39:17 +01:00
Log.i(Helper.TAG, "Boundary searching=" + state.search);
if (state.search == null)
state.imessages = state.ifolder.getMessages();
2018-10-21 06:39:38 +00:00
else
2018-11-14 07:39:17 +01:00
state.imessages = state.ifolder.search(
2018-10-21 06:39:38 +00:00
new OrTerm(
new OrTerm(
2018-11-26 12:46:02 +01:00
new OrTerm(
new FromStringTerm(state.search),
new RecipientStringTerm(Message.RecipientType.TO, state.search)
),
new OrTerm(
new SubjectTerm(state.search),
new BodyTerm(state.search)
)
2018-10-21 06:39:38 +00:00
),
2018-11-26 12:46:02 +01:00
new FlagTerm(new Flags(Helper.sanitizeKeyword(state.search)), true)
2018-10-21 06:39:38 +00:00
)
);
2018-11-14 07:39:17 +01:00
Log.i(Helper.TAG, "Boundary found messages=" + state.imessages.length);
2018-10-21 06:39:38 +00:00
2018-11-14 07:39:17 +01:00
state.index = state.imessages.length - 1;
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) {
Log.i(Helper.TAG, "Boundary index=" + state.index);
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-10-21 06:39:38 +00:00
Log.i(Helper.TAG, "Boundary sync uid=" + uid);
2018-12-06 11:59:57 +01:00
EntityMessage message = db.message().getMessageByUid(state.fid, uid);
2018-10-21 06:39:38 +00:00
if (message == null) {
2018-12-06 11:59:57 +01:00
message = ServiceSynchronize.synchronizeMessage(
2018-11-26 16:57:00 +01:00
state.context,
2018-12-06 11:59:57 +01:00
folder, state.ifolder, (IMAPMessage) isub[j], true, false);
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-10-21 06:39:38 +00:00
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex));
} catch (FolderClosedException ex) {
throw ex;
} catch (FolderClosedIOException ex) {
throw ex;
2018-10-21 06:39:38 +00:00
} catch (Throwable ex) {
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex));
} 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
Log.i(Helper.TAG, "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-10-20 17:56:09 +00:00
Log.i(Helper.TAG, "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) {
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex));
} 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
}
}
}