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

225 lines
8.8 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;
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;
import java.util.Date;
2019-01-04 18:37:56 +00:00
import java.util.List;
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 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
static void sync(DB db, long fid) {
if (db.operation().getOperationCount(fid, EntityOperation.SYNC) == 0) {
2019-01-01 18:49:21 +00:00
EntityFolder folder = db.folder().getFolder(fid);
2019-01-01 18:49:21 +00:00
int sync_days = folder.sync_days;
if (folder.last_sync != null) {
int ago_days = (int) ((new Date().getTime() - folder.last_sync) / (24 * 3600 * 1000L)) + 1;
if (ago_days > sync_days)
sync_days = ago_days;
}
JSONArray jargs = new JSONArray();
2019-01-01 18:49:21 +00:00
jargs.put(folder.initialize ? Math.min(EntityFolder.DEFAULT_INIT, folder.keep_days) : sync_days);
jargs.put(folder.keep_days);
2018-12-25 08:37:19 +00:00
jargs.put(folder.download);
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 = jargs.toString();
2018-12-10 17:44:45 +00:00
operation.created = new Date().getTime();
operation.id = db.operation().insertOperation(operation);
db.folder().setFolderSyncState(fid, "requested");
2018-12-10 17:44:45 +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) {
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));
db.message().setMessageUiIgnored(similar.id, true);
}
} 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)) {
EntityFolder source = db.folder().getFolder(message.folder);
2019-01-04 18:37:56 +00:00
EntityFolder target = db.folder().getFolder(jargs.getLong(0));
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-04 18:37:56 +00:00
// Create copy without uid in target folder
// Message with same msgid can be in archive and source folder
if (db.message().countMessageByMsgId(target.id, message.msgid) == 0) {
long id = message.id;
long uid = message.uid;
message.id = null;
message.uid = null;
message.folder = target.id;
long newid = db.message().insertMessage(message);
message.id = id;
message.uid = uid;
message.folder = source.id;
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);
}
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
for (EntityAttachment attachment : attachments) {
long aid = attachment.id;
attachment.id = null;
attachment.message = newid;
attachment.progress = null;
attachment.id = db.attachment().insertAttachment(attachment);
if (attachment.available)
try {
Helper.copy(
EntityAttachment.getFile(context, aid),
EntityAttachment.getFile(context, attachment.id));
} catch (IOException ex) {
Log.e(ex);
2019-01-06 10:15:06 +00:00
db.attachment().setProgress(attachment.id, null);
2019-01-04 18:37:56 +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);
} 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();
2018-12-10 17:44:45 +00:00
operation.folder = message.folder;
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);
}
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) &&
2018-12-02 13:19:54 +00:00
(this.message == null ? other.message == null : this.message.equals(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) &&
(this.error == null ? other.error == null : this.error.equals(other.error)));
} else
return false;
}
2018-08-02 13:33:06 +00:00
}