Search in sources :

Example 1 with DatabasePreviewType

use of com.fsck.k9.mailstore.DatabasePreviewType in project k-9 by k9mail.

the class MessageListAdapter method getPreview.

private String getPreview(Cursor cursor) {
    String previewTypeString = cursor.getString(PREVIEW_TYPE_COLUMN);
    DatabasePreviewType previewType = DatabasePreviewType.fromDatabaseValue(previewTypeString);
    switch(previewType) {
        case NONE:
        case ERROR:
            {
                return "";
            }
        case ENCRYPTED:
            {
                return fragment.getString(R.string.preview_encrypted);
            }
        case TEXT:
            {
                return cursor.getString(PREVIEW_COLUMN);
            }
    }
    throw new AssertionError("Unknown preview type: " + previewType);
}
Also used : DatabasePreviewType(com.fsck.k9.mailstore.DatabasePreviewType)

Example 2 with DatabasePreviewType

use of com.fsck.k9.mailstore.DatabasePreviewType in project k-9 by k9mail.

the class LocalMessage method populateFromGetMessageCursor.

void populateFromGetMessageCursor(Cursor cursor) throws MessagingException {
    final String subject = cursor.getString(0);
    this.setSubject(subject == null ? "" : subject);
    Address[] from = Address.unpack(cursor.getString(1));
    if (from.length > 0) {
        this.setFrom(from[0]);
    }
    this.setInternalSentDate(new Date(cursor.getLong(2)));
    this.setUid(cursor.getString(3));
    String flagList = cursor.getString(4);
    if (flagList != null && flagList.length() > 0) {
        String[] flags = flagList.split(",");
        for (String flag : flags) {
            try {
                this.setFlagInternal(Flag.valueOf(flag), true);
            } catch (Exception e) {
                if (!"X_BAD_FLAG".equals(flag)) {
                    Timber.w("Unable to parse flag %s", flag);
                }
            }
        }
    }
    this.mId = cursor.getLong(5);
    this.setRecipients(RecipientType.TO, Address.unpack(cursor.getString(6)));
    this.setRecipients(RecipientType.CC, Address.unpack(cursor.getString(7)));
    this.setRecipients(RecipientType.BCC, Address.unpack(cursor.getString(8)));
    this.setReplyTo(Address.unpack(cursor.getString(9)));
    this.mAttachmentCount = cursor.getInt(10);
    this.setInternalDate(new Date(cursor.getLong(11)));
    this.setMessageId(cursor.getString(12));
    String previewTypeString = cursor.getString(24);
    DatabasePreviewType databasePreviewType = DatabasePreviewType.fromDatabaseValue(previewTypeString);
    previewType = databasePreviewType.getPreviewType();
    if (previewType == PreviewType.TEXT) {
        mPreview = cursor.getString(14);
    } else {
        mPreview = "";
    }
    if (this.mFolder == null) {
        LocalFolder f = new LocalFolder(this.localStore, cursor.getInt(13));
        f.open(LocalFolder.OPEN_MODE_RW);
        this.mFolder = f;
    }
    mThreadId = (cursor.isNull(15)) ? -1 : cursor.getLong(15);
    mRootId = (cursor.isNull(16)) ? -1 : cursor.getLong(16);
    boolean deleted = (cursor.getInt(17) == 1);
    boolean read = (cursor.getInt(18) == 1);
    boolean flagged = (cursor.getInt(19) == 1);
    boolean answered = (cursor.getInt(20) == 1);
    boolean forwarded = (cursor.getInt(21) == 1);
    setFlagInternal(Flag.DELETED, deleted);
    setFlagInternal(Flag.SEEN, read);
    setFlagInternal(Flag.FLAGGED, flagged);
    setFlagInternal(Flag.ANSWERED, answered);
    setFlagInternal(Flag.FORWARDED, forwarded);
    setMessagePartId(cursor.getLong(22));
    mimeType = cursor.getString(23);
    byte[] header = cursor.getBlob(25);
    if (header != null) {
        MessageHeaderParser.parse(this, new ByteArrayInputStream(header));
    } else {
        Timber.d("No headers available for this message!");
    }
    headerNeedsUpdating = false;
}
Also used : Address(com.fsck.k9.mail.Address) ByteArrayInputStream(java.io.ByteArrayInputStream) Date(java.util.Date) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException)

Example 3 with DatabasePreviewType

use of com.fsck.k9.mailstore.DatabasePreviewType 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

MessagingException (com.fsck.k9.mail.MessagingException)2 WrappedException (com.fsck.k9.mailstore.LockableDatabase.WrappedException)2 IOException (java.io.IOException)2 ContentValues (android.content.ContentValues)1 Address (com.fsck.k9.mail.Address)1 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)1 DatabasePreviewType (com.fsck.k9.mailstore.DatabasePreviewType)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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Date (java.util.Date)1