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
|
|
|
*/
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
2018-12-10 17:44:45 +00:00
|
|
|
import org.json.JSONException;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
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-10 17:44:45 +00:00
|
|
|
queue(db, message, 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-10 17:44:45 +00:00
|
|
|
queue(db, message, 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 value1, Object value2) {
|
2018-12-01 13:02:27 +00:00
|
|
|
JSONArray jargs = new JSONArray();
|
|
|
|
jargs.put(value1);
|
|
|
|
jargs.put(value2);
|
2018-12-10 17:44:45 +00:00
|
|
|
queue(db, message, name, jargs);
|
2018-08-09 20:45:42 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-11 11:18:26 +00:00
|
|
|
static void sync(DB db, long fid) {
|
|
|
|
if (db.operation().getOperationCount(fid, EntityOperation.SYNC) == 0) {
|
|
|
|
EntityFolder folder = db.folder().getFolder(fid);
|
|
|
|
JSONArray jargs = new JSONArray();
|
|
|
|
jargs.put(folder.initialize ? Math.min(EntityFolder.DEFAULT_INIT, folder.keep_days) : folder.sync_days);
|
|
|
|
jargs.put(folder.keep_days);
|
2018-12-25 08:37:19 +00:00
|
|
|
jargs.put(folder.download);
|
2018-12-11 11:18:26 +00:00
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
EntityOperation operation = new EntityOperation();
|
2018-12-11 11:18:26 +00:00
|
|
|
operation.folder = folder.id;
|
2018-12-10 17:44:45 +00:00
|
|
|
operation.message = null;
|
|
|
|
operation.name = SYNC;
|
2018-12-11 11:18:26 +00:00
|
|
|
operation.args = jargs.toString();
|
2018-12-10 17:44:45 +00:00
|
|
|
operation.created = new Date().getTime();
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
|
2018-12-11 11:18:26 +00:00
|
|
|
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-03 13:41:23 +00:00
|
|
|
}
|
2018-12-02 13:19:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
private static void queue(DB db, EntityMessage message, String name, JSONArray jargs) {
|
|
|
|
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);
|
|
|
|
if (!EntityFolder.ARCHIVE.equals(source.type))
|
|
|
|
db.message().setMessageUiHide(message.id, true);
|
2018-12-10 17:44:45 +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();
|
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-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-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
|
|
|
}
|