Search in sources :

Example 1 with PreviewResult

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

the class MessagePreviewCreatorTest method createPreview_withTextPart.

@Test
public void createPreview_withTextPart() throws Exception {
    Message message = createDummyMessage();
    Part textPart = createTextPart("text/plain");
    when(encryptionDetector.isEncrypted(message)).thenReturn(false);
    when(textPartFinder.findFirstTextPart(message)).thenReturn(textPart);
    when(previewTextExtractor.extractPreview(textPart)).thenReturn("expected");
    PreviewResult result = previewCreator.createPreview(message);
    assertTrue(result.isPreviewTextAvailable());
    assertEquals(PreviewType.TEXT, result.getPreviewType());
    assertEquals("expected", result.getPreviewText());
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 2 with PreviewResult

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

the class MessagePreviewCreatorTest method createPreview_withEmptyTextPart.

@Test
public void createPreview_withEmptyTextPart() throws Exception {
    Message message = createDummyMessage();
    Part textPart = createEmptyPart("text/plain");
    when(encryptionDetector.isEncrypted(message)).thenReturn(false);
    when(textPartFinder.findFirstTextPart(message)).thenReturn(textPart);
    PreviewResult result = previewCreator.createPreview(message);
    assertFalse(result.isPreviewTextAvailable());
    assertEquals(PreviewType.NONE, result.getPreviewType());
    verifyNoMoreInteractions(previewTextExtractor);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 3 with PreviewResult

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

the class MessagePreviewCreatorTest method createPreview_withPreviewTextExtractorThrowing.

@Test
public void createPreview_withPreviewTextExtractorThrowing() throws Exception {
    Message message = createDummyMessage();
    Part textPart = createTextPart("text/plain");
    when(encryptionDetector.isEncrypted(message)).thenReturn(false);
    when(textPartFinder.findFirstTextPart(message)).thenReturn(textPart);
    when(previewTextExtractor.extractPreview(textPart)).thenThrow(new PreviewExtractionException(""));
    PreviewResult result = previewCreator.createPreview(message);
    assertFalse(result.isPreviewTextAvailable());
    assertEquals(PreviewType.ERROR, result.getPreviewType());
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 4 with PreviewResult

use of com.fsck.k9.message.extractors.PreviewResult 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)

Example 5 with PreviewResult

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

the class MessagePreviewCreatorTest method createPreview_withEncryptedMessage.

@Test
public void createPreview_withEncryptedMessage() throws Exception {
    Message message = createDummyMessage();
    when(encryptionDetector.isEncrypted(message)).thenReturn(true);
    PreviewResult result = previewCreator.createPreview(message);
    assertFalse(result.isPreviewTextAvailable());
    assertEquals(PreviewType.ENCRYPTED, result.getPreviewType());
    verifyNoMoreInteractions(textPartFinder);
    verifyNoMoreInteractions(previewTextExtractor);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) Test(org.junit.Test)

Aggregations

MimeMessage (com.fsck.k9.mail.internet.MimeMessage)6 Message (com.fsck.k9.mail.Message)5 Test (org.junit.Test)5 Part (com.fsck.k9.mail.Part)3 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)3 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)3 ContentValues (android.content.ContentValues)1 MessagingException (com.fsck.k9.mail.MessagingException)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