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[] fromParts = packet.getFrom().split("/");
105 if (fromParts.length>=2) {
106 String name = fromParts[1];
107 String type = packet.getAttribute("type");
108 if (type==null) {
109 User user = new User();
110 Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
111 user.setName(name);
112 user.setAffiliation(item.getAttribute("affiliation"));
113 user.setRole(item.getAttribute("role"));
114 user.setName(name);
115 if (name.equals(getNick())) {
116 this.isOnline = true;
117 this.error = 0;
118 self = user;
119 } else {
120 addUser(user);
121 }
122 } else if (type.equals("unavailable")) {
123 if (name.equals(getNick())) {
124 Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
125 String nick = item.getAttribute("nick");
126 if (nick!=null) {
127 aboutToRename = false;
128 if (renameListener!=null) {
129 renameListener.onRename(true);
130 }
131 this.setNick(nick);
132 }
133 }
134 deleteUser(packet.getAttribute("from").split("/")[1]);
135 } else if (type.equals("error")) {
136 Element error = packet.findChild("error");
137 if (error.hasChild("conflict")) {
138 if (aboutToRename) {
139 if (renameListener!=null) {
140 renameListener.onRename(false);
141 }
142 aboutToRename = false;
143 } else {
144 this.error = ERROR_NICK_IN_USE;
145 }
146 }
147 }
148 }
149 }
150
151 public List<User> getUsers() {
152 return this.users;
153 }
154
155 public String getNick() {
156 String[] split = conversation.getContactJid().split("/");
157 if (split.length == 2) {
158 return split[1];
159 } else {
160 if (conversation.getAccount()!=null) {
161 return conversation.getAccount().getUsername();
162 } else {
163 return null;
164 }
165 }
166 }
167
168 public void setNick(String nick) {
169 String jid = conversation.getContactJid().split("/")[0]+"/"+nick;
170 conversation.setContactJid(jid);
171 }
172
173 public void setConversation(Conversation conversation) {
174 this.conversation = conversation;
175 }
176
177 public boolean online() {
178 return this.isOnline;
179 }
180
181 public int getError() {
182 return this.error;
183 }
184
185 public void setOnRenameListener(OnRenameListener listener) {
186 this.renameListener = listener;
187 }
188
189 public OnRenameListener getOnRenameListener() {
190 return this.renameListener;
191 }
192
193 public void setOffline() {
194 this.users.clear();
195 this.error = 0;
196 this.isOnline = false;
197 }
198
199 public User getSelf() {
200 return self;
201 }
202
203 public void setSubject(String content) {
204 this.subject = content;
205 }
206
207 public String getSubject() {
208 return this.subject;
209 }
210
211 public void flagAboutToRename() {
212 this.aboutToRename = true;
213 }
214}