Files
FairEmail/app/src/main/java/eu/faircode/email/EntityIdentity.java

217 lines
7.6 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-09-14 11:33:22 +00:00
import org.json.JSONException;
import org.json.JSONObject;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.room.Entity;
2018-08-08 10:22:12 +00:00
import androidx.room.ForeignKey;
import androidx.room.Index;
2018-08-08 06:55:47 +00:00
import androidx.room.PrimaryKey;
2018-08-02 13:33:06 +00:00
2018-08-08 10:22:12 +00:00
import static androidx.room.ForeignKey.CASCADE;
2018-08-02 13:33:06 +00:00
@Entity(
tableName = EntityIdentity.TABLE_NAME,
2018-08-08 10:22:12 +00:00
foreignKeys = {
@ForeignKey(childColumns = "account", entity = EntityAccount.class, parentColumns = "id", onDelete = CASCADE)
},
2018-08-02 13:33:06 +00:00
indices = {
2018-11-10 16:47:11 +00:00
@Index(value = {"account"}),
@Index(value = {"account", "email"})
2018-08-02 13:33:06 +00:00
}
)
public class EntityIdentity {
static final String TABLE_NAME = "identity";
@PrimaryKey(autoGenerate = true)
public Long id;
@NonNull
public String name;
@NonNull
public String email;
@NonNull
2018-08-08 10:22:12 +00:00
public Long account;
2018-12-26 10:54:48 +00:00
public String display;
public Integer color;
public String signature;
@NonNull
public Integer auth_type;
2018-08-08 10:22:12 +00:00
@NonNull
2018-08-02 13:33:06 +00:00
public String host; // SMTP
@NonNull
public Boolean starttls;
@NonNull
2018-10-23 07:52:20 +00:00
public Boolean insecure;
@NonNull
public Integer port;
@NonNull
2018-08-02 13:33:06 +00:00
public String user;
@NonNull
public String password;
2019-01-10 18:24:20 +00:00
public String realm;
2018-08-02 13:33:06 +00:00
@NonNull
2018-12-26 10:54:48 +00:00
public Boolean synchronize;
2018-08-27 14:31:45 +00:00
@NonNull
2018-08-02 13:33:06 +00:00
public Boolean primary;
2018-12-26 10:54:48 +00:00
public String replyto;
public String bcc;
2018-08-02 13:33:06 +00:00
@NonNull
public Boolean plain_only = false;
@NonNull
2019-01-19 18:13:48 +00:00
public Boolean delivery_receipt = false;
2018-12-26 10:54:48 +00:00
@NonNull
2019-01-19 18:13:48 +00:00
public Boolean read_receipt = false;
2018-08-25 14:24:26 +00:00
@NonNull
2019-02-17 16:35:01 +00:00
public Boolean store_sent = false;
public Long sent_folder = null; // obsolete
public Boolean tbd;
2018-08-15 11:26:59 +00:00
public String state;
public String error;
2019-01-25 18:13:00 +00:00
public Long last_connected;
2018-08-02 13:33:06 +00:00
2019-02-09 21:03:53 +00:00
String getProtocol() {
return (starttls ? "smtp" : "smtps");
}
2018-09-14 11:33:22 +00:00
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("id", id);
2018-09-14 11:33:22 +00:00
json.put("name", name);
json.put("email", email);
2018-12-26 10:54:48 +00:00
json.put("display", display);
if (color != null)
json.put("color", color);
json.put("signature", signature);
2018-11-10 10:45:03 +00:00
// not account
2018-12-26 10:54:48 +00:00
json.put("auth_type", auth_type);
2018-09-14 11:33:22 +00:00
json.put("host", host);
json.put("starttls", starttls);
2018-10-24 11:14:56 +00:00
json.put("insecure", insecure);
2018-11-10 10:45:03 +00:00
json.put("port", port);
2018-09-14 11:33:22 +00:00
json.put("user", user);
json.put("password", password);
json.put("realm", realm);
json.put("synchronize", synchronize);
2018-12-26 10:54:48 +00:00
json.put("primary", primary);
json.put("replyto", replyto);
json.put("bcc", bcc);
json.put("plain_only", plain_only);
2018-12-26 10:54:48 +00:00
json.put("delivery_receipt", delivery_receipt);
json.put("read_receipt", read_receipt);
2019-02-17 16:35:01 +00:00
json.put("store_sent", store_sent);
2018-11-10 10:45:03 +00:00
// not state
// not error
2018-09-14 11:33:22 +00:00
return json;
}
public static EntityIdentity fromJSON(JSONObject json) throws JSONException {
EntityIdentity identity = new EntityIdentity();
// id
2018-09-14 11:33:22 +00:00
identity.name = json.getString("name");
2018-12-26 10:54:48 +00:00
identity.email = json.getString("email");
if (json.has("display"))
identity.display = json.getString("display");
2018-12-26 10:54:48 +00:00
if (json.has("color"))
identity.color = json.getInt("color");
if (json.has("signature"))
identity.signature = json.getString("signature");
identity.auth_type = json.getInt("auth_type");
identity.host = json.getString("host");
identity.starttls = json.getBoolean("starttls");
identity.insecure = (json.has("insecure") && json.getBoolean("insecure"));
identity.port = json.getInt("port");
identity.user = json.getString("user");
identity.password = json.getString("password");
if (json.has("realm"))
identity.realm = json.getString("realm");
2018-12-26 10:54:48 +00:00
identity.synchronize = json.getBoolean("synchronize");
identity.primary = json.getBoolean("primary");
2018-12-07 20:25:16 +01:00
2018-09-14 11:33:22 +00:00
if (json.has("replyto"))
identity.replyto = json.getString("replyto");
if (json.has("bcc"))
identity.bcc = json.getString("bcc");
if (json.has("plain_only"))
identity.plain_only = json.getBoolean("plain_only");
2018-12-07 20:25:16 +01:00
if (json.has("delivery_receipt"))
identity.delivery_receipt = json.getBoolean("delivery_receipt");
if (json.has("read_receipt"))
identity.read_receipt = json.getBoolean("read_receipt");
2019-01-19 18:13:48 +00:00
2019-02-17 16:35:01 +00:00
if (json.has("store_sent"))
identity.store_sent = json.getBoolean("store_sent");
2018-09-14 11:33:22 +00:00
return identity;
}
2018-08-02 13:33:06 +00:00
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityIdentity) {
EntityIdentity other = (EntityIdentity) obj;
return (this.name.equals(other.name) &&
this.email.equals(other.email) &&
2018-08-08 10:22:12 +00:00
this.account.equals(other.account) &&
2018-12-26 10:54:48 +00:00
(this.display == null ? other.display == null : this.display.equals(other.display)) &&
(this.color == null ? other.color == null : this.color.equals(other.color)) &&
(this.signature == null ? other.signature == null : this.signature.equals(other.signature)) &&
2019-01-23 09:16:33 +00:00
this.auth_type.equals(other.auth_type) &&
2018-08-02 13:33:06 +00:00
this.host.equals(other.host) &&
this.starttls.equals(other.starttls) &&
2018-11-10 10:45:03 +00:00
this.insecure.equals(other.insecure) &&
this.port.equals(other.port) &&
2018-08-02 13:33:06 +00:00
this.user.equals(other.user) &&
this.password.equals(other.password) &&
2019-01-23 09:16:33 +00:00
(this.realm == null ? other.realm == null : this.realm.equals(other.realm)) &&
2018-08-15 11:26:59 +00:00
this.synchronize.equals(other.synchronize) &&
2018-12-26 10:54:48 +00:00
this.primary.equals(other.primary) &&
(this.replyto == null ? other.replyto == null : this.replyto.equals(other.replyto)) &&
2019-01-23 09:16:33 +00:00
(this.bcc == null ? other.bcc == null : this.bcc.equals(other.bcc)) &&
2018-12-26 10:54:48 +00:00
this.delivery_receipt.equals(other.delivery_receipt) &&
this.read_receipt.equals(other.read_receipt) &&
2019-02-17 16:35:01 +00:00
this.store_sent.equals(other.store_sent) &&
(this.tbd == null ? other.tbd == null : this.tbd.equals(other.tbd)) &&
2018-08-15 11:26:59 +00:00
(this.state == null ? other.state == null : this.state.equals(other.state)) &&
2019-01-25 18:13:00 +00:00
(this.error == null ? other.error == null : this.error.equals(other.error)) &&
(this.last_connected == null ? other.last_connected == null : this.last_connected.equals(other.last_connected)));
2018-08-02 13:33:06 +00:00
} else
return false;
}
String getDisplayName() {
return (display == null ? name : display);
}
2019-01-18 18:40:51 +00:00
@NonNull
@Override
public String toString() {
2019-01-21 18:37:09 +00:00
return getDisplayName() + " <" + email + ">" + (primary ? "" : "");
2019-01-18 18:40:51 +00:00
}
2018-08-02 13:33:06 +00:00
}