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.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
81
82 public void deleteUser(String name) {
83 for(int i = 0; i < users.size(); ++i) {
84 if (users.get(i).getName().equals(name)) {
85 users.remove(i);
86 return;
87 }
88 }
89 }
90
91 public void addUser(User user) {
92 for(int i = 0; i < users.size(); ++i) {
93 if (users.get(i).getName().equals(user.getName())) {
94 users.set(i, user);
95 return;
96 }
97 }
98 users.add(user);
99 }
100
101 public void processPacket(PresencePacket packet) {
102 Log.d("xmppService","process Packet for muc options: "+packet.toString());
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 Log.d("xmppService","nick: "+getNick());
113 Log.d("xmppService","name: "+name);
114 if (name.equals(getNick())) {
115 this.isOnline = true;
116 this.error = 0;
117 self = user;
118 } else {
119 addUser(user);
120 }
121 } else if (type.equals("unavailable")) {
122 Log.d("xmppService","name: "+name);
123 if (name.equals(getNick())) {
124 Element item = packet.findChild("x").findChild("item");
125 Log.d("xmppService","nick equals name");
126 String nick = item.getAttribute("nick");
127 if (nick!=null) {
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 this.error = ERROR_NICK_IN_USE;
139 }
140 }
141 }
142
143 public List<User> getUsers() {
144 return this.users;
145 }
146
147 public String getNick() {
148 String[] split = conversation.getContactJid().split("/");
149 if (split.length == 2) {
150 return split[1];
151 } else {
152 return conversation.getAccount().getUsername();
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}