Search in sources :

Example 1 with MessagePreviewCreator

use of com.fsck.k9.message.extractors.MessagePreviewCreator in project k-9 by k9mail.

the class LocalFolder method saveMessage.

protected void saveMessage(SQLiteDatabase db, Message message, boolean copy, Map<String, String> uidMap) throws MessagingException {
    if (!(message instanceof MimeMessage)) {
        throw new Error("LocalStore can only store Messages that extend MimeMessage");
    }
    long oldMessageId = -1;
    String uid = message.getUid();
    boolean shouldCreateNewMessage = uid == null || copy;
    if (shouldCreateNewMessage) {
        String randomLocalUid = K9.LOCAL_UID_PREFIX + UUID.randomUUID().toString();
        if (copy) {
            // Save mapping: source UID -> target UID
            uidMap.put(uid, randomLocalUid);
        } else {
            // Modify the Message instance to reference the new UID
            message.setUid(randomLocalUid);
        }
        // The message will be saved with the newly generated UID
        uid = randomLocalUid;
    } else {
        LocalMessage oldMessage = getMessage(uid);
        if (oldMessage != null) {
            oldMessageId = oldMessage.getId();
            long oldRootMessagePartId = oldMessage.getMessagePartId();
            deleteMessagePartsAndDataFromDisk(oldRootMessagePartId);
        }
    }
    long rootId = -1;
    long parentId = -1;
    long msgId;
    if (oldMessageId == -1) {
        // This is a new message. Do the message threading.
        ThreadInfo threadInfo = doMessageThreading(db, message);
        oldMessageId = threadInfo.msgId;
        rootId = threadInfo.rootId;
        parentId = threadInfo.parentId;
    }
    try {
        MessagePreviewCreator previewCreator = localStore.getMessagePreviewCreator();
        PreviewResult previewResult = previewCreator.createPreview(message);
        PreviewType previewType = previewResult.getPreviewType();
        DatabasePreviewType databasePreviewType = DatabasePreviewType.fromPreviewType(previewType);
        MessageFulltextCreator fulltextCreator = localStore.getMessageFulltextCreator();
        String fulltext = fulltextCreator.createFulltext(message);
        AttachmentCounter attachmentCounter = localStore.getAttachmentCounter();
        int attachmentCount = attachmentCounter.getAttachmentCount(message);
        long rootMessagePartId = saveMessageParts(db, message);
        ContentValues cv = new ContentValues();
        cv.put("message_part_id", rootMessagePartId);
        cv.put("uid", uid);
        cv.put("subject", message.getSubject());
        cv.put("sender_list", Address.pack(message.getFrom()));
        cv.put("date", message.getSentDate() == null ? System.currentTimeMillis() : message.getSentDate().getTime());
        cv.put("flags", this.localStore.serializeFlags(message.getFlags()));
        cv.put("deleted", message.isSet(Flag.DELETED) ? 1 : 0);
        cv.put("read", message.isSet(Flag.SEEN) ? 1 : 0);
        cv.put("flagged", message.isSet(Flag.FLAGGED) ? 1 : 0);
        cv.put("answered", message.isSet(Flag.ANSWERED) ? 1 : 0);
        cv.put("forwarded", message.isSet(Flag.FORWARDED) ? 1 : 0);
        cv.put("folder_id", mFolderId);
        cv.put("to_list", Address.pack(message.getRecipients(RecipientType.TO)));
        cv.put("cc_list", Address.pack(message.getRecipients(RecipientType.CC)));
        cv.put("bcc_list", Address.pack(message.getRecipients(RecipientType.BCC)));
        cv.put("reply_to_list", Address.pack(message.getReplyTo()));
        cv.put("attachment_count", attachmentCount);
        cv.put("internal_date", message.getInternalDate() == null ? System.currentTimeMillis() : message.getInternalDate().getTime());
        cv.put("mime_type", message.getMimeType());
        cv.put("empty", 0);
        cv.put("preview_type", databasePreviewType.getDatabaseValue());
        if (previewResult.isPreviewTextAvailable()) {
            cv.put("preview", previewResult.getPreviewText());
        } else {
            cv.putNull("preview");
        }
        String messageId = message.getMessageId();
        if (messageId != null) {
            cv.put("message_id", messageId);
        }
        if (oldMessageId == -1) {
            msgId = db.insert("messages", "uid", cv);
            // Create entry in 'threads' table
            cv.clear();
            cv.put("message_id", msgId);
            if (rootId != -1) {
                cv.put("root", rootId);
            }
            if (parentId != -1) {
                cv.put("parent", parentId);
            }
            db.insert("threads", null, cv);
        } else {
            msgId = oldMessageId;
            db.update("messages", cv, "id = ?", new String[] { Long.toString(oldMessageId) });
        }
        if (fulltext != null) {
            cv.clear();
            cv.put("docid", msgId);
            cv.put("fulltext", fulltext);
            db.replace("messages_fulltext", null, cv);
        }
    } catch (Exception e) {
        throw new MessagingException("Error appending message: " + message.getSubject(), e);
    }
}
Also used : ContentValues(android.content.ContentValues) MessageFulltextCreator(com.fsck.k9.message.extractors.MessageFulltextCreator) MessagingException(com.fsck.k9.mail.MessagingException) AttachmentCounter(com.fsck.k9.message.extractors.AttachmentCounter) PreviewResult(com.fsck.k9.message.extractors.PreviewResult) MessagePreviewCreator(com.fsck.k9.message.extractors.MessagePreviewCreator) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) PreviewType(com.fsck.k9.message.extractors.PreviewResult.PreviewType) MimeMessage(com.fsck.k9.mail.internet.MimeMessage)

Aggregations

ContentValues (android.content.ContentValues)1 MessagingException (com.fsck.k9.mail.MessagingException)1 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)1 WrappedException (com.fsck.k9.mailstore.LockableDatabase.WrappedException)1 AttachmentCounter (com.fsck.k9.message.extractors.AttachmentCounter)1 MessageFulltextCreator (com.fsck.k9.message.extractors.MessageFulltextCreator)1 MessagePreviewCreator (com.fsck.k9.message.extractors.MessagePreviewCreator)1 PreviewResult (com.fsck.k9.message.extractors.PreviewResult)1 PreviewType (com.fsck.k9.message.extractors.PreviewResult.PreviewType)1 IOException (java.io.IOException)1