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

148 lines
5.5 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
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.util.Log;
import org.json.JSONArray;
import java.util.Date;
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
public static final String ADD = "add";
public static final String MOVE = "move";
public static final String DELETE = "delete";
public static final String SEND = "send";
2018-11-25 12:34:08 +00:00
public static final String SEEN = "seen";
2018-11-24 18:14:28 +00:00
public static final String ANSWERED = "answered";
2018-11-25 12:34:08 +00:00
public static final String FLAG = "flag";
public static final String KEYWORD = "keyword";
2018-09-05 07:23:51 +00:00
public static final String HEADERS = "headers";
2018-09-15 07:22:42 +00:00
public static final String BODY = "body";
public static final String ATTACHMENT = "attachment";
2018-12-02 13:19:54 +00:00
public static final String SYNC = "sync";
2018-08-09 20:45:42 +00:00
static void queue(DB db, EntityMessage message, String name) {
2018-12-01 13:02:27 +00:00
JSONArray jargs = new JSONArray();
2018-12-02 13:19:54 +00:00
queue(db, message.folder, message.id, name, jargs);
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
static void queue(DB db, EntityMessage message, String name, Object value) {
2018-12-01 13:02:27 +00:00
JSONArray jargs = new JSONArray();
jargs.put(value);
2018-12-02 13:19:54 +00:00
queue(db, message.folder, message.id, name, jargs);
2018-12-03 14:46:24 +00:00
if (SEEN.equals(name)) {
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid, message.ui_found)) {
db.message().setMessageUiSeen(similar.id, (boolean) value);
db.message().setMessageUiIgnored(similar.id, true);
}
2018-12-03 14:46:24 +00:00
} else if (FLAG.equals(name))
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid, message.ui_found))
db.message().setMessageUiFlagged(similar.id, (boolean) value);
2018-12-03 14:46:24 +00:00
else if (ANSWERED.equals(name))
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid, message.ui_found))
db.message().setMessageUiAnswered(similar.id, (boolean) value);
2018-12-03 14:46:24 +00:00
else if (MOVE.equals(name))
db.message().setMessageUiHide(message.id, true);
else if (DELETE.equals(name))
db.message().setMessageUiHide(message.id, true);
2018-08-02 13:33:06 +00:00
}
2018-08-09 20:45:42 +00:00
static void queue(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);
2018-12-02 13:19:54 +00:00
queue(db, message.folder, message.id, name, jargs);
2018-08-09 20:45:42 +00:00
}
2018-08-02 13:33:06 +00:00
2018-12-02 13:19:54 +00:00
static void sync(DB db, long folder) {
if (db.operation().getOperationCount(folder, EntityOperation.SYNC) == 0) {
2018-12-02 13:19:54 +00:00
queue(db, folder, null, EntityOperation.SYNC, new JSONArray());
db.folder().setFolderSyncState(folder, "requested");
}
2018-12-02 13:19:54 +00:00
}
private static void queue(DB db, long folder, Long message, String name, JSONArray jargs) {
2018-08-02 13:33:06 +00:00
EntityOperation operation = new EntityOperation();
2018-12-02 13:19:54 +00:00
operation.folder = folder;
operation.message = message;
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-08-09 20:45:42 +00:00
Log.i(Helper.TAG, "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
}