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 User self = new User();
80 private String subject = null;
81
82
83 public void deleteUser(String name) {
84 for(int i = 0; i < users.size(); ++i) {
85 if (users.get(i).getName().equals(name)) {
86 users.remove(i);
87 return;
88 }
89 }
90 }
91
92 public void addUser(User user) {
93 for(int i = 0; i < users.size(); ++i) {
94 if (users.get(i).getName().equals(user.getName())) {
95 users.set(i, user);
96 return;
97 }
98 }
99 users.add(user);
100 }
101
102 public void processPacket(PresencePacket packet) {
103 String name = packet.getAttribute("from").split("/")[1];
104 String type = packet.getAttribute("type");
105 if (type==null) {
106 User user = new User();
107 Element item = packet.findChild("x").findChild("item");
108 user.setName(name);
109 user.setAffiliation(item.getAttribute("affiliation"));
110 user.setRole(item.getAttribute("role"));
111 user.setName(name);
112 if (name.equals(getNick())) {
113 this.isOnline = true;
114 this.error = 0;
115 self = user;
116 } else {
117 addUser(user);
118 }
119 } else if (type.equals("unavailable")) {
120 if (name.equals(getNick())) {
121 Element item = packet.findChild("x").findChild("item");
122 String nick = item.getAttribute("nick");
123 if (nick!=null) {
124 if (renameListener!=null) {
125 renameListener.onRename(true);
126 }
127 this.setNick(nick);
128 }
129 }
130 deleteUser(packet.getAttribute("from").split("/")[1]);
131 } else if (type.equals("error")) {
132 Element error = packet.findChild("error");
133 if (error.hasChild("conflict")) {
134 this.error = ERROR_NICK_IN_USE;
135 }
136 }
137 }
138
139 public List<User> getUsers() {
140 return this.users;
141 }
142
143 public String getNick() {
144 String[] split = conversation.getContactJid().split("/");
145 if (split.length == 2) {
146 return split[1];
147 } else {
148 if (conversation.getAccount()!=null) {
149 return conversation.getAccount().getUsername();
150 } else {
151 return null;
152 }
153 }
154 }
155
156 public void setNick(String nick) {
157 String jid = conversation.getContactJid().split("/")[0]+"/"+nick;
158 conversation.setContactJid(jid);
159 }
160
161 public void setConversation(Conversation conversation) {
162 this.conversation = conversation;
163 }
164
165 public boolean online() {
166 return this.isOnline;
167 }
168
169 public int getError() {
170 return this.error;
171 }
172
173 public void setOnRenameListener(OnRenameListener listener) {
174 this.renameListener = listener;
175 }
176
177 public OnRenameListener getOnRenameListener() {
178 return this.renameListener;
179 }
180
181 public void setOffline() {
182 this.users.clear();
183 this.error = 0;
184 this.isOnline = false;
185 }
186
187 public User getSelf() {
188 return self;
189 }
190
191 public void setSubject(String content) {
192 this.subject = content;
193 }
194
195 public String getSubject() {
196 return this.subject;
197 }
198}