FairEmail/app/src/main/java/eu/faircode/email/EntityFolder.java

333 lines
11 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2018-12-01 09:17:49 +00:00
import android.content.Context;
2018-09-14 11:33:22 +00:00
import org.json.JSONException;
import org.json.JSONObject;
2018-10-06 21:44:34 +00:00
import java.io.Serializable;
2018-11-15 07:20:48 +00:00
import java.text.Collator;
2018-08-02 13:33:06 +00:00
import java.util.Arrays;
2018-11-15 07:20:48 +00:00
import java.util.Collections;
import java.util.Comparator;
2018-08-02 13:33:06 +00:00
import java.util.List;
2018-11-15 07:20:48 +00:00
import java.util.Locale;
2018-08-02 13:33:06 +00:00
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import static androidx.room.ForeignKey.CASCADE;
2018-08-02 13:33:06 +00:00
@Entity(
tableName = EntityFolder.TABLE_NAME,
foreignKeys = {
@ForeignKey(childColumns = "account", entity = EntityAccount.class, parentColumns = "id", onDelete = CASCADE)
2018-08-02 13:33:06 +00:00
},
indices = {
@Index(value = {"account", "name"}, unique = true),
2018-08-02 13:33:06 +00:00
@Index(value = {"account"}),
@Index(value = {"name"}),
@Index(value = {"type"}),
@Index(value = {"unified"})
2018-08-02 13:33:06 +00:00
}
)
2018-10-06 21:44:34 +00:00
public class EntityFolder implements Serializable {
2018-08-02 13:33:06 +00:00
static final String TABLE_NAME = "folder";
@PrimaryKey(autoGenerate = true)
public Long id;
public Long account; // Outbox = null
@NonNull
public String name;
@NonNull
public String type;
@NonNull
2018-12-03 09:39:44 +00:00
public Integer level;
@NonNull
public Boolean synchronize;
@NonNull
public Boolean poll = false;
@NonNull
public Boolean download = true;
@NonNull
2018-11-14 09:49:59 +00:00
public Integer sync_days;
@NonNull
public Integer keep_days;
public String display;
@NonNull
public Boolean hide = false;
@NonNull
public Boolean unified = false;
@NonNull
public Boolean notify = false;
2018-11-26 08:12:44 +00:00
public String[] keywords;
2019-01-05 07:38:16 +00:00
@NonNull
public Boolean initialize = true;
2018-12-22 11:40:47 +00:00
public Boolean tbc; // to be created
public Boolean tbd; // to be deleted
2018-08-13 14:44:47 +00:00
public String state;
2018-12-03 12:41:36 +00:00
public String sync_state;
public String error;
2019-01-01 18:49:21 +00:00
public Long last_sync;
2018-08-09 07:02:41 +00:00
static final String INBOX = "Inbox";
static final String OUTBOX = "Outbox";
static final String ARCHIVE = "All";
static final String DRAFTS = "Drafts";
static final String TRASH = "Trash";
static final String JUNK = "Junk";
static final String SENT = "Sent";
static final String SYSTEM = "System";
2018-08-09 07:02:41 +00:00
static final String USER = "User";
2018-08-02 13:33:06 +00:00
2018-12-17 08:49:36 +00:00
// https://www.iana.org/assignments/imap-mailbox-name-attributes/imap-mailbox-name-attributes.xhtml
2019-02-01 21:06:14 +00:00
private static final List<String> SYSTEM_FOLDER_ATTR = Arrays.asList(
"All",
2018-12-17 08:49:36 +00:00
"Archive",
"Drafts",
"Trash",
"Junk",
"Sent",
"Important",
"Flagged"
2018-08-02 13:33:06 +00:00
);
2019-02-01 21:06:14 +00:00
private static final List<String> SYSTEM_FOLDER_TYPE = Arrays.asList(
2018-12-17 08:49:36 +00:00
ARCHIVE,
2018-08-09 07:02:41 +00:00
ARCHIVE,
DRAFTS,
TRASH,
JUNK,
SENT,
SYSTEM,
SYSTEM
); // MUST match SYSTEM_FOLDER_ATTR
2018-08-02 13:33:06 +00:00
2018-08-06 16:01:56 +00:00
static final List<String> FOLDER_SORT_ORDER = Arrays.asList(
2018-12-17 08:50:11 +00:00
INBOX,
2018-12-18 15:42:28 +00:00
OUTBOX,
2018-08-09 07:02:41 +00:00
DRAFTS,
SENT,
ARCHIVE,
TRASH,
JUNK,
SYSTEM,
2018-08-09 07:02:41 +00:00
USER
2018-08-06 16:01:56 +00:00
);
2019-01-01 13:37:03 +00:00
static final int DEFAULT_INIT = 14; // days
static final int DEFAULT_SYNC = 7; // days
static final int DEFAULT_KEEP = 30; // days
2018-08-06 20:14:11 +00:00
static final List<String> SYSTEM_FOLDER_SYNC = Arrays.asList(
2018-08-09 07:02:41 +00:00
DRAFTS,
2018-12-08 07:46:22 +00:00
SENT,
ARCHIVE,
2018-12-27 14:49:47 +00:00
TRASH,
JUNK
2018-08-06 20:14:11 +00:00
);
static final List<Boolean> SYSTEM_FOLDER_DOWNLOAD = Arrays.asList(
true, // drafts
false, // sent
false, // archive
false, // trash
false // junk
); // MUST match SYSTEM_FOLDER_SYNC
2018-08-06 20:14:11 +00:00
2018-08-15 09:50:39 +00:00
public EntityFolder() {
}
2019-01-21 18:54:33 +00:00
static int getIcon(String type) {
if (EntityFolder.INBOX.equals(type))
return R.drawable.baseline_move_to_inbox_24;
if (EntityFolder.ARCHIVE.equals(type))
return R.drawable.baseline_archive_24;
if (EntityFolder.TRASH.equals(type))
return R.drawable.baseline_delete_24;
if (EntityFolder.JUNK.equals(type))
return R.drawable.baseline_flag_24;
return R.drawable.baseline_folder_24;
}
2018-12-01 09:17:49 +00:00
String getDisplayName(Context context) {
return (display == null ? Helper.localizeFolderName(context, name) : display);
}
2018-11-11 07:07:41 +00:00
boolean isOutgoing() {
return isOutgoing(this.type);
}
static boolean isOutgoing(String type) {
return DRAFTS.equals(type) || OUTBOX.equals(type) || SENT.equals(type);
}
2019-02-01 21:06:14 +00:00
static String getType(String[] attrs, String fullName) {
for (String attr : attrs) {
if ("\\Noselect".equals(attr) || "\\NonExistent".equals(attr))
return null;
if (attr.startsWith("\\")) {
int index = SYSTEM_FOLDER_ATTR.indexOf(attr.substring(1));
if (index >= 0)
return SYSTEM_FOLDER_TYPE.get(index);
}
}
// https://tools.ietf.org/html/rfc3501#section-5.1
if ("INBOX".equals(fullName.toUpperCase()))
return INBOX;
return USER;
}
2018-12-04 08:01:08 +00:00
static int getLevel(Character separator, String name) {
int level = 0;
2018-12-22 15:46:54 +00:00
if (separator != null) {
2018-12-04 08:01:08 +00:00
for (int i = 0; i < name.length(); i++)
if (name.charAt(i) == separator)
level++;
2018-12-22 15:46:54 +00:00
if (name.startsWith("INBOX" + separator))
level--;
}
2018-12-04 08:01:08 +00:00
return level;
}
2018-08-02 13:33:06 +00:00
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityFolder) {
EntityFolder other = (EntityFolder) obj;
2018-08-15 19:34:22 +00:00
return (this.id.equals(other.id) &&
(this.account == null ? other.account == null : this.account.equals(other.account)) &&
2018-08-02 13:33:06 +00:00
this.name.equals(other.name) &&
this.type.equals(other.type) &&
2018-12-03 09:39:44 +00:00
this.level.equals(other.level) &&
2018-08-02 13:33:06 +00:00
this.synchronize.equals(other.synchronize) &&
this.poll.equals(other.poll) &&
2019-01-05 07:38:16 +00:00
this.download.equals(other.download) &&
this.sync_days.equals(other.sync_days) &&
this.keep_days.equals(other.keep_days) &&
(this.display == null ? other.display == null : this.display.equals(other.display)) &&
this.hide == other.hide &&
this.unified == other.unified &&
this.notify == other.notify &&
Helper.equal(this.keywords, other.keywords) &&
(this.tbd == null ? other.tbd == null : this.tbd.equals(other.tbd)) &&
2018-08-13 14:44:47 +00:00
(this.state == null ? other.state == null : this.state.equals(other.state)) &&
2018-12-03 12:41:36 +00:00
(this.sync_state == null ? other.sync_state == null : this.sync_state.equals(other.sync_state)) &&
2019-01-01 18:49:21 +00:00
(this.error == null ? other.error == null : this.error.equals(other.error)) &&
(this.last_sync == null ? other.last_sync == null : this.last_sync.equals(other.last_sync)));
2018-08-02 13:33:06 +00:00
} else
return false;
}
2018-08-10 09:45:36 +00:00
@Override
public String toString() {
2019-01-18 11:15:07 +00:00
return (display == null ? name : display);
2018-08-10 09:45:36 +00:00
}
2018-08-15 09:50:39 +00:00
2018-09-14 11:33:22 +00:00
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("name", name);
json.put("type", type);
2018-12-03 09:39:44 +00:00
json.put("level", level);
2018-09-14 11:33:22 +00:00
json.put("synchronize", synchronize);
json.put("poll", poll);
2019-01-05 07:38:16 +00:00
json.put("download", download);
2018-11-14 09:49:59 +00:00
json.put("sync_days", sync_days);
json.put("keep_days", keep_days);
2018-10-06 21:36:11 +00:00
json.put("display", display);
json.put("hide", hide);
json.put("unified", unified);
json.put("notify", notify);
2018-09-14 11:33:22 +00:00
return json;
}
public static EntityFolder fromJSON(JSONObject json) throws JSONException {
EntityFolder folder = new EntityFolder();
folder.name = json.getString("name");
folder.type = json.getString("type");
2018-12-03 09:39:44 +00:00
if (json.has("level"))
folder.level = json.getInt("level");
2018-12-04 08:01:08 +00:00
else
folder.level = 0;
2018-12-03 09:39:44 +00:00
2018-09-14 11:33:22 +00:00
folder.synchronize = json.getBoolean("synchronize");
2019-01-05 07:38:16 +00:00
if (json.has("poll"))
folder.poll = json.getBoolean("poll");
2019-01-05 07:38:16 +00:00
if (json.has("download"))
folder.download = json.getBoolean("download");
2018-11-14 09:49:59 +00:00
if (json.has("after"))
folder.sync_days = json.getInt("after");
else
folder.sync_days = json.getInt("sync_days");
if (json.has("keep_days"))
folder.keep_days = json.getInt("keep_days");
else
folder.keep_days = folder.sync_days;
2018-10-06 21:36:11 +00:00
if (json.has("display"))
folder.display = json.getString("display");
2018-10-06 21:36:11 +00:00
if (json.has("hide"))
folder.hide = json.getBoolean("hide");
2018-10-06 21:36:11 +00:00
folder.unified = json.getBoolean("unified");
if (json.has("notify"))
folder.notify = json.getBoolean("notify");
2018-09-14 11:33:22 +00:00
return folder;
}
2018-11-15 07:20:48 +00:00
2018-12-22 15:13:52 +00:00
static void sort(final Context context, List<EntityFolder> folders) {
2018-11-15 07:20:48 +00:00
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
2018-12-22 10:44:44 +00:00
int i1 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type);
int i2 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type);
int s = Integer.compare(i1, i2);
2018-11-15 07:20:48 +00:00
if (s != 0)
return s;
int c = -f1.synchronize.compareTo(f2.synchronize);
if (c != 0)
return c;
2018-12-22 15:13:52 +00:00
String name1 = f1.getDisplayName(context);
String name2 = f2.getDisplayName(context);
return collator.compare(name1, name2);
2018-11-15 07:20:48 +00:00
}
});
}
2018-08-02 13:33:06 +00:00
}