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

165 lines
5.9 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
Copyright 2018 by Marcel Bokhorst (M66B)
*/
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;
public String replyto;
@NonNull
2018-08-08 10:22:12 +00:00
public Long account;
@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;
@NonNull
2018-08-27 14:31:45 +00:00
public Integer auth_type;
@NonNull
2018-08-02 13:33:06 +00:00
public Boolean primary;
2018-11-10 16:47:11 +00:00
public Integer color;
2018-11-14 14:51:50 +01:00
public String signature;
2018-08-02 13:33:06 +00:00
@NonNull
public Boolean synchronize;
2018-08-25 14:24:26 +00:00
@NonNull
public Boolean store_sent;
2018-11-13 10:05:33 +01:00
public Long sent_folder;
public Boolean tbd;
2018-08-15 11:26:59 +00:00
public String state;
public String error;
2018-08-02 13:33:06 +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("email", email);
json.put("replyto", replyto);
2018-11-10 10:45:03 +00:00
// not account
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);
2018-09-14 11:33:22 +00:00
json.put("auth_type", auth_type);
json.put("primary", primary);
2018-11-10 16:47:11 +00:00
if (color != null)
json.put("color", color);
2018-11-14 14:51:50 +01:00
json.put("signature", signature);
json.put("synchronize", synchronize);
2018-09-14 11:33:22 +00:00
json.put("store_sent", store_sent);
2018-11-13 10:05:33 +01:00
if (sent_folder != null)
json.put("sent_folder", sent_folder);
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();
identity.name = json.getString("name");
identity.email = json.getString("email");
if (json.has("replyto"))
identity.replyto = json.getString("replyto");
identity.host = json.getString("host");
identity.starttls = json.getBoolean("starttls");
2018-10-24 11:14:56 +00:00
identity.insecure = (json.has("insecure") && json.getBoolean("insecure"));
2018-11-10 10:45:03 +00:00
identity.port = json.getInt("port");
2018-09-14 11:33:22 +00:00
identity.user = json.getString("user");
identity.password = json.getString("password");
identity.auth_type = json.getInt("auth_type");
identity.primary = json.getBoolean("primary");
2018-11-10 16:47:11 +00:00
if (json.has("color"))
identity.color = json.getInt("color");
2018-11-14 14:51:50 +01:00
if (json.has("signature"))
identity.signature = json.getString("signature");
2018-09-14 11:33:22 +00:00
identity.synchronize = json.getBoolean("synchronize");
identity.store_sent = json.getBoolean("store_sent");
2018-11-13 10:05:33 +01:00
if (json.has("sent_folder"))
identity.sent_folder = json.getLong("sent_folder");
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-04 19:54:33 +00:00
(this.replyto == null ? other.replyto == null : this.replyto.equals(other.replyto)) &&
2018-08-08 10:22:12 +00:00
this.account.equals(other.account) &&
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) &&
this.primary.equals(other.primary) &&
2018-11-10 16:47:11 +00:00
(this.color == null ? other.color == null : this.color.equals(other.color)) &&
2018-11-14 14:51:50 +01:00
(this.signature == null ? other.signature == null : this.signature.equals(other.signature)) &&
2018-08-15 11:26:59 +00:00
this.synchronize.equals(other.synchronize) &&
2018-08-25 14:24:26 +00:00
this.store_sent.equals(other.store_sent) &&
2018-11-13 10:05:33 +01:00
(this.sent_folder == null ? other.sent_folder == null : this.sent_folder.equals(other.sent_folder)) &&
(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)) &&
(this.error == null ? other.error == null : this.error.equals(other.error)));
2018-08-02 13:33:06 +00:00
} else
return false;
}
@Override
public String toString() {
2018-08-06 09:50:53 +00:00
return name + (primary ? "" : "");
2018-08-02 13:33:06 +00:00
}
}