Experimental delete attachment support

pull/213/head
M66B 2 years ago
parent efdfab0744
commit 59f1e78af2

@ -137,7 +137,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
lparam.setMarginStart(attachment.subsequence == null ? 0 : dp12);
view.setLayoutParams(lparam);
ibDelete.setVisibility(readonly ? View.GONE : View.VISIBLE);
ibDelete.setVisibility(readonly && !BuildConfig.DEBUG ? View.GONE : View.VISIBLE);
if (!readonly && attachment.isImage()) {
if (attachment.available) {
@ -343,11 +343,13 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
private void onDelete(final EntityAttachment attachment) {
Bundle args = new Bundle();
args.putLong("id", attachment.id);
args.putBoolean("readonly", readonly);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean readonly = args.getBoolean("readonly");
EntityAttachment attachment;
@ -359,6 +361,13 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
if (attachment == null)
return null;
if (readonly) {
EntityMessage message = db.message().getMessage(attachment.message);
if (message == null)
return null;
EntityOperation.queue(context, message, EntityOperation.ATTACHMENT, attachment.id, true);
db.message().setMessageUiHide(message.id, true);
} else
db.attachment().deleteAttachment(attachment.id);
db.setTransactionSuccessful();
@ -366,6 +375,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
db.endTransaction();
}
if (!readonly)
attachment.getFile(context).delete();
return null;

@ -118,6 +118,8 @@ import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.StoreClosedException;
@ -2054,6 +2056,7 @@ class Core {
DB db = DB.getInstance(context);
long id = jargs.getLong(0);
boolean delete = jargs.optBoolean(1, false);
// Get attachment
EntityAttachment attachment = db.attachment().getAttachment(id);
@ -2073,6 +2076,23 @@ class Core {
if (imessage == null)
throw new MessageRemovedException();
if (delete) {
MimeMessage icopy = new MimeMessageEx((MimeMessage) imessage, message.msgid);
MessageHelper helper = new MessageHelper(icopy, context);
MessageHelper.MessageParts parts = helper.getMessageParts();
List<MessageHelper.AttachmentPart> aparts = parts.getAttachmentParts();
if (attachment.sequence > aparts.size())
throw new IllegalArgumentException("Invalid attachment sequence=" + attachment.sequence + "/" + aparts.size());
Part apart = aparts.get(attachment.sequence - 1).part;
if (!deletePart(icopy, apart))
throw new IllegalArgumentException("Attachment part not found");
ifolder.appendMessages(new Message[]{icopy});
imessage.setFlag(Flags.Flag.DELETED, true);
expunge(context, ifolder, Arrays.asList(imessage));
} else {
// Get message parts
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
MessageHelper.MessageParts parts = helper.getMessageParts();
@ -2083,6 +2103,32 @@ class Core {
if (attachment.size != null)
EntityLog.log(context, "Operation attachment size=" + attachment.size);
}
}
static boolean deletePart(Part part, Part attachment) throws MessagingException, IOException {
boolean deleted = false;
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int count = multipart.getCount();
for (int i = 0; i < count; i++) {
Part child = multipart.getBodyPart(i);
if (child == attachment) {
multipart.removeBodyPart(i);
deleted = true;
} else {
if (deletePart(child, attachment))
deleted = true;
}
}
}
if (part instanceof MimeMessage)
((MimeMessage) part).saveChanges();
else if (part instanceof Multipart)
part.setDataHandler(part.getDataHandler());
return deleted;
}
private static void onBody(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, POP3Folder ifolder, POP3Store istore) throws MessagingException, IOException {
if (!EntityFolder.INBOX.equals(folder.type))

@ -30,6 +30,11 @@ public class MimeMessageEx extends MimeMessage {
private String msgid;
private MimeMessage original;
MimeMessageEx(MimeMessage imessage, String msgid) throws MessagingException {
super(imessage);
this.msgid = msgid;
}
MimeMessageEx(Session session, String msgid) {
super(session);
this.msgid = msgid;

Loading…
Cancel
Save