MucOptions.java

  1package eu.siacs.conversations.entities;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5
  6import eu.siacs.conversations.xml.Element;
  7import eu.siacs.conversations.xmpp.PresencePacket;
  8import android.annotation.SuppressLint;
  9import android.util.Log;
 10
 11@SuppressLint("DefaultLocale")
 12public class MucOptions {
 13	public static final int ERROR_NICK_IN_USE = 1;
 14	
 15	public interface OnRenameListener {
 16		public void onRename(boolean success);
 17	}
 18	
 19	public class User {
 20		public static final int ROLE_MODERATOR = 3;
 21		public static final int ROLE_NONE = 0;
 22		public static final int ROLE_PARTICIPANT = 2;
 23		public static final int ROLE_VISITOR = 1;
 24		public static final int AFFILIATION_ADMIN = 4;
 25		public static final int AFFILIATION_OWNER = 3;
 26		public static final int AFFILIATION_MEMBER = 2;
 27		public static final int AFFILIATION_OUTCAST = 1;
 28		public static final int AFFILIATION_NONE = 0;
 29		
 30		private int role;
 31		private int affiliation;
 32		private String name;
 33		
 34		public String getName() {
 35			return name;
 36		}
 37		public void setName(String user) {
 38			this.name = user;
 39		}
 40		
 41		public int getRole() {
 42			return this.role;
 43		}
 44		public void setRole(String role) {
 45			role = role.toLowerCase();
 46			if (role.equals("moderator")) {
 47				this.role = ROLE_MODERATOR;
 48			} else if (role.equals("participant")) {
 49				this.role = ROLE_PARTICIPANT;
 50			} else if (role.equals("visitor")) {
 51				this.role = ROLE_VISITOR;
 52			} else {
 53				this.role = ROLE_NONE;
 54			}
 55		}
 56		public int getAffiliation() {
 57			return this.affiliation;
 58		}
 59		public void setAffiliation(String affiliation) {
 60			if (affiliation.equalsIgnoreCase("admin")) {
 61				this.affiliation = AFFILIATION_ADMIN;
 62			} else if (affiliation.equalsIgnoreCase("owner")) {
 63				this.affiliation = AFFILIATION_OWNER;
 64			} else if (affiliation.equalsIgnoreCase("member")) {
 65				this.affiliation = AFFILIATION_MEMBER;
 66			} else if (affiliation.equalsIgnoreCase("outcast")) {
 67				this.affiliation = AFFILIATION_OUTCAST;
 68			} else {
 69				this.affiliation = AFFILIATION_NONE;
 70			}
 71		}
 72	}
 73	private ArrayList<User> users = new ArrayList<User>();
 74	private Conversation conversation;
 75	private boolean isOnline = false;
 76	private int error = 0;
 77	private OnRenameListener renameListener = null;
 78
 79	
 80	public void deleteUser(String name) {
 81		for(int i = 0; i < users.size(); ++i) {
 82			if (users.get(i).getName().equals(name)) {
 83				users.remove(i);
 84				return;
 85			}
 86		}
 87	}
 88	
 89	public void addUser(User user) {
 90		for(int i = 0; i < users.size(); ++i) {
 91			if (users.get(i).getName().equals(user.getName())) {
 92				users.set(i, user);
 93				return;
 94			}
 95		}
 96		users.add(user);
 97		}
 98	
 99	public void processPacket(PresencePacket packet) {
100		Log.d("xmppService","process Packet for muc options: "+packet.toString());
101		String name = packet.getAttribute("from").split("/")[1];
102			String type = packet.getAttribute("type");
103			if (type==null) {
104				User user = new User();
105				Element item = packet.findChild("x").findChild("item");
106				user.setName(name);
107				user.setAffiliation(item.getAttribute("affiliation"));
108				user.setRole(item.getAttribute("role"));
109				user.setName(name);
110				addUser(user);
111				Log.d("xmppService","nick: "+getNick());
112				Log.d("xmppService","name: "+name);
113				if (name.equals(getNick())) {
114					this.isOnline = true;
115					this.error = 0;
116				}
117			} else if (type.equals("unavailable")) {
118				Log.d("xmppService","name: "+name);
119				if (name.equals(getNick())) {
120					Element item = packet.findChild("x").findChild("item");
121					Log.d("xmppService","nick equals name");
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			return conversation.getAccount().getUsername();
149		}
150	}
151	
152	public void setNick(String nick) {
153		String jid = conversation.getContactJid().split("/")[0]+"/"+nick;
154		conversation.setContactJid(jid);
155	}
156	
157	public void setConversation(Conversation conversation) {
158		this.conversation = conversation;
159	}
160	
161	public boolean online() {
162		return this.isOnline;
163	}
164	
165	public int getError() {
166		return this.error;
167	}
168
169	public void setOnRenameListener(OnRenameListener listener) {
170		this.renameListener = listener;
171	}
172	
173	public OnRenameListener getOnRenameListener() {
174		return this.renameListener;
175	}
176
177	public void setOffline() {
178		this.users.clear();
179		this.error = 0;
180		this.isOnline = false;
181	}
182}