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

323 lines
12 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
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 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-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 17:44:45 +00: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;
2019-01-08 07:13:44 +00:00
import java.util.Calendar;
import java.util.Date;
2019-09-06 14:45:37 +00:00
import java.util.HashMap;
import java.util.Map;
2019-02-26 10:05:21 +00: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;
2019-05-11 19:04:27 +00:00
public String state;
2018-12-01 14:13:57 +00:00
public String error;
2018-08-02 13:33:06 +00:00
2018-12-09 14:49:43 +00:00
static final String ADD = "add";
static final String MOVE = "move";
2019-03-02 14:32:19 +00:00
static final String COPY = "copy";
2018-12-09 14:49:43 +00:00
static final String DELETE = "delete";
static final String SEND = "send";
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 14:49:43 +00:00
static final String BODY = "body";
static final String ATTACHMENT = "attachment";
static final String SYNC = "sync";
2019-04-25 16:47:52 +00:00
static final String SUBSCRIBE = "subscribe";
2019-05-18 06:49:20 +00:00
static void queue(Context context, EntityMessage message, String name, Object... values) {
DB db = DB.getInstance(context);
2018-12-01 13:02:27 +00:00
JSONArray jargs = new JSONArray();
2019-03-02 07:36:57 +00:00
for (Object value : values)
jargs.put(value);
2018-12-02 13:19:54 +00:00
2019-01-23 18:52:52 +00:00
long folder = message.folder;
2018-12-10 17:44:45 +00:00
try {
if (SEEN.equals(name)) {
2019-09-10 16:05:27 +00:00
boolean seen = jargs.getBoolean(0);
2019-06-23 08:18:43 +00:00
boolean ignore = jargs.optBoolean(1, true);
2018-12-10 17:44:45 +00:00
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid)) {
2019-09-10 16:05:27 +00:00
db.message().setMessageUiSeen(similar.id, seen);
2019-06-23 08:18:43 +00:00
db.message().setMessageUiIgnored(similar.id, ignore);
2018-12-10 17:44:45 +00:00
}
2019-05-15 09:10:47 +00: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().getMessageByMsgId(message.account, message.msgid)) {
db.message().setMessageUiFlagged(similar.id, flagged);
db.message().setMessageColor(similar.id, flagged ? color : null);
}
2018-12-10 17:44:45 +00:00
2019-05-15 09:10:47 +00:00
} else if (ANSWERED.equals(name))
2018-12-10 17:44:45 +00:00
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid))
db.message().setMessageUiAnswered(similar.id, jargs.getBoolean(0));
2018-12-14 19:15:07 +00:00
else if (MOVE.equals(name)) {
2019-01-24 07:13:54 +00:00
// Parameters:
2019-06-14 06:31:37 +00:00
// 0: target folder
// 1: auto read
2019-01-24 07:13:54 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean autoread = prefs.getBoolean("autoread", false);
2019-06-23 08:28:33 +00:00
autoread = (autoread && jargs.optBoolean(1, true));
2019-01-24 07:13:54 +00:00
jargs.put(1, autoread);
2018-12-14 19:15:07 +00: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 14:21:06 +00:00
EntityLog.log(context, "Move message=" + message.id + ":" + message.subject +
" source=" + source.id + ":" + source.name + "" +
" target=" + target.id + ":" + target.name +
" autoread=" + autoread);
2019-07-25 08:14:32 +00:00
if (!EntityFolder.ARCHIVE.equals(source.type) ||
EntityFolder.TRASH.equals(target.type) || EntityFolder.JUNK.equals(target.type))
2019-06-23 11:23:29 +00:00
db.message().setMessageUiHide(message.id, new Date().getTime());
2018-12-10 17:44:45 +00: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-08 07:13:44 +00:00
Calendar cal_keep = Calendar.getInstance();
cal_keep.add(Calendar.DAY_OF_MONTH, -target.keep_days);
cal_keep.set(Calendar.HOUR_OF_DAY, 0);
cal_keep.set(Calendar.MINUTE, 0);
cal_keep.set(Calendar.SECOND, 0);
cal_keep.set(Calendar.MILLISECOND, 0);
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 &&
target.synchronize &&
2019-01-08 07:13:44 +00:00
message.received > cal_keep.getTimeInMillis() &&
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 15:53:56 +00:00
Long identity = message.identity;
2019-01-04 18:37:56 +00:00
long uid = message.uid;
2019-01-25 12:45:37 +00:00
boolean seen = message.seen;
boolean ui_seen = message.ui_seen;
Long ui_hide = message.ui_hide;
2019-03-18 09:41:46 +00:00
boolean ui_browsed = message.ui_browsed;
2019-06-20 06:39:37 +00:00
String error = message.error;
2019-06-13 15:53:56 +00: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 15:53:56 +00:00
message.identity = null;
2019-01-23 18:52:52 +00:00
message.uid = null;
2019-01-25 12:45:37 +00:00
if (autoread) {
message.seen = true;
message.ui_seen = true;
}
message.ui_hide = 0L;
2019-03-18 09:41:46 +00:00
message.ui_browsed = false;
2019-06-20 06:39:37 +00: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 06:31:37 +00:00
long tmpid = message.id;
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 15:53:56 +00:00
message.identity = identity;
2019-01-23 18:52:52 +00:00
message.uid = uid;
2019-01-25 12:45:37 +00:00
message.seen = seen;
message.ui_seen = ui_seen;
message.ui_hide = ui_hide;
2019-03-18 09:41:46 +00:00
message.ui_browsed = ui_browsed;
2019-06-20 06:39:37 +00: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 18:52:21 +00:00
db.message().setMessageContent(tmpid, false, null, null, null);
2019-04-05 08:27:38 +00:00
db.message().setMessageSize(message.id, 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 06:31:37 +00:00
// Cross account move
if (!source.account.equals(target.account))
2019-09-08 07:03:40 +00:00
if (message.raw != null && message.raw) {
2019-01-25 12:45:37 +00:00
name = ADD;
folder = target.id;
2019-06-14 06:31:37 +00:00
} else
2019-01-25 12:45:37 +00:00
name = RAW;
2019-01-23 18:52:52 +00:00
2018-12-14 19:15:07 +00:00
} else if (DELETE.equals(name))
2019-06-23 11:23:29 +00:00
db.message().setMessageUiHide(message.id, new Date().getTime());
2018-12-10 17:44:45 +00:00
} catch (JSONException ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-12-10 17:44:45 +00:00
}
2019-09-06 14:45:37 +00:00
EntityOperation op = new EntityOperation();
op.account = message.account;
op.folder = folder;
op.message = message.id;
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-02-28 18:13:28 +00:00
if (SEND.equals(name))
ServiceSend.start(context);
else
2019-07-22 11:18:52 +00:00
ServiceSynchronize.process(context, false);
}
2018-08-02 13:33:06 +00:00
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 17:14:24 +00:00
if (folder == null)
return;
2019-03-02 17:46:49 +00:00
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-03-02 07:46:53 +00:00
2019-05-22 06:02:37 +00:00
if (foreground) // Show spinner
db.folder().setFolderSyncState(fid, "requested");
}
2019-03-02 07:46:53 +00:00
2019-05-23 17:14:24 +00:00
if (folder.account == null) // Outbox
ServiceSend.start(context);
else if (foreground)
2019-07-22 11:18:52 +00:00
ServiceSynchronize.process(context, true);
2019-03-02 07:46:53 +00:00
}
2019-04-25 16:47:52 +00: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);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityOperation) {
EntityOperation other = (EntityOperation) obj;
return (this.folder.equals(other.folder) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.message, other.message) &&
this.name.equals(other.name) &&
2018-12-01 13:02:27 +00:00
this.args.equals(other.args) &&
2018-12-01 14:13:57 +00:00
this.created.equals(other.created) &&
2019-05-11 19:04:27 +00:00
Objects.equals(this.state, other.state) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.error, other.error));
} else
return false;
}
2018-08-02 13:33:06 +00:00
}