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

277 lines
11 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;
import android.preference.PreferenceManager;
2019-01-04 18:37:56 +00:00
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-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-02-26 10:05:21 +00:00
import java.util.Objects;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
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 = {
2018-08-10 09:45:36 +00:00
@Index(value = {"folder"}),
2018-08-02 13:33:06 +00:00
@Index(value = {"message"})
}
)
public class EntityOperation {
static final String TABLE_NAME = "operation";
@PrimaryKey(autoGenerate = true)
public Long id;
@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;
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";
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-01-04 18:37:56 +00:00
static void queue(Context context, DB db, EntityMessage message, String name) {
2018-12-01 13:02:27 +00:00
JSONArray jargs = new JSONArray();
2019-01-04 18:37:56 +00:00
queue(context, db, message, name, jargs);
2018-08-02 13:33:06 +00:00
}
2019-01-04 18:37:56 +00:00
static void queue(Context context, DB db, EntityMessage message, String name, Object value) {
2018-12-01 13:02:27 +00:00
JSONArray jargs = new JSONArray();
jargs.put(value);
2019-01-04 18:37:56 +00:00
queue(context, db, message, name, jargs);
2018-08-02 13:33:06 +00:00
}
2019-01-04 18:37:56 +00:00
static void queue(Context context, DB db, EntityMessage message, String name, Object value1, Object value2) {
2018-12-01 13:02:27 +00:00
JSONArray jargs = new JSONArray();
jargs.put(value1);
jargs.put(value2);
2019-01-04 18:37:56 +00:00
queue(context, db, message, name, jargs);
2018-08-09 20:45:42 +00:00
}
2018-08-02 13:33:06 +00:00
2019-02-28 18:13:28 +00:00
static void sync(Context context, long fid) {
DB db = DB.getInstance(context);
if (db.operation().getOperationCount(fid, EntityOperation.SYNC) == 0) {
2019-01-01 18:49:21 +00:00
EntityFolder folder = db.folder().getFolder(fid);
2019-02-28 18:13:28 +00:00
EntityAccount account = null;
if (folder.account != null)
account = db.account().getAccount(folder.account);
2019-01-01 18:49:21 +00:00
2018-12-10 17:44:45 +00:00
EntityOperation operation = new EntityOperation();
operation.folder = folder.id;
2018-12-10 17:44:45 +00:00
operation.message = null;
operation.name = SYNC;
operation.args = folder.getSyncArgs().toString();
2018-12-10 17:44:45 +00:00
operation.created = new Date().getTime();
operation.id = db.operation().insertOperation(operation);
2019-03-01 14:32:18 +00:00
if (account != null && !"connected".equals(account.state)) {
2019-02-28 18:13:28 +00:00
db.folder().setFolderState(fid, "waiting");
db.folder().setFolderSyncState(fid, "manual");
} else
db.folder().setFolderSyncState(fid, "requested");
2018-12-10 17:44:45 +00:00
2019-02-28 18:13:28 +00:00
if (account == null) // Outbox
2019-02-28 07:11:46 +00:00
ServiceSend.start(context);
2019-03-01 14:32:18 +00:00
else if (!"connected".equals(account.state))
2019-02-28 18:13:28 +00:00
ServiceUI.process(context, fid);
2019-02-27 14:06:10 +00:00
2018-12-24 12:27:45 +00:00
Log.i("Queued sync folder=" + folder);
}
2018-12-02 13:19:54 +00:00
}
2019-01-04 18:37:56 +00:00
private static void queue(Context context, DB db, EntityMessage message, String name, JSONArray jargs) {
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)) {
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid)) {
db.message().setMessageUiSeen(similar.id, jargs.getBoolean(0));
2019-01-10 18:32:53 +00:00
db.message().setMessageUiIgnored(similar.id, true);
2018-12-10 17:44:45 +00:00
}
} else if (FLAG.equals(name))
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid))
db.message().setMessageUiFlagged(similar.id, jargs.getBoolean(0));
else if (ANSWERED.equals(name))
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:
// 0: target folder id
// 1: allow auto read
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean autoread = prefs.getBoolean("autoread", false);
if (jargs.length() > 1)
autoread = (autoread && jargs.getBoolean(1));
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
if (!EntityFolder.ARCHIVE.equals(source.type) || EntityFolder.TRASH.equals(target.type))
2018-12-14 19:15:07 +00:00
db.message().setMessageUiHide(message.id, true);
2018-12-10 17:44:45 +00:00
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
Long newid = null;
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-01-04 18:37:56 +00:00
long id = message.id;
long uid = message.uid;
2019-01-25 12:45:37 +00:00
boolean seen = message.seen;
boolean ui_seen = message.ui_seen;
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-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;
}
newid = db.message().insertMessage(message);
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-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;
2019-01-19 18:13:48 +00:00
2019-01-04 18:37:56 +00:00
if (message.content)
try {
Helper.copy(
EntityMessage.getFile(context, id),
EntityMessage.getFile(context, newid));
} catch (IOException ex) {
Log.e(ex);
db.message().setMessageContent(newid, false, null);
}
2019-01-22 18:02:30 +00:00
EntityAttachment.copy(context, db, message.id, newid);
// Store new id for when source message was deleted
2019-01-24 07:13:54 +00:00
jargs.put(2, newid);
2019-01-04 18:37:56 +00:00
}
2019-01-23 18:52:52 +00:00
// Cross account move
2019-01-25 12:45:37 +00:00
if (!source.account.equals(target.account))
if (message.raw != null && message.raw) {
name = ADD;
folder = target.id;
jargs = new JSONArray();
jargs.put(0, newid); // Can be null
jargs.put(1, autoread);
} else {
name = RAW;
jargs = new JSONArray();
jargs.put(0, newid); // Can be null
jargs.put(1, autoread);
jargs.put(2, target.id);
}
2019-01-23 18:52:52 +00:00
2018-12-14 19:15:07 +00:00
} else if (DELETE.equals(name))
2018-12-10 17:44:45 +00:00
db.message().setMessageUiHide(message.id, true);
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
}
2018-08-02 13:33:06 +00:00
EntityOperation operation = new EntityOperation();
2019-01-23 18:52:52 +00:00
operation.folder = folder;
2018-12-10 17:44:45 +00:00
operation.message = message.id;
2018-08-02 13:33:06 +00:00
operation.name = name;
2018-12-01 13:02:27 +00:00
operation.args = jargs.toString();
operation.created = new Date().getTime();
2018-08-09 20:45:42 +00:00
operation.id = db.operation().insertOperation(operation);
2018-08-02 13:33:06 +00:00
2018-12-24 12:27:45 +00:00
Log.i("Queued op=" + operation.id + "/" + operation.name +
2018-12-02 13:19:54 +00:00
" msg=" + operation.folder + "/" + operation.message +
2018-08-09 20:45:42 +00:00
" args=" + operation.args);
2019-02-28 18:13:28 +00:00
if (SEND.equals(name))
ServiceSend.start(context);
else {
EntityAccount account = db.account().getAccount(message.account);
2019-03-01 14:32:18 +00:00
if (account != null && !"connected".equals(account.state))
2019-02-28 18:13:28 +00:00
ServiceUI.process(context, operation.folder);
}
}
2018-08-02 13:33:06 +00:00
@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-02-26 10:05:21 +00:00
Objects.equals(this.error, other.error));
} else
return false;
}
2018-08-02 13:33:06 +00:00
}