2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of Safe email.
|
|
|
|
|
|
|
|
Safe email is free software: you can redistribute it and/or modify
|
|
|
|
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.
|
|
|
|
|
|
|
|
NetGuard is distributed in the hope that it will be useful,
|
|
|
|
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
|
|
|
|
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.arch.persistence.room.Entity;
|
|
|
|
import android.arch.persistence.room.ForeignKey;
|
|
|
|
import android.arch.persistence.room.Index;
|
|
|
|
import android.arch.persistence.room.PrimaryKey;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
|
|
|
import static android.arch.persistence.room.ForeignKey.CASCADE;
|
|
|
|
|
|
|
|
@Entity(
|
|
|
|
tableName = EntityOperation.TABLE_NAME,
|
|
|
|
foreignKeys = {
|
|
|
|
@ForeignKey(childColumns = "message", entity = EntityMessage.class, parentColumns = "id", onDelete = CASCADE)
|
|
|
|
},
|
|
|
|
indices = {
|
|
|
|
@Index(value = {"message"})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
public class EntityOperation {
|
|
|
|
static final String TABLE_NAME = "operation";
|
|
|
|
|
|
|
|
@PrimaryKey(autoGenerate = true)
|
|
|
|
public Long id;
|
|
|
|
@NonNull
|
|
|
|
public Long message;
|
|
|
|
@NonNull
|
|
|
|
public String name;
|
|
|
|
public String args;
|
|
|
|
|
|
|
|
public static final String SEEN = "seen";
|
|
|
|
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-08-03 19:12:19 +00:00
|
|
|
public static final String ATTACHMENT = "attachment";
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
static void queue(Context context, EntityMessage message, String name) {
|
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
|
queue(context, message, name, jsonArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void queue(Context context, EntityMessage message, String name, Object value) {
|
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
|
jsonArray.put(value);
|
|
|
|
queue(context, message, name, jsonArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void queue(Context context, EntityMessage message, String name, JSONArray jsonArray) {
|
|
|
|
DaoOperation dao = DB.getInstance(context).operation();
|
|
|
|
|
|
|
|
int purged = 0;
|
2018-08-02 17:58:40 +00:00
|
|
|
if (SEEN.equals(name)) {
|
|
|
|
if (message.uid == null) {
|
|
|
|
// local message
|
|
|
|
return;
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
purged = dao.deleteOperations(message.id, name);
|
2018-08-02 20:52:06 +00:00
|
|
|
} else if (DELETE.equals(name)) {
|
|
|
|
if (message.uid == null)
|
|
|
|
return;
|
2018-08-02 17:58:40 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
EntityOperation operation = new EntityOperation();
|
|
|
|
operation.message = message.id;
|
|
|
|
operation.name = name;
|
|
|
|
operation.args = jsonArray.toString();
|
|
|
|
operation.id = dao.insertOperation(operation);
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Queued op=" + operation.id + "/" + name +
|
|
|
|
" args=" + operation.args +
|
|
|
|
" msg=" + message.folder + "/" + message.id + " uid=" + message.uid +
|
|
|
|
" purged=" + purged);
|
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(
|
2018-08-04 22:35:47 +00:00
|
|
|
new Intent(SEND.equals(name)
|
|
|
|
? ServiceSynchronize.ACTION_PROCESS_OUTBOX
|
|
|
|
: ServiceSynchronize.ACTION_PROCESS_FOLDER)
|
|
|
|
.putExtra("folder", message.folder));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|