diff --git a/app/src/main/java/eu/faircode/email/EntityFolder.java b/app/src/main/java/eu/faircode/email/EntityFolder.java index c9623d9fa8..485f4114a2 100644 --- a/app/src/main/java/eu/faircode/email/EntityFolder.java +++ b/app/src/main/java/eu/faircode/email/EntityFolder.java @@ -37,8 +37,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Objects; import static androidx.room.ForeignKey.CASCADE; @@ -157,10 +159,21 @@ public class EntityFolder extends EntityOrder implements Serializable { USER )); + private static Map GUESS_FOLDER_TYPE = new HashMap() {{ + put("all", EntityFolder.ARCHIVE); + put("archive", EntityFolder.ARCHIVE); + put("draft", EntityFolder.DRAFTS); + put("concept", EntityFolder.DRAFTS); + put("trash", EntityFolder.TRASH); + put("junk", EntityFolder.JUNK); + put("spam", EntityFolder.JUNK); + put("sent", EntityFolder.SENT); + }}; + static final int DEFAULT_SYNC = 7; // days static final int DEFAULT_KEEP = 30; // days - static final List SYSTEM_FOLDER_SYNC = Collections.unmodifiableList(Arrays.asList( + private static final List SYSTEM_FOLDER_SYNC = Collections.unmodifiableList(Arrays.asList( INBOX, DRAFTS, SENT, @@ -168,7 +181,7 @@ public class EntityFolder extends EntityOrder implements Serializable { TRASH, JUNK )); - static final List SYSTEM_FOLDER_DOWNLOAD = Collections.unmodifiableList(Arrays.asList( + private static final List SYSTEM_FOLDER_DOWNLOAD = Collections.unmodifiableList(Arrays.asList( true, // inbox true, // drafts false, // sent @@ -180,6 +193,23 @@ public class EntityFolder extends EntityOrder implements Serializable { public EntityFolder() { } + public EntityFolder(String fullName, String type) { + this.name = fullName; + this.type = type; + + int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type); + this.synchronize = (sync >= 0); + this.download = (sync < 0 || EntityFolder.SYSTEM_FOLDER_DOWNLOAD.get(sync)); + + this.sync_days = EntityFolder.DEFAULT_SYNC; + this.keep_days = EntityFolder.DEFAULT_KEEP; + + if (EntityFolder.INBOX.equals(type)) { + this.unified = true; + this.notify = true; + } + } + static String getNotificationChannelId(long id) { return "notification.folder." + id; } @@ -271,6 +301,13 @@ public class EntityFolder extends EntityOrder implements Serializable { return USER; } + static String guessType(String fullName) { + for (String guess : GUESS_FOLDER_TYPE.keySet()) + if (fullName.toLowerCase().contains(guess)) + return GUESS_FOLDER_TYPE.get(guess); + return null; + } + String getParentName(Character separator) { if (separator == null) return null; diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index 17ef710f47..48a0e23cda 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -587,16 +587,8 @@ public class FragmentAccount extends FragmentBase { result.idle = iservice.getStore().hasCapability("IDLE"); boolean inbox = false; - boolean archive = false; - boolean drafts = false; - boolean trash = false; - boolean sent = false; - boolean junk = false; - EntityFolder altArchive = null; - EntityFolder altDrafts = null; - EntityFolder altTrash = null; - EntityFolder altSent = null; - EntityFolder altJunk = null; + + List guesses = new ArrayList<>(); for (Folder ifolder : iservice.getStore().getDefaultFolder().list("*")) { // Check folder attributes @@ -608,69 +600,48 @@ public class FragmentAccount extends FragmentBase { if (type != null) { // Create entry EntityFolder folder = db.folder().getFolderByName(id, fullName); - if (folder == null) { - int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type); - folder = new EntityFolder(); - folder.name = fullName; - folder.type = type; - folder.synchronize = (sync >= 0); - folder.download = (sync < 0 ? true : EntityFolder.SYSTEM_FOLDER_DOWNLOAD.get(sync)); - folder.sync_days = EntityFolder.DEFAULT_SYNC; - folder.keep_days = EntityFolder.DEFAULT_KEEP; - } + if (folder == null) + folder = new EntityFolder(fullName, type); result.folders.add(folder); if (EntityFolder.USER.equals(type)) { - if (folder.name.toLowerCase().contains("archive")) - altArchive = folder; - if (folder.name.toLowerCase().contains("draft")) - altDrafts = folder; - if (folder.name.toLowerCase().contains("trash")) - altTrash = folder; - if (folder.name.toLowerCase().contains("sent")) - altSent = folder; - if (folder.name.toLowerCase().contains("junk")) - altJunk = folder; - } else { - if (EntityFolder.INBOX.equals(type)) - inbox = true; - else if (EntityFolder.ARCHIVE.equals(type)) - archive = true; - else if (EntityFolder.DRAFTS.equals(type)) - drafts = true; - else if (EntityFolder.TRASH.equals(type)) - trash = true; - else if (EntityFolder.SENT.equals(type)) - sent = true; - else if (EntityFolder.JUNK.equals(type)) - junk = true; + String guess = EntityFolder.guessType(fullName); + if (guess != null) + guesses.add(folder); } - if (EntityFolder.INBOX.equals(type)) + if (EntityFolder.INBOX.equals(type)) { + inbox = true; + result.utf8 = (Boolean) ((IMAPFolder) ifolder).doCommand(new IMAPFolder.ProtocolCommand() { @Override public Object doCommand(IMAPProtocol protocol) { return protocol.supportsUtf8(); } }); + } Log.i(folder.name + " id=" + folder.id + " type=" + folder.type + " attr=" + TextUtils.join(",", attrs)); } } + for (EntityFolder guess : guesses) { + boolean has = false; + String gtype = EntityFolder.guessType(guess.name); + for (EntityFolder folder : result.folders) + if (folder.type.equals(gtype)) { + has = true; + break; + } + if (!has) { + guess.type = gtype; + Log.i(guess.name + " guessed type=" + gtype); + } + } + if (!inbox) throw new IllegalArgumentException(context.getString(R.string.title_no_inbox)); - if (!archive && altArchive != null) - altArchive.type = EntityFolder.ARCHIVE; - if (!drafts && altDrafts != null) - altDrafts.type = EntityFolder.DRAFTS; - if (!trash && altTrash != null) - altTrash.type = EntityFolder.TRASH; - if (!sent && altSent != null) - altSent.type = EntityFolder.SENT; - if (!junk && altJunk != null) - altJunk.type = EntityFolder.JUNK; if (result.folders.size() > 0) Collections.sort(result.folders, result.folders.get(0).getComparator(null)); diff --git a/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java b/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java index a44de3f494..04d40c5dc4 100644 --- a/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java @@ -275,9 +275,8 @@ public class FragmentQuickSetup extends FragmentBase { throw ex; } - boolean inbox = false; - boolean drafts = false; - EntityFolder altDrafts = null; + List guesses = new ArrayList<>(); + for (Folder ifolder : iservice.getStore().getDefaultFolder().list("*")) { String fullName = ifolder.getFullName(); String[] attrs = ((IMAPFolder) ifolder).getAttributes(); @@ -285,47 +284,41 @@ public class FragmentQuickSetup extends FragmentBase { Log.i(fullName + " attrs=" + TextUtils.join(" ", attrs) + " type=" + type); if (type != null) { - int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type); - - EntityFolder folder = new EntityFolder(); - folder.name = fullName; - folder.type = type; - folder.synchronize = (sync >= 0); - folder.download = (sync < 0 ? true : EntityFolder.SYSTEM_FOLDER_DOWNLOAD.get(sync)); - folder.sync_days = EntityFolder.DEFAULT_SYNC; - folder.keep_days = EntityFolder.DEFAULT_KEEP; - - if (EntityFolder.INBOX.equals(type)) { - folder.unified = true; - folder.notify = true; - inbox = true; - } else if (EntityFolder.DRAFTS.equals(type)) - drafts = true; - + EntityFolder folder = new EntityFolder(fullName, type); folders.add(folder); - } else if (fullName.toLowerCase().contains("draft")) { - int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(EntityFolder.DRAFTS); - - EntityFolder folder = new EntityFolder(); - folder.name = fullName; - folder.type = EntityFolder.DRAFTS; - folder.synchronize = (sync >= 0); - folder.download = (sync < 0 ? true : EntityFolder.SYSTEM_FOLDER_DOWNLOAD.get(sync)); - folder.sync_days = EntityFolder.DEFAULT_SYNC; - folder.keep_days = EntityFolder.DEFAULT_KEEP; - - altDrafts = folder; + + if (EntityFolder.USER.equals(type)) { + String guess = EntityFolder.guessType(fullName); + if (guess != null) + guesses.add(folder); + } } } - Log.i("Quick inbox=" + inbox + " drafts=" + drafts); - - if (!drafts && altDrafts != null) { - drafts = true; - folders.add(altDrafts); - Log.i("Quick alt drafts=" + altDrafts.name); + for (EntityFolder guess : guesses) { + boolean has = false; + String gtype = EntityFolder.guessType(guess.name); + for (EntityFolder folder : folders) + if (folder.type.equals(gtype)) { + has = true; + break; + } + if (!has) { + guess.type = gtype; + Log.i(guess.name + " guessed type=" + gtype); + } } + boolean inbox = false; + boolean drafts = false; + for (EntityFolder folder : folders) + if (EntityFolder.INBOX.equals(folder.type)) + inbox = true; + else if (EntityFolder.DRAFTS.equals(folder.type)) + drafts = true; + + Log.i("Quick inbox=" + inbox + " drafts=" + drafts); + if (!inbox || !drafts) throw new IllegalArgumentException( context.getString(R.string.title_setup_no_settings, dparts[1]));