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

476 lines
18 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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-08-02 13:33:06 +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-08-02 13:33:06 +00:00
2020-01-05 18:32:53 +01:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2019-01-04 18:37:56 +00:00
import android.content.Context;
2019-01-24 07:13:54 +00:00
import android.content.SharedPreferences;
2019-09-27 14:14:04 +02:00
import android.text.TextUtils;
2019-01-04 18:37:56 +00:00
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
2018-08-02 13:33:06 +00:00
import org.json.JSONArray;
2018-12-10 18:44:45 +01:00
import org.json.JSONException;
2018-08-02 13:33:06 +00:00
2019-03-14 07:45:13 +00:00
import java.io.File;
2019-01-04 18:37:56 +00:00
import java.io.IOException;
2020-01-28 14:35:49 +01:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
2019-09-06 16:45:37 +02:00
import java.util.HashMap;
2020-01-28 14:35:49 +01:00
import java.util.List;
2019-09-06 16:45:37 +02:00
import java.util.Map;
2019-02-26 11:05:21 +01:00
import java.util.Objects;
2018-08-08 06:55:47 +00:00
import static androidx.room.ForeignKey.CASCADE;
2018-08-02 13:33:06 +00:00
@Entity(
tableName = EntityOperation.TABLE_NAME,
foreignKeys = {
2018-08-09 20:45:42 +00:00
@ForeignKey(childColumns = "folder", entity = EntityFolder.class, parentColumns = "id", onDelete = CASCADE),
2018-08-08 06:55:47 +00:00
@ForeignKey(childColumns = "message", entity = EntityMessage.class, parentColumns = "id", onDelete = CASCADE)
2018-08-02 13:33:06 +00:00
},
indices = {
@Index(value = {"account"}),
2018-08-10 09:45:36 +00:00
@Index(value = {"folder"}),
2019-03-03 12:59:58 +00:00
@Index(value = {"message"}),
@Index(value = {"name"}),
@Index(value = {"state"})
2018-08-02 13:33:06 +00:00
}
)
public class EntityOperation {
static final String TABLE_NAME = "operation";
@PrimaryKey(autoGenerate = true)
public Long id;
public Long account; // performance
2018-08-02 13:33:06 +00:00
@NonNull
2018-08-09 20:45:42 +00:00
public Long folder;
2018-08-02 13:33:06 +00:00
public Long message;
@NonNull
public String name;
@NonNull
2018-08-02 13:33:06 +00:00
public String args;
@NonNull
public Long created;
@NonNull
public int tries = 0;
2019-05-11 21:04:27 +02:00
public String state;
2018-12-01 15:13:57 +01:00
public String error;
2018-08-02 13:33:06 +00:00
2018-12-09 15:49:43 +01:00
static final String ADD = "add";
static final String MOVE = "move";
2019-03-02 14:32:19 +00:00
static final String COPY = "copy";
2019-09-17 13:38:23 +02:00
static final String FETCH = "fetch";
2019-09-17 16:01:29 +02:00
static final String DELETE = "delete";
2018-12-09 15:49:43 +01:00
static final String SEEN = "seen";
static final String ANSWERED = "answered";
static final String FLAG = "flag";
static final String KEYWORD = "keyword";
static final String HEADERS = "headers";
static final String RAW = "raw";
2018-12-09 15:49:43 +01:00
static final String BODY = "body";
static final String ATTACHMENT = "attachment";
static final String SYNC = "sync";
2019-04-25 18:47:52 +02:00
static final String SUBSCRIBE = "subscribe";
2019-09-17 13:38:23 +02:00
static final String SEND = "send";
static final String EXISTS = "exists";
2019-05-18 08:49:20 +02:00
static void queue(Context context, EntityMessage message, String name, Object... values) {
DB db = DB.getInstance(context);
2018-12-10 18:44:45 +01:00
try {
2019-11-03 15:25:13 +01:00
JSONArray jargs = new JSONArray();
for (Object value : values)
jargs.put(value);
2020-01-15 10:29:16 +01:00
if (ADD.equals(name)) {
if (EntityMessage.PGP_SIGNENCRYPT.equals(message.encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(message.encrypt)) {
EntityFolder folder = db.folder().getFolder(message.folder);
2020-01-15 12:07:45 +01:00
if (folder != null && EntityFolder.DRAFTS.equals(folder.type)) {
WorkerFts.init(context, false);
2020-01-15 10:29:16 +01:00
return;
2020-01-15 12:07:45 +01:00
}
2020-01-15 10:29:16 +01:00
}
}
2019-12-02 08:35:09 +01:00
if (MOVE.equals(name) &&
(EntityMessage.PGP_SIGNENCRYPT.equals(message.encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(message.encrypt))) {
EntityFolder folder = db.folder().getFolder(message.folder);
if (folder != null && EntityFolder.DRAFTS.equals(folder.type))
name = DELETE;
}
2018-12-10 18:44:45 +01:00
if (SEEN.equals(name)) {
2019-09-10 18:05:27 +02:00
boolean seen = jargs.getBoolean(0);
2019-06-23 10:18:43 +02:00
boolean ignore = jargs.optBoolean(1, true);
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid)) {
2019-09-10 18:05:27 +02:00
db.message().setMessageUiSeen(similar.id, seen);
2019-06-23 10:18:43 +02:00
db.message().setMessageUiIgnored(similar.id, ignore);
2019-11-03 15:25:13 +01:00
queue(context, similar.account, similar.folder, similar.id, name, jargs);
2018-12-10 18:44:45 +01:00
}
2019-11-03 15:25:13 +01:00
return;
2018-12-10 18:44:45 +01:00
2019-05-15 11:10:47 +02:00
} else if (FLAG.equals(name)) {
boolean flagged = jargs.getBoolean(0);
Integer color = (jargs.length() > 1 && !jargs.isNull(1) ? jargs.getInt(1) : null);
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid)) {
2019-09-28 20:25:19 +02:00
db.message().setMessageUiFlagged(similar.id, flagged, flagged ? color : null);
2019-11-03 15:25:13 +01:00
queue(context, similar.account, similar.folder, similar.id, name, jargs);
}
return;
2018-12-10 18:44:45 +01:00
2019-11-03 15:25:13 +01:00
} else if (ANSWERED.equals(name)) {
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid)) {
2018-12-10 18:44:45 +01:00
db.message().setMessageUiAnswered(similar.id, jargs.getBoolean(0));
2019-11-03 15:25:13 +01:00
queue(context, similar.account, similar.folder, similar.id, name, jargs);
}
return;
2018-12-10 18:44:45 +01:00
2020-01-28 14:35:49 +01:00
} else if (KEYWORD.equals(name)) {
String keyword = jargs.getString(0);
boolean set = jargs.getBoolean(1);
List<String> keywords = new ArrayList<>(Arrays.asList(message.keywords));
while (keywords.remove(keyword))
;
if (set)
keywords.add(keyword);
Collections.sort(keywords);
db.message().setMessageKeywords(message.id, DB.Converters.fromStringArray(keywords.toArray(new String[0])));
2019-11-03 15:25:13 +01:00
} else if (MOVE.equals(name)) {
2019-01-24 07:13:54 +00:00
// Parameters:
2019-06-14 08:31:37 +02:00
// 0: target folder
// 1: mark seen
2019-09-28 16:16:55 +02:00
// 2: temporary message
// 3: remove flag
2019-01-24 07:13:54 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean autoread = prefs.getBoolean("autoread", false);
2019-10-01 17:31:58 +02:00
boolean autounflag = prefs.getBoolean("autounflag", false);
2020-02-03 19:25:46 +01:00
boolean reset_importance = prefs.getBoolean("reset_importance", false);
2019-01-24 07:13:54 +00:00
2020-01-14 11:34:26 +01:00
if (jargs.opt(1) != null) // rules
autoread = jargs.getBoolean(1);
jargs.put(1, autoread);
jargs.put(3, autounflag);
2019-10-02 09:58:54 +02:00
2018-12-14 20:15:07 +01:00
EntityFolder source = db.folder().getFolder(message.folder);
2019-01-04 18:37:56 +00:00
EntityFolder target = db.folder().getFolder(jargs.getLong(0));
2019-01-28 16:57:30 +00:00
if (source.id.equals(target.id))
return;
2019-01-04 18:37:56 +00:00
2019-08-20 16:21:06 +02:00
EntityLog.log(context, "Move message=" + message.id + ":" + message.subject +
" source=" + source.id + ":" + source.name + "" +
" target=" + target.id + ":" + target.name +
2019-10-01 17:31:58 +02:00
" auto read=" + autoread + " flag=" + autounflag);
2019-08-20 16:21:06 +02:00
2020-02-03 19:25:46 +01:00
if (autoread || autounflag || reset_importance)
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid)) {
2019-10-01 18:00:06 +02:00
if (autoread)
db.message().setMessageUiSeen(similar.id, true);
if (autounflag)
db.message().setMessageUiFlagged(similar.id, false, null);
2020-02-03 19:25:46 +01:00
if (reset_importance)
db.message().setMessageImportance(similar.id, null);
2019-10-01 18:00:06 +02:00
}
2019-09-17 11:08:17 +02:00
2019-10-01 17:31:58 +02:00
2019-07-25 10:14:32 +02:00
if (!EntityFolder.ARCHIVE.equals(source.type) ||
EntityFolder.TRASH.equals(target.type) || EntityFolder.JUNK.equals(target.type))
2019-09-27 18:25:55 +02:00
db.message().setMessageUiHide(message.id, true);
2018-12-10 18:44:45 +01:00
if (message.ui_snoozed != null &&
(EntityFolder.ARCHIVE.equals(target.type) || EntityFolder.TRASH.equals(target.type))) {
message.ui_snoozed = null;
EntityMessage.snooze(context, message.id, null);
}
2019-01-04 18:37:56 +00:00
// Create copy without uid in target folder
2019-01-12 09:18:37 +00:00
// Message with same msgid can be in archive
2019-01-12 08:06:36 +00:00
if (message.uid != null &&
2019-09-27 14:14:04 +02:00
!TextUtils.isEmpty(message.msgid) &&
2019-01-08 07:13:44 +00:00
db.message().countMessageByMsgId(target.id, message.msgid) == 0) {
2019-03-14 07:45:13 +00:00
File msource = message.getFile(context);
2019-03-07 08:45:10 +00:00
// Copy message to target folder
2019-01-04 18:37:56 +00:00
long id = message.id;
2019-06-13 17:53:56 +02:00
Long identity = message.identity;
2019-01-04 18:37:56 +00:00
long uid = message.uid;
2020-02-06 20:12:38 +01:00
Boolean raw = message.raw;
2019-09-27 12:08:25 +02:00
int notifying = message.notifying;
2020-01-15 10:29:16 +01:00
boolean fts = message.fts;
2020-02-03 19:25:46 +01:00
Integer importance = message.importance;
2019-01-25 12:45:37 +00:00
boolean seen = message.seen;
2020-02-03 19:25:46 +01:00
boolean flagged = message.flagged;
2019-01-25 12:45:37 +00:00
boolean ui_seen = message.ui_seen;
2020-02-03 19:25:46 +01:00
boolean ui_flagged = message.ui_flagged;
2019-09-27 18:25:55 +02:00
Boolean ui_hide = message.ui_hide;
2019-03-18 09:41:46 +00:00
boolean ui_browsed = message.ui_browsed;
2020-02-05 09:59:09 +01:00
Integer color = message.color;
2019-06-20 08:39:37 +02:00
String error = message.error;
2019-06-13 17:53:56 +02:00
2019-01-04 18:37:56 +00:00
message.id = null;
2019-01-23 18:52:52 +00:00
message.account = target.account;
2019-01-04 18:37:56 +00:00
message.folder = target.id;
2019-06-13 17:53:56 +02:00
message.identity = null;
2019-01-23 18:52:52 +00:00
message.uid = null;
2020-02-06 20:12:38 +01:00
message.raw = null;
2019-09-27 12:08:25 +02:00
message.notifying = 0;
2020-01-15 10:29:16 +01:00
message.fts = false;
2020-02-03 19:25:46 +01:00
if (reset_importance)
message.importance = null;
2019-01-25 12:45:37 +00:00
if (autoread) {
message.seen = true;
message.ui_seen = true;
}
2020-02-03 19:25:46 +01:00
if (autounflag) {
message.flagged = false;
message.ui_flagged = false;
2020-02-05 09:59:09 +01:00
message.color = null;
2020-02-03 19:25:46 +01:00
}
2019-09-27 18:25:55 +02:00
message.ui_hide = false;
2019-03-18 09:41:46 +00:00
message.ui_browsed = false;
2019-06-20 08:39:37 +02:00
message.error = null;
2019-03-14 07:45:13 +00:00
message.id = db.message().insertMessage(message);
File mtarget = message.getFile(context);
2019-06-14 08:31:37 +02:00
long tmpid = message.id;
2019-09-28 16:16:55 +02:00
jargs.put(2, tmpid);
2019-03-07 08:45:10 +00:00
2019-01-04 18:37:56 +00:00
message.id = id;
2019-01-23 18:52:52 +00:00
message.account = source.account;
2019-01-04 18:37:56 +00:00
message.folder = source.id;
2019-06-13 17:53:56 +02:00
message.identity = identity;
2019-01-23 18:52:52 +00:00
message.uid = uid;
2020-02-06 20:12:38 +01:00
message.raw = raw;
2019-09-27 12:08:25 +02:00
message.notifying = notifying;
2020-01-15 10:29:16 +01:00
message.fts = fts;
2020-02-03 19:25:46 +01:00
message.importance = importance;
2019-01-25 12:45:37 +00:00
message.seen = seen;
2020-02-03 19:25:46 +01:00
message.flagged = flagged;
2019-01-25 12:45:37 +00:00
message.ui_seen = ui_seen;
2020-02-03 19:25:46 +01:00
message.ui_flagged = ui_flagged;
message.ui_hide = ui_hide;
2019-03-18 09:41:46 +00:00
message.ui_browsed = ui_browsed;
2020-02-05 09:59:09 +01:00
message.color = color;
2019-06-20 08:39:37 +02:00
message.error = error;
2019-01-19 18:13:48 +00:00
2019-01-04 18:37:56 +00:00
if (message.content)
try {
2019-03-14 07:45:13 +00:00
Helper.copy(msource, mtarget);
2019-01-04 18:37:56 +00:00
} catch (IOException ex) {
Log.e(ex);
2019-05-04 20:52:21 +02:00
db.message().setMessageContent(tmpid, false, null, null, null);
2019-09-30 21:00:28 +02:00
db.message().setMessageSize(message.id, null, null);
2019-01-04 18:37:56 +00:00
}
2019-03-14 07:18:42 +00:00
EntityAttachment.copy(context, message.id, tmpid);
2019-01-04 18:37:56 +00:00
}
2019-06-14 08:31:37 +02:00
// Cross account move
2020-02-06 20:08:34 +01:00
if (source.account.equals(target.account))
queue(context, message.account, source.id, message.id, name, jargs);
else {
if (message.raw != null && message.raw)
queue(context, target.account, target.id, message.id, ADD, jargs);
else
queue(context, source.account, source.id, message.id, RAW, jargs);
}
2019-11-03 15:25:13 +01:00
return;
2019-01-23 18:52:52 +00:00
2018-12-14 20:15:07 +01:00
} else if (DELETE.equals(name))
2019-09-27 18:25:55 +02:00
db.message().setMessageUiHide(message.id, true);
2019-10-05 12:42:04 +02:00
else if (ATTACHMENT.equals(name))
2020-01-04 20:02:29 +01:00
db.attachment().setProgress(jargs.getLong(0), 0);
2019-10-05 12:42:04 +02:00
2019-11-03 15:25:13 +01:00
queue(context, message.account, message.folder, message.id, name, jargs);
2018-12-10 18:44:45 +01:00
} catch (JSONException ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-12-10 18:44:45 +01:00
}
2019-11-03 15:25:13 +01:00
}
2019-11-11 10:53:03 +01:00
private static void queue(Context context, Long account, long folder, long message, String name, JSONArray jargs) {
2019-11-03 15:25:13 +01:00
DB db = DB.getInstance(context);
2018-12-10 18:44:45 +01:00
2019-09-06 16:45:37 +02:00
EntityOperation op = new EntityOperation();
2019-11-03 15:25:13 +01:00
op.account = account;
2019-09-06 16:45:37 +02:00
op.folder = folder;
2019-11-03 15:25:13 +01:00
op.message = message;
2019-09-06 16:45:37 +02:00
op.name = name;
op.args = jargs.toString();
op.created = new Date().getTime();
op.id = db.operation().insertOperation(op);
Log.i("Queued op=" + op.id + "/" + op.name +
" folder=" + op.folder + " msg=" + op.message +
" args=" + op.args);
Map<String, String> crumb = new HashMap<>();
crumb.put("name", op.name);
crumb.put("args", op.args);
crumb.put("folder", op.account + ":" + op.folder);
if (op.message != null)
crumb.put("message", Long.toString(op.message));
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
Log.breadcrumb("queued", crumb);
}
2018-08-02 13:33:06 +00:00
2019-09-17 13:38:23 +02:00
static void queue(Context context, EntityFolder folder, String name, Object... values) {
DB db = DB.getInstance(context);
JSONArray jargs = new JSONArray();
for (Object value : values)
jargs.put(value);
EntityOperation op = new EntityOperation();
op.account = folder.account;
op.folder = folder.id;
op.message = null;
op.name = name;
op.args = jargs.toString();
op.created = new Date().getTime();
op.id = db.operation().insertOperation(op);
Log.i("Queued op=" + op.id + "/" + op.name +
" folder=" + op.folder + " msg=" + op.message +
" args=" + op.args);
Map<String, String> crumb = new HashMap<>();
crumb.put("name", op.name);
crumb.put("args", op.args);
crumb.put("folder", op.account + ":" + op.folder);
if (op.message != null)
crumb.put("message", Long.toString(op.message));
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
Log.breadcrumb("queued", crumb);
}
2019-03-02 07:46:53 +00:00
static void sync(Context context, long fid, boolean foreground) {
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(fid);
2019-05-23 19:14:24 +02:00
if (folder == null)
return;
2019-03-02 17:46:49 +00:00
// TODO: replace sync parameters?
if (db.operation().getOperationCount(fid, EntityOperation.SYNC) == 0) {
2019-03-02 07:46:53 +00:00
EntityOperation operation = new EntityOperation();
operation.account = folder.account;
2019-03-02 07:46:53 +00:00
operation.folder = folder.id;
operation.message = null;
operation.name = SYNC;
operation.args = folder.getSyncArgs().toString();
2019-03-02 07:46:53 +00:00
operation.created = new Date().getTime();
operation.id = db.operation().insertOperation(operation);
Log.i("Queued sync folder=" + folder);
2019-05-22 08:02:37 +02:00
}
2019-03-02 07:46:53 +00:00
2020-01-30 13:23:30 +01:00
if (foreground) // Show spinner
db.folder().setFolderSyncState(fid, "requested");
2019-05-23 19:14:24 +02:00
if (folder.account == null) // Outbox
ServiceSend.start(context);
2019-03-02 07:46:53 +00:00
}
2019-04-25 18:47:52 +02:00
static void subscribe(Context context, long fid, boolean subscribe) {
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(fid);
JSONArray jargs = new JSONArray();
jargs.put(subscribe);
EntityOperation operation = new EntityOperation();
operation.account = folder.account;
operation.folder = folder.id;
operation.message = null;
operation.name = SUBSCRIBE;
operation.args = jargs.toString();
operation.created = new Date().getTime();
operation.id = db.operation().insertOperation(operation);
Log.i("Queued subscribe=" + subscribe + " folder=" + folder);
}
2019-12-07 20:32:58 +01:00
void cleanup(Context context) {
DB db = DB.getInstance(context);
if (message != null)
db.message().setMessageUiHide(message, false);
if (EntityOperation.MOVE.equals(name) ||
EntityOperation.ADD.equals(name) ||
EntityOperation.RAW.equals(name))
try {
JSONArray jargs = new JSONArray(args);
long tmpid = jargs.optLong(2, -1);
if (tmpid < 0)
return;
db.message().deleteMessage(tmpid);
} catch (JSONException ex) {
Log.e(ex);
}
2020-02-08 12:26:49 +01:00
2020-02-08 12:49:12 +01:00
if (EntityOperation.SYNC.equals(name))
db.folder().setFolderSyncState(folder, null);
2020-02-08 12:26:49 +01:00
if (message != null) {
EntityMessage m = db.message().getMessage(message);
if (m == null || m.uid == null)
return;
EntityFolder f = db.folder().getFolder(folder);
if (f == null)
return;
queue(context, f, FETCH, m.uid);
}
2019-12-07 20:32:58 +01:00
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityOperation) {
EntityOperation other = (EntityOperation) obj;
return (this.folder.equals(other.folder) &&
2019-02-26 11:05:21 +01:00
Objects.equals(this.message, other.message) &&
this.name.equals(other.name) &&
2018-12-01 14:02:27 +01:00
this.args.equals(other.args) &&
2018-12-01 15:13:57 +01:00
this.created.equals(other.created) &&
2019-05-11 21:04:27 +02:00
Objects.equals(this.state, other.state) &&
2019-02-26 11:05:21 +01:00
Objects.equals(this.error, other.error));
} else
return false;
}
2018-08-02 13:33:06 +00:00
}