1package de.gultsch.chat.entities;
2
3import org.json.JSONException;
4import org.json.JSONObject;
5
6import de.gultsch.chat.crypto.OtrEngine;
7import de.gultsch.chat.xmpp.XmppConnection;
8import android.content.ContentValues;
9import android.content.Context;
10import android.database.Cursor;
11import android.util.JsonReader;
12import android.util.Log;
13
14public class Account extends AbstractEntity{
15
16 private static final long serialVersionUID = 6174825093869578035L;
17
18 public static final String TABLENAME = "accounts";
19
20 public static final String USERNAME = "username";
21 public static final String SERVER = "server";
22 public static final String PASSWORD = "password";
23 public static final String OPTIONS = "options";
24 public static final String ROSTERVERSION = "rosterversion";
25 public static final String KEYS = "keys";
26
27 public static final int OPTION_USETLS = 0;
28 public static final int OPTION_DISABLED = 1;
29
30 public static final int STATUS_DISABLED = -1;
31 public static final int STATUS_OFFLINE = 0;
32 public static final int STATUS_ONLINE = 1;
33 public static final int STATUS_UNAUTHORIZED = 2;
34 public static final int STATUS_NOINTERNET = 3;
35 public static final int STATUS_TLS_ERROR = 4;
36 public static final int STATUS_SERVER_NOT_FOUND = 5;
37
38 protected String username;
39 protected String server;
40 protected String password;
41 protected int options = 0;
42 protected String rosterVersion;
43 protected String resource;
44 protected int status = 0;
45 protected JSONObject keys = new JSONObject();
46
47 protected boolean online = false;
48
49 transient OtrEngine otrEngine = null;
50 transient XmppConnection xmppConnection = null;
51
52 public Account() {
53 this.uuid = "0";
54 }
55
56 public Account(String username, String server, String password) {
57 this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
58 }
59 public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
60 this.uuid = uuid;
61 this.username = username;
62 this.server = server;
63 this.password = password;
64 this.options = options;
65 this.rosterVersion = rosterVersion;
66 try {
67 this.keys = new JSONObject(keys);
68 } catch (JSONException e) {
69
70 }
71 }
72
73 public boolean isOptionSet(int option) {
74 return ((options & (1 << option)) != 0);
75 }
76
77 public void setOption(int option, boolean value) {
78 if (value) {
79 this.options = (this.options | 1 << option);
80 } else {
81 this.options = (this.options & 0 << option);
82 }
83 }
84
85 public String getUsername() {
86 return username;
87 }
88
89 public void setUsername(String username) {
90 this.username = username;
91 }
92
93 public String getServer() {
94 return server;
95 }
96
97 public void setServer(String server) {
98 this.server = server;
99 }
100
101 public String getPassword() {
102 return password;
103 }
104
105 public void setPassword(String password) {
106 this.password = password;
107 }
108
109 public void setStatus(int status) {
110 this.status = status;
111 }
112
113 public int getStatus() {
114 if (isOptionSet(OPTION_DISABLED)) {
115 return STATUS_DISABLED;
116 } else {
117 return this.status;
118 }
119 }
120
121 public void setResource(String resource) {
122 this.resource = resource;
123 }
124
125 public String getJid() {
126 return username+"@"+server;
127 }
128
129 public JSONObject getKeys() {
130 return keys;
131 }
132
133 public void setKey(String keyName, String keyValue) throws JSONException {
134 this.keys.put(keyName, keyValue);
135 }
136
137 @Override
138 public ContentValues getContentValues() {
139 ContentValues values = new ContentValues();
140 values.put(UUID,uuid);
141 values.put(USERNAME, username);
142 values.put(SERVER, server);
143 values.put(PASSWORD, password);
144 values.put(OPTIONS,options);
145 values.put(KEYS,this.keys.toString());
146 values.put(ROSTERVERSION,rosterVersion);
147 return values;
148 }
149
150 public static Account fromCursor(Cursor cursor) {
151 return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
152 cursor.getString(cursor.getColumnIndex(USERNAME)),
153 cursor.getString(cursor.getColumnIndex(SERVER)),
154 cursor.getString(cursor.getColumnIndex(PASSWORD)),
155 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
156 cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
157 cursor.getString(cursor.getColumnIndex(KEYS))
158 );
159 }
160
161
162 public OtrEngine getOtrEngine(Context context) {
163 if (otrEngine==null) {
164 otrEngine = new OtrEngine(context,this);
165 }
166 return this.otrEngine;
167 }
168
169 public XmppConnection getXmppConnection() {
170 return this.xmppConnection;
171 }
172
173 public void setXmppConnection(XmppConnection connection) {
174 this.xmppConnection = connection;
175 }
176
177 public String getFullJid() {
178 return this.getJid()+"/"+this.resource;
179 }
180}