MucOptions.java

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