1package eu.siacs.conversations.entities;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import eu.siacs.conversations.entities.MucOptions.User;
7import eu.siacs.conversations.xml.Element;
8import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
9import android.annotation.SuppressLint;
10import android.util.Log;
11
12@SuppressLint("DefaultLocale")
13public class MucOptions {
14 public static final int ERROR_NICK_IN_USE = 1;
15
16 public interface OnRenameListener {
17 public void onRename(boolean success);
18 }
19
20 public class User {
21 public static final int ROLE_MODERATOR = 3;
22 public static final int ROLE_NONE = 0;
23 public static final int ROLE_PARTICIPANT = 2;
24 public static final int ROLE_VISITOR = 1;
25 public static final int AFFILIATION_ADMIN = 4;
26 public static final int AFFILIATION_OWNER = 3;
27 public static final int AFFILIATION_MEMBER = 2;
28 public static final int AFFILIATION_OUTCAST = 1;
29 public static final int AFFILIATION_NONE = 0;
30
31 private int role;
32 private int affiliation;
33 private String name;
34
35 public String getName() {
36 return name;
37 }
38 public void setName(String user) {
39 this.name = user;
40 }
41
42 public int getRole() {
43 return this.role;
44 }
45 public void setRole(String role) {
46 role = role.toLowerCase();
47 if (role.equals("moderator")) {
48 this.role = ROLE_MODERATOR;
49 } else if (role.equals("participant")) {
50 this.role = ROLE_PARTICIPANT;
51 } else if (role.equals("visitor")) {
52 this.role = ROLE_VISITOR;
53 } else {
54 this.role = ROLE_NONE;
55 }
56 }
57 public int getAffiliation() {
58 return this.affiliation;
59 }
60 public void setAffiliation(String affiliation) {
61 if (affiliation.equalsIgnoreCase("admin")) {
62 this.affiliation = AFFILIATION_ADMIN;
63 } else if (affiliation.equalsIgnoreCase("owner")) {
64 this.affiliation = AFFILIATION_OWNER;
65 } else if (affiliation.equalsIgnoreCase("member")) {
66 this.affiliation = AFFILIATION_MEMBER;
67 } else if (affiliation.equalsIgnoreCase("outcast")) {
68 this.affiliation = AFFILIATION_OUTCAST;
69 } else {
70 this.affiliation = AFFILIATION_NONE;
71 }
72 }
73 }
74 private ArrayList<User> users = new ArrayList<User>();
75 private Conversation conversation;
76 private boolean isOnline = false;
77 private int error = 0;
78 private OnRenameListener renameListener = null;
79 private boolean aboutToRename = false;
80 private User self = new User();
81 private String subject = null;
82
83
84 public void deleteUser(String name) {
85 for(int i = 0; i < users.size(); ++i) {
86 if (users.get(i).getName().equals(name)) {
87 users.remove(i);
88 return;
89 }
90 }
91 }
92
93 public void addUser(User user) {
94 for(int i = 0; i < users.size(); ++i) {
95 if (users.get(i).getName().equals(user.getName())) {
96 users.set(i, user);
97 return;
98 }
99 }
100 users.add(user);
101 }
102
103 public void processPacket(PresencePacket packet) {
104 String name = packet.getAttribute("from").split("/")[1];
105 String type = packet.getAttribute("type");
106 if (type==null) {
107 User user = new User();
108 Element item = packet.findChild("x").findChild("item");
109 user.setName(name);
110 user.setAffiliation(item.getAttribute("affiliation"));
111 user.setRole(item.getAttribute("role"));
112 user.setName(name);
113 if (name.equals(getNick())) {
114 this.isOnline = true;
115 this.error = 0;
116 self = user;
117 } else {
118 addUser(user);
119 }
120 } else if (type.equals("unavailable")) {
121 if (name.equals(getNick())) {
122 Element item = packet.findChild("x").findChild("item");
123 String nick = item.getAttribute("nick");
124 if (nick!=null) {
125 aboutToRename = false;
126 if (renameListener!=null) {
127 renameListener.onRename(true);
128 }
129 this.setNick(nick);
130 }
131 }
132 deleteUser(packet.getAttribute("from").split("/")[1]);
133 } else if (type.equals("error")) {
134 Element error = packet.findChild("error");
135 if (error.hasChild("conflict")) {
136 if (aboutToRename) {
137 if (renameListener!=null) {
138 renameListener.onRename(false);
139 }
140 aboutToRename = false;
141 } else {
142 this.error = ERROR_NICK_IN_USE;
143 }
144 }
145 }
146 }
147
148 public List<User> getUsers() {
149 return this.users;
150 }
151
152 public String getNick() {
153 String[] split = conversation.getContactJid().split("/");
154 if (split.length == 2) {
155 return split[1];
156 } else {
157 if (conversation.getAccount()!=null) {
158 return conversation.getAccount().getUsername();
159 } else {
160 return null;
161 }
162 }
163 }
164
165 public void setNick(String nick) {
166 String jid = conversation.getContactJid().split("/")[0]+"/"+nick;
167 conversation.setContactJid(jid);
168 }
169
170 public void setConversation(Conversation conversation) {
171 this.conversation = conversation;
172 }
173
174 public boolean online() {
175 return this.isOnline;
176 }
177
178 public int getError() {
179 return this.error;
180 }
181
182 public void setOnRenameListener(OnRenameListener listener) {
183 this.renameListener = listener;
184 }
185
186 public OnRenameListener getOnRenameListener() {
187 return this.renameListener;
188 }
189
190 public void setOffline() {
191 this.users.clear();
192 this.error = 0;
193 this.isOnline = false;
194 }
195
196 public User getSelf() {
197 return self;
198 }
199
200 public void setSubject(String content) {
201 this.subject = content;
202 }
203
204 public String getSubject() {
205 return this.subject;
206 }
207
208 public void flagAboutToRename() {
209 this.aboutToRename = true;
210 }
211}