Search in sources :

Example 11 with LocalMessage

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

the class LocalFolder method loadMessageParts.

private void loadMessageParts(SQLiteDatabase db, LocalMessage message) throws MessagingException {
    Map<Long, Part> partById = new HashMap<>();
    String[] columns = { // 0
    "id", // 1
    "type", // 2
    "parent", // 3
    "mime_type", // 4
    "decoded_body_size", // 5
    "display_name", // 6
    "header", // 7
    "encoding", // 8
    "charset", // 9
    "data_location", // 10
    "data", // 11
    "preamble", // 12
    "epilogue", // 13
    "boundary", // 14
    "content_id", // 15
    "server_extra" };
    Cursor cursor = db.query("message_parts", columns, "root = ?", new String[] { String.valueOf(message.getMessagePartId()) }, null, null, "seq");
    try {
        while (cursor.moveToNext()) {
            loadMessagePart(message, partById, cursor);
        }
    } finally {
        cursor.close();
    }
}
Also used : HashMap(java.util.HashMap) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Cursor(android.database.Cursor)

Example 12 with LocalMessage

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

the class LocalFolder method moveMessages.

@Override
public Map<String, String> moveMessages(final List<? extends Message> msgs, final Folder destFolder) throws MessagingException {
    if (!(destFolder instanceof LocalFolder)) {
        throw new MessagingException("moveMessages called with non-LocalFolder");
    }
    final LocalFolder lDestFolder = (LocalFolder) destFolder;
    final Map<String, String> uidMap = new HashMap<>();
    try {
        this.localStore.database.execute(false, new DbCallback<Void>() {

            @Override
            public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                try {
                    lDestFolder.open(OPEN_MODE_RW);
                    for (Message message : msgs) {
                        LocalMessage lMessage = (LocalMessage) message;
                        String oldUID = message.getUid();
                        Timber.d("Updating folder_id to %s for message with UID %s, " + "id %d currently in folder %s", lDestFolder.getId(), message.getUid(), lMessage.getId(), getName());
                        String newUid = K9.LOCAL_UID_PREFIX + UUID.randomUUID().toString();
                        message.setUid(newUid);
                        uidMap.put(oldUID, newUid);
                        // Message threading in the target folder
                        ThreadInfo threadInfo = lDestFolder.doMessageThreading(db, message);
                        /*
                             * "Move" the message into the new folder
                             */
                        long msgId = lMessage.getId();
                        String[] idArg = new String[] { Long.toString(msgId) };
                        ContentValues cv = new ContentValues();
                        cv.put("folder_id", lDestFolder.getId());
                        cv.put("uid", newUid);
                        db.update("messages", cv, "id = ?", idArg);
                        // Create/update entry in 'threads' table for the message in the
                        // target folder
                        cv.clear();
                        cv.put("message_id", msgId);
                        if (threadInfo.threadId == -1) {
                            if (threadInfo.rootId != -1) {
                                cv.put("root", threadInfo.rootId);
                            }
                            if (threadInfo.parentId != -1) {
                                cv.put("parent", threadInfo.parentId);
                            }
                            db.insert("threads", null, cv);
                        } else {
                            db.update("threads", cv, "id = ?", new String[] { Long.toString(threadInfo.threadId) });
                        }
                        /*
                             * Add a placeholder message so we won't download the original
                             * message again if we synchronize before the remote move is
                             * complete.
                             */
                        // We need to open this folder to get the folder id
                        open(OPEN_MODE_RW);
                        cv.clear();
                        cv.put("uid", oldUID);
                        cv.putNull("flags");
                        cv.put("read", 1);
                        cv.put("deleted", 1);
                        cv.put("folder_id", mFolderId);
                        cv.put("empty", 0);
                        String messageId = message.getMessageId();
                        if (messageId != null) {
                            cv.put("message_id", messageId);
                        }
                        final long newId;
                        if (threadInfo.msgId != -1) {
                            // There already existed an empty message in the target folder.
                            // Let's use it as placeholder.
                            newId = threadInfo.msgId;
                            db.update("messages", cv, "id = ?", new String[] { Long.toString(newId) });
                        } else {
                            newId = db.insert("messages", null, cv);
                        }
                        /*
                             * Update old entry in 'threads' table to point to the newly
                             * created placeholder.
                             */
                        cv.clear();
                        cv.put("message_id", newId);
                        db.update("threads", cv, "id = ?", new String[] { Long.toString(lMessage.getThreadId()) });
                    }
                } catch (MessagingException e) {
                    throw new WrappedException(e);
                }
                return null;
            }
        });
        this.localStore.notifyChange();
        return uidMap;
    } catch (WrappedException e) {
        throw (MessagingException) e.getCause();
    }
}
Also used : ContentValues(android.content.ContentValues) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) HashMap(java.util.HashMap) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 13 with LocalMessage

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

the class MigrationTest method migrateTextHtml.

@Test
public void migrateTextHtml() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertMultipartAlternativeMessage(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("9");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(8, msg.getId());
    Assert.assertEquals(9, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/alternative", msg.getMimeType());
    Assert.assertEquals(0, msg.getAttachmentCount());
    Multipart msgBody = (Multipart) msg.getBody();
    Assert.assertEquals("------------060200010509000000040004", msgBody.getBoundary());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 14 with LocalMessage

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

the class MigrationTest method migratePgpInlineEncryptedMessage.

@Test
public void migratePgpInlineEncryptedMessage() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertPgpInlineEncryptedMessage(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("7");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(6, msg.getId());
    Assert.assertEquals(12, msg.getHeaderNames().size());
    Assert.assertEquals("text/plain", msg.getMimeType());
    Assert.assertEquals(0, msg.getAttachmentCount());
    Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody);
    String msgTextContent = MessageExtractor.getTextFromPart(msg);
    Assert.assertEquals(OpenPgpUtils.PARSE_RESULT_MESSAGE, OpenPgpUtils.parseMessage(msgTextContent));
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 15 with LocalMessage

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

the class LocalStoreTest method findPartById__withRootLocalMessage.

@Test
public void findPartById__withRootLocalMessage() throws Exception {
    LocalMessage searchRoot = new LocalMessage(null, "uid", null);
    searchRoot.setMessagePartId(123L);
    Part part = LocalStore.findPartById(searchRoot, 123L);
    assertSame(searchRoot, part);
}
Also used : Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Aggregations

LocalMessage (com.fsck.k9.mailstore.LocalMessage)42 Test (org.junit.Test)23 FetchProfile (com.fsck.k9.mail.FetchProfile)19 Message (com.fsck.k9.mail.Message)19 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)19 MessagingException (com.fsck.k9.mail.MessagingException)15 LocalFolder (com.fsck.k9.mailstore.LocalFolder)13 LocalStore (com.fsck.k9.mailstore.LocalStore)12 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)10 ArrayList (java.util.ArrayList)9 Notification (android.app.Notification)8 Part (com.fsck.k9.mail.Part)8 MessageReference (com.fsck.k9.activity.MessageReference)7 Multipart (com.fsck.k9.mail.Multipart)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 SuppressLint (android.annotation.SuppressLint)6 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)6 IOException (java.io.IOException)6 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)5