Search in sources :

Example 1 with MigrationsHelper

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

the class MigrationTo55 method createFtsSearchTable.

static void createFtsSearchTable(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    db.execSQL("CREATE VIRTUAL TABLE messages_fulltext USING fts4 (fulltext)");
    LocalStore localStore = migrationsHelper.getLocalStore();
    MessageFulltextCreator fulltextCreator = localStore.getMessageFulltextCreator();
    try {
        List<LocalFolder> folders = localStore.getPersonalNamespaces(true);
        ContentValues cv = new ContentValues();
        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.BODY);
        for (LocalFolder folder : folders) {
            List<String> messageUids = folder.getAllMessageUids();
            for (String messageUid : messageUids) {
                LocalMessage localMessage = folder.getMessage(messageUid);
                folder.fetch(Collections.singletonList(localMessage), fp, null);
                String fulltext = fulltextCreator.createFulltext(localMessage);
                if (!TextUtils.isEmpty(fulltext)) {
                    Timber.d("fulltext for msg id %d is %d chars long", localMessage.getId(), fulltext.length());
                    cv.clear();
                    cv.put("docid", localMessage.getId());
                    cv.put("fulltext", fulltext);
                    db.insert("messages_fulltext", null, cv);
                } else {
                    Timber.d("no fulltext for msg id %d :(", localMessage.getId());
                }
            }
        }
    } catch (MessagingException e) {
        Timber.e(e, "error indexing fulltext - skipping rest, fts index is incomplete!");
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) ContentValues(android.content.ContentValues) LocalMessage(com.fsck.k9.mailstore.LocalMessage) FetchProfile(com.fsck.k9.mail.FetchProfile) MessageFulltextCreator(com.fsck.k9.message.extractors.MessageFulltextCreator) MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore)

Example 2 with MigrationsHelper

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

the class MigrationTo42 method from41MoveFolderPreferences.

public static void from41MoveFolderPreferences(MigrationsHelper migrationsHelper) {
    try {
        LocalStore localStore = migrationsHelper.getLocalStore();
        Storage storage = migrationsHelper.getStorage();
        long startTime = System.currentTimeMillis();
        StorageEditor editor = storage.edit();
        List<? extends Folder> folders = localStore.getPersonalNamespaces(true);
        for (Folder folder : folders) {
            if (folder instanceof LocalFolder) {
                LocalFolder lFolder = (LocalFolder) folder;
                lFolder.save(editor);
            }
        }
        editor.commit();
        long endTime = System.currentTimeMillis();
        Timber.i("Putting folder preferences for %d folders back into Preferences took %d ms", folders.size(), endTime - startTime);
    } catch (Exception e) {
        Timber.e(e, "Could not replace Preferences in upgrade from DB_VERSION 41");
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) Storage(com.fsck.k9.preferences.Storage) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) StorageEditor(com.fsck.k9.preferences.StorageEditor)

Example 3 with MigrationsHelper

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

the class MigrationTo43 method fixOutboxFolders.

public static void fixOutboxFolders(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    try {
        LocalStore localStore = migrationsHelper.getLocalStore();
        Account account = migrationsHelper.getAccount();
        Context context = migrationsHelper.getContext();
        // If folder "OUTBOX" (old, v3.800 - v3.802) exists, rename it to
        // "K9MAIL_INTERNAL_OUTBOX" (new)
        LocalFolder oldOutbox = new LocalFolder(localStore, "OUTBOX");
        if (oldOutbox.exists()) {
            ContentValues cv = new ContentValues();
            cv.put("name", Account.OUTBOX);
            db.update("folders", cv, "name = ?", new String[] { "OUTBOX" });
            Timber.i("Renamed folder OUTBOX to %s", OUTBOX);
        }
        // Check if old (pre v3.800) localized outbox folder exists
        String localizedOutbox = context.getString(R.string.special_mailbox_name_outbox);
        LocalFolder obsoleteOutbox = new LocalFolder(localStore, localizedOutbox);
        if (obsoleteOutbox.exists()) {
            // Get all messages from the localized outbox ...
            List<? extends Message> messages = obsoleteOutbox.getMessages(null, false);
            if (messages.size() > 0) {
                // ... and move them to the drafts folder (we don't want to
                // surprise the user by sending potentially very old messages)
                LocalFolder drafts = new LocalFolder(localStore, account.getDraftsFolderName());
                obsoleteOutbox.moveMessages(messages, drafts);
            }
            // Now get rid of the localized outbox
            obsoleteOutbox.delete();
            obsoleteOutbox.delete(true);
        }
    } catch (Exception e) {
        Timber.e(e, "Error trying to fix the outbox folders");
    }
}
Also used : Context(android.content.Context) LocalFolder(com.fsck.k9.mailstore.LocalFolder) ContentValues(android.content.ContentValues) Account(com.fsck.k9.Account) LocalStore(com.fsck.k9.mailstore.LocalStore)

Example 4 with MigrationsHelper

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

the class MigrationTo46 method addMessagesFlagColumns.

public static void addMessagesFlagColumns(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    db.execSQL("ALTER TABLE messages ADD read INTEGER default 0");
    db.execSQL("ALTER TABLE messages ADD flagged INTEGER default 0");
    db.execSQL("ALTER TABLE messages ADD answered INTEGER default 0");
    db.execSQL("ALTER TABLE messages ADD forwarded INTEGER default 0");
    String[] projection = { "id", "flags" };
    ContentValues cv = new ContentValues();
    List<Flag> extraFlags = new ArrayList<>();
    Cursor cursor = db.query("messages", projection, null, null, null, null, null);
    try {
        while (cursor.moveToNext()) {
            long id = cursor.getLong(0);
            String flagList = cursor.getString(1);
            boolean read = false;
            boolean flagged = false;
            boolean answered = false;
            boolean forwarded = false;
            if (flagList != null && flagList.length() > 0) {
                String[] flags = flagList.split(",");
                for (String flagStr : flags) {
                    try {
                        Flag flag = Flag.valueOf(flagStr);
                        switch(flag) {
                            case ANSWERED:
                                {
                                    answered = true;
                                    break;
                                }
                            case DELETED:
                                {
                                    // Don't store this in column 'flags'
                                    break;
                                }
                            case FLAGGED:
                                {
                                    flagged = true;
                                    break;
                                }
                            case FORWARDED:
                                {
                                    forwarded = true;
                                    break;
                                }
                            case SEEN:
                                {
                                    read = true;
                                    break;
                                }
                            case DRAFT:
                            case RECENT:
                            case X_DESTROYED:
                            case X_DOWNLOADED_FULL:
                            case X_DOWNLOADED_PARTIAL:
                            case X_REMOTE_COPY_STARTED:
                            case X_SEND_FAILED:
                            case X_SEND_IN_PROGRESS:
                                {
                                    extraFlags.add(flag);
                                    break;
                                }
                        }
                    } catch (Exception e) {
                    // Ignore bad flags
                    }
                }
            }
            cv.put("flags", migrationsHelper.serializeFlags(extraFlags));
            cv.put("read", read);
            cv.put("flagged", flagged);
            cv.put("answered", answered);
            cv.put("forwarded", forwarded);
            db.update("messages", cv, "id = ?", new String[] { Long.toString(id) });
            cv.clear();
            extraFlags.clear();
        }
    } finally {
        cursor.close();
    }
    db.execSQL("CREATE INDEX IF NOT EXISTS msg_read ON messages (read)");
    db.execSQL("CREATE INDEX IF NOT EXISTS msg_flagged ON messages (flagged)");
}
Also used : ContentValues(android.content.ContentValues) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Flag(com.fsck.k9.mail.Flag)

Example 5 with MigrationsHelper

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

the class MigrationTo51 method updateFlagsForMessage.

private static void updateFlagsForMessage(SQLiteDatabase db, long messageId, String messageFlags, MigrationsHelper migrationsHelper) {
    List<Flag> extraFlags = new ArrayList<>();
    if (messageFlags != null && messageFlags.length() > 0) {
        String[] flags = messageFlags.split(",");
        for (String flagStr : flags) {
            try {
                Flag flag = Flag.valueOf(flagStr);
                extraFlags.add(flag);
            } catch (Exception e) {
            // Ignore bad flags
            }
        }
    }
    extraFlags.add(Flag.X_MIGRATED_FROM_V50);
    String flagsString = migrationsHelper.serializeFlags(extraFlags);
    db.execSQL("UPDATE messages SET flags = ? WHERE id = ?", new Object[] { flagsString, messageId });
}
Also used : ArrayList(java.util.ArrayList) Flag(com.fsck.k9.mail.Flag) IOException(java.io.IOException)

Aggregations

ContentValues (android.content.ContentValues)5 Account (com.fsck.k9.Account)5 LocalStore (com.fsck.k9.mailstore.LocalStore)4 LocalFolder (com.fsck.k9.mailstore.LocalFolder)3 Cursor (android.database.Cursor)2 SQLiteException (android.database.sqlite.SQLiteException)2 Flag (com.fsck.k9.mail.Flag)2 Folder (com.fsck.k9.mail.Folder)2 Storage (com.fsck.k9.preferences.Storage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Context (android.content.Context)1 FetchProfile (com.fsck.k9.mail.FetchProfile)1 MessagingException (com.fsck.k9.mail.MessagingException)1 MimeHeader (com.fsck.k9.mail.internet.MimeHeader)1 LocalMessage (com.fsck.k9.mailstore.LocalMessage)1 LockableDatabase (com.fsck.k9.mailstore.LockableDatabase)1 MigrationsHelper (com.fsck.k9.mailstore.MigrationsHelper)1 MessageFulltextCreator (com.fsck.k9.message.extractors.MessageFulltextCreator)1 StorageEditor (com.fsck.k9.preferences.StorageEditor)1