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;
|
|
|
|
|
2018-08-12 10:51:09 +00:00
|
|
|
import java.util.Date;
|
2018-08-06 11:05:14 +00:00
|
|
|
|
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;
|
2018-08-11 08:28:54 +00:00
|
|
|
@NonNull
|
2018-08-02 13:33:06 +00:00
|
|
|
public String args;
|
2018-08-12 10:51:09 +00:00
|
|
|
@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";
|
2018-08-06 11:05:14 +00:00
|
|
|
|
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)) {
|
2018-12-06 10:59:57 +00:00
|
|
|
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid)) {
|
2018-12-03 15:16:25 +00:00
|
|
|
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))
|
2018-12-06 10:59:57 +00:00
|
|
|
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid))
|
2018-12-03 15:16:25 +00:00
|
|
|
db.message().setMessageUiFlagged(similar.id, (boolean) value);
|
|
|
|
|
2018-12-03 14:46:24 +00:00
|
|
|
else if (ANSWERED.equals(name))
|
2018-12-06 10:59:57 +00:00
|
|
|
for (EntityMessage similar : db.message().getMessageByMsgId(message.account, message.msgid))
|
2018-12-03 15:16:25 +00:00
|
|
|
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) {
|
2018-12-03 13:41:23 +00:00
|
|
|
if (db.operation().getOperationCount(folder, EntityOperation.SYNC) == 0) {
|
2018-12-02 13:19:54 +00:00
|
|
|
queue(db, folder, null, EntityOperation.SYNC, new JSONArray());
|
2018-12-03 13:41:23 +00:00
|
|
|
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();
|
2018-08-12 10:51:09 +00:00
|
|
|
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-06 11:05:14 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-11 08:28:54 +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)) &&
|
2018-08-11 08:28:54 +00:00
|
|
|
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)));
|
2018-08-11 08:28:54 +00:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|