Skip to content

Commit

Permalink
Add FolderType support to (Local)Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
cketti committed Nov 13, 2018
1 parent fbf4f44 commit 91ae94d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@file:JvmName("FolderTypeConverter")
package com.fsck.k9.mailstore

import com.fsck.k9.mail.Folder.FolderType


@JvmName("fromDatabaseFolderType")
fun String.toFolderType(): FolderType {
return when (this) {
"regular" -> FolderType.REGULAR
"inbox" -> FolderType.INBOX
"outbox" -> FolderType.OUTBOX
"drafts" -> FolderType.DRAFTS
"sent" -> FolderType.SENT
"trash" -> FolderType.TRASH
"spam" -> FolderType.SPAM
"archive" -> FolderType.ARCHIVE
else -> throw AssertionError("Unknown folder type: $this")
}
}

fun FolderType.toDatabaseFolderType(): String {
return when (this) {
FolderType.REGULAR -> "regular"
FolderType.INBOX -> "inbox"
FolderType.OUTBOX -> "outbox"
FolderType.DRAFTS -> "drafts"
FolderType.SENT -> "sent"
FolderType.TRASH -> "trash"
FolderType.SPAM -> "spam"
FolderType.ARCHIVE -> "archive"
}
}
18 changes: 18 additions & 0 deletions app/core/src/main/java/com/fsck/k9/mailstore/LocalFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ public LocalFolder(LocalStore localStore, String serverId) {
}

public LocalFolder(LocalStore localStore, String serverId, String name) {
this(localStore, serverId, name, FolderType.REGULAR);
}

public LocalFolder(LocalStore localStore, String serverId, String name, FolderType type) {
this.localStore = localStore;
this.serverId = serverId;
this.name = name;
super.setType(type);
attachmentInfoExtractor = localStore.getAttachmentInfoExtractor();

if (getAccount().getInboxFolder().equals(getServerId())) {
Expand Down Expand Up @@ -218,6 +223,9 @@ void open(Cursor cursor) throws MessagingException {
moreMessages = MoreMessages.fromDatabaseName(moreMessagesValue);
name = cursor.getString(LocalStore.FOLDER_NAME_INDEX);
localOnly = cursor.getInt(LocalStore.LOCAL_ONLY_INDEX) == 1;
String typeString = cursor.getString(LocalStore.TYPE_INDEX);
FolderType folderType = FolderTypeConverter.fromDatabaseFolderType(typeString);
super.setType(folderType);
}

@Override
Expand Down Expand Up @@ -255,6 +263,16 @@ public void setName(String name) throws MessagingException {
updateFolderColumn("name", name);
}

@Override
public void setType(FolderType type) {
super.setType(type);
try {
updateFolderColumn("type", FolderTypeConverter.toDatabaseFolderType(type));
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

@Override
public boolean exists() throws MessagingException {
return this.localStore.getDatabase().execute(false, new DbCallback<Boolean>() {
Expand Down
9 changes: 6 additions & 3 deletions app/core/src/main/java/com/fsck/k9/mailstore/LocalStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public class LocalStore {
static final String GET_FOLDER_COLS =
"folders.id, name, visible_limit, last_updated, status, push_state, last_pushed, " +
"integrate, top_group, poll_class, push_class, display_class, notify_class, more_messages, server_id, " +
"local_only";
"local_only, type";

static final int FOLDER_ID_INDEX = 0;
static final int FOLDER_NAME_INDEX = 1;
Expand All @@ -150,6 +150,7 @@ public class LocalStore {
static final int MORE_MESSAGES_INDEX = 13;
static final int FOLDER_SERVER_ID_INDEX = 14;
static final int LOCAL_ONLY_INDEX = 15;
static final int TYPE_INDEX = 16;

static final String[] UID_CHECK_PROJECTION = { "uid" };

Expand Down Expand Up @@ -911,6 +912,7 @@ public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
String serverId = folder.getServerId();
String name = folder.getName();
boolean localOnly = folder.isLocalOnly();
String databaseFolderType = FolderTypeConverter.toDatabaseFolderType(folder.getType());

if (K9.DEVELOPER_MODE) {
Cursor cursor = db.query("folders", new String[] { "id", "server_id" },
Expand Down Expand Up @@ -953,7 +955,7 @@ public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
}
folder.refresh(serverId, prefHolder); // Recover settings from Preferences

db.execSQL("INSERT INTO folders (name, visible_limit, top_group, display_class, poll_class, notify_class, push_class, integrate, server_id, local_only) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] {
db.execSQL("INSERT INTO folders (name, visible_limit, top_group, display_class, poll_class, notify_class, push_class, integrate, server_id, local_only, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] {
name,
visibleLimit,
prefHolder.inTopGroup ? 1 : 0,
Expand All @@ -963,7 +965,8 @@ public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
prefHolder.pushClass.name(),
prefHolder.integrate ? 1 : 0,
serverId,
localOnly ? 1 : 0
localOnly ? 1 : 0,
databaseFolderType
});

}
Expand Down
20 changes: 20 additions & 0 deletions mail/common/src/main/java/com/fsck/k9/mail/Folder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public abstract class Folder<T extends Message> {
private String status = null;
private long lastChecked = 0;
private long lastPush = 0;
private FolderType type = FolderType.REGULAR;

public static final int OPEN_MODE_RW=0;
public static final int OPEN_MODE_RO=1;
Expand All @@ -22,6 +23,17 @@ public enum FolderClass {
NONE, NO_CLASS, INHERITED, FIRST_CLASS, SECOND_CLASS
}

public enum FolderType {
REGULAR,
INBOX,
OUTBOX,
DRAFTS,
SENT,
TRASH,
SPAM,
ARCHIVE
}

/**
* Forces an open of the MailProvider. If the provider is already open this
* function returns without doing anything.
Expand Down Expand Up @@ -201,4 +213,12 @@ public List<T> search(String queryString, final Set<Flag> requiredFlags, final S
throws MessagingException {
throw new MessagingException("K-9 does not support searches on this folder type");
}

public FolderType getType() {
return type;
}

public void setType(FolderType type) {
this.type = type;
}
}

0 comments on commit 91ae94d

Please sign in to comment.