/*
* File : MUCUtils.java
* Created : 01.01.2006
* By : kahless
*
* GOIM - Gamers Own Instant Messenger
* Copyright (C) 2005 Herbert Poul (kahless@sphene.net / herbert.poul@gmail.com)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.xmpp.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sphene.goim.rcp.beans.GOIMAccount;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.muc.MultiUserChat;
public class MUCUtils {
public static final String MUC_AUTOJOIN_LIST = "muc_autojoin_list";
public static boolean isOnAutoJoinList(GOIMAccount account, String roomname) {
List<MUCRoom> channels = getAutoJoinList(account);
if(channels == null) return false;
for(MUCRoom room : channels)
if(room.name.equals(roomname)) return true;
return false;
}
public static void addToAutoJoinList(GOIMAccount account, String roomname) {
List<MUCRoom> channels = getAutoJoinList(account);
if(channels == null) account.storePreferenceValue(MUC_AUTOJOIN_LIST,channels = new ArrayList<MUCRoom>());
if(!channels.contains(roomname))
channels.add(new MUCRoom(roomname));
System.out.println("Added");
account.fireChangeEvent();
}
public static void removeFromAutoJoinList(GOIMAccount account, String roomname) {
List<MUCRoom> channels = getAutoJoinList(account);
if(channels == null) return;
Iterator<MUCRoom> i = channels.iterator();
while(i.hasNext()) {
if(i.next().name.equals(roomname)) {
System.out.println("Remove");
i.remove();
account.fireChangeEvent();
return;
}
}
}
@SuppressWarnings("unchecked")
public static List<MUCRoom> getAutoJoinList(GOIMAccount account) {
List<MUCRoom> channels = (List<MUCRoom>)account.getPreferenceValue(MUC_AUTOJOIN_LIST);
return channels;
}
public static void joinMUCRoom(GOIMAccount account, String room, String password) {
MultiUserChat muc = new MultiUserChat(account.xmpp.getConnection(),room);
account.chatWindowExtensionManager.openMUCWindow(room, muc);
try {
muc.join(account.getUsername(),password);
} catch (XMPPException e) {
//ErrorDialog.openError(PlatformUI.window.getShell(),"Error while Joining room","Error while trying to join Room: " + e.toString(),new Status(Status.ERROR,GOIMPlugin.ID,Status.OK,"Error while trying to join MUC room",e));
e.printStackTrace();
String msg = "Error while Joining MUC Channel " + room;
if(e.getXMPPError() != null) {
msg = msg + " - " + JabberErrorConditionMapping.getMeaning(e.getXMPPError().getCode());
if(e.getXMPPError().getMessage() != null) {
msg = msg + ": " + e.getXMPPError().getMessage();
}
}
throw new RuntimeException(msg,e);
}
account.chatWindowExtensionManager.openMUCWindow(room,muc);
}
public static void joinMUCRoom(GOIMAccount account, MUCRoom room) {
joinMUCRoom(account,room.name,room.password);
}
public static class MUCRoom implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public String password;
public MUCRoom(String name) { this.name = name; }
public boolean equals(Object obj) {
return (obj instanceof MUCRoom && ((MUCRoom)obj).name.equals(name)) ||
(obj instanceof String && name.equals(obj));
}
}
}