/******************************************************************************
* Copyright (c) 2008 Savino Sguera. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this
* distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Contributors:
* Savino Sguera < s.sguera@ieee.org >
******************************************************************************
*/
package org.eclipse.twipse.ecf;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.IContainerListener;
import org.eclipse.ecf.core.events.ContainerConnectedEvent;
import org.eclipse.ecf.core.events.ContainerConnectingEvent;
import org.eclipse.ecf.core.events.IContainerEvent;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.core.user.IUser;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.presence.IAccountManager;
import org.eclipse.ecf.presence.IIMMessageListener;
import org.eclipse.ecf.presence.IPresence;
import org.eclipse.ecf.presence.IPresenceContainerAdapter;
import org.eclipse.ecf.presence.IPresenceListener;
import org.eclipse.ecf.presence.IPresenceSender;
import org.eclipse.ecf.presence.Presence;
import org.eclipse.ecf.presence.chatroom.IChatRoomManager;
import org.eclipse.ecf.presence.history.IHistory;
import org.eclipse.ecf.presence.history.IHistoryManager;
import org.eclipse.ecf.presence.im.ChatMessage;
import org.eclipse.ecf.presence.im.ChatMessageEvent;
import org.eclipse.ecf.presence.im.IChat;
import org.eclipse.ecf.presence.im.IChatManager;
import org.eclipse.ecf.presence.im.IChatMessageSender;
import org.eclipse.ecf.presence.im.ITypingMessageSender;
import org.eclipse.ecf.presence.im.TypingMessage;
import org.eclipse.ecf.presence.im.TypingMessageEvent;
import org.eclipse.ecf.presence.im.IChatMessage.Type;
import org.eclipse.ecf.presence.roster.IRoster;
import org.eclipse.ecf.presence.roster.IRosterEntry;
import org.eclipse.ecf.presence.roster.IRosterItem;
import org.eclipse.ecf.presence.roster.IRosterListener;
import org.eclipse.ecf.presence.roster.IRosterManager;
import org.eclipse.ecf.presence.roster.IRosterSubscriptionListener;
import org.eclipse.ecf.presence.roster.IRosterSubscriptionSender;
import org.eclipse.ecf.presence.roster.RosterEntry;
import org.eclipse.ecf.presence.service.IPresenceService;
import org.eclipse.twipse.TwipsePlugin;
import org.eclipse.twipse.core.ITwitterClient;
import org.eclipse.twipse.core.TwitterClient;
import org.eclipse.twipse.logging.TwipseLog;
import org.eclipse.twipse.model.Status;
import org.eclipse.twipse.model.User;
public class TwipseContainer implements IContainer,
IPresenceService, IPresenceSender, IRoster,
IRosterManager, IRosterSubscriptionSender {
//private Map chatSessions;
private List containerListeners;
private List updateListeners;
private List messageListeners;
private List presenceListeners;
private List subscriptionListeners;
private List<IRosterItem> entries;
private IUser user;
private Namespace namespace;
private ID guid;
private ITwitterClient client;
private TwitterID connectID=null;
protected IHistoryManager historyManager = new IHistoryManager() {
public IHistory getHistory(ID partnerID, Map options) {
// TODO Auto-generated method stub
return null;
}
public boolean isActive() {
// TODO Auto-generated method stub
return false;
}
public void setActive(boolean active) {
// TODO Auto-generated method stub
}
public Object getAdapter(Class adapter) {
// TODO Auto-generated method stub
return null;
}
};
TwipseContainer() throws IDCreateException {
client = new TwitterClient();
guid = IDFactory.getDefault().createGUID();
namespace = IDFactory.getDefault().getNamespaceByName(
TwipsePlugin.NAMESPACE_IDENTIFIER);
user = new Account();
//chatSessions = new Hashtable();
containerListeners = new ArrayList();
updateListeners = new ArrayList();
messageListeners = new ArrayList();
presenceListeners = new ArrayList();
subscriptionListeners = new ArrayList();
entries = new ArrayList<IRosterItem>();
}
/*protected IRosterEntry makeRosterEntry(IRosterItem parent, User user) {
String userName = user.getScreen_name();
ID userID;
IRosterEntry entry;
try {
userID = IDFactory.getDefault().createID(connectID.getNamespace(), userName);
IUser child = new org.eclipse.ecf.core.user.User(userID,userName);
IPresence presence = new Presence(IPresence.Type.AVAILABLE);
entry = new RosterEntry(parent, child,presence);
//IPresence presence = presenceContainer.createPresence(userID.getName());
//entry.setPresenceState(presence);
return entry;
} catch (IDCreateException e) {
e.printStackTrace();
}
return null;
}*/
public void connect(ID targetID, IConnectContext connectContext)
throws ContainerConnectException {
TwipseLog.logInfo("Twipse container connect - targetID == "+targetID);
if (!(targetID instanceof TwitterID)) {
ContainerConnectException e = new ContainerConnectException(
"Errore ID");
TwipseLog.logError("ErroreID", e);
throw e;
}
//client = new TwitterClient();
fireContainerEvent(new ContainerConnectingEvent(guid, targetID));
connectID = (TwitterID) targetID;
Map<User,Status> friends = client.getFriends();
for(User u : friends.keySet()){
TwipseLog.logInfo(u.getScreen_name());
entries.add(new TwipseRosterEntry(this,u,friends.get(u),namespace));
}
fireContainerEvent(new ContainerConnectedEvent(guid, connectID));
}
public void disconnect() {
// TODO Auto-generated method stub
}
public void dispose() {
// TODO Auto-generated method stub
}
public Object getAdapter(Class serviceType) {
// TODO Auto-generated method stub
return null;
}
public Namespace getConnectNamespace() {
return IDFactory.getDefault()
.getNamespaceByName(TwipsePlugin.NAMESPACE_IDENTIFIER);
}
public ID getConnectedID() {
return connectID;
}
public ID getID() {
return guid;
}
public IAccountManager getAccountManager() {
return null;
}
public IChatRoomManager getChatRoomManager() {
return null;
}
public IPresenceSender getPresenceSender() {
return this;
}
public void addListener(IContainerListener listener) {
if (listener != null) {
synchronized (containerListeners) {
if (!containerListeners.contains(listener)) {
containerListeners.add(listener);
}
}
}
}
public void removeListener(IContainerListener listener) {
if (listener != null) {
synchronized (containerListeners) {
containerListeners.remove(listener);
}
}
}
public IChatManager getChatManager() {
// TODO Auto-generated method stub
return null;
}
public IRosterManager getRosterManager() {
// TODO Auto-generated method stub
return null;
}
public void sendPresenceUpdate(ID targetID, IPresence presence)
throws ECFException {
//TODO
}
@SuppressWarnings("unchecked")
public Collection getItems() {
// TODO Auto-generated method stub
return null;
}
public IPresenceContainerAdapter getPresenceContainerAdapter() {
// TODO Auto-generated method stub
return null;
}
public IUser getUser() {
// TODO Auto-generated method stub
return null;
}
public String getName() {
// TODO Auto-generated method stub
return null;
}
public IRosterItem getParent() {
// TODO Auto-generated method stub
return null;
}
public IRoster getRoster() {
// TODO Auto-generated method stub
return null;
}
public void addPresenceListener(IPresenceListener listener) {
// TODO Auto-generated method stub
}
public void addRosterListener(IRosterListener listener) {
// TODO Auto-generated method stub
}
public void addRosterSubscriptionListener(
IRosterSubscriptionListener listener) {
// TODO Auto-generated method stub
}
public IRosterSubscriptionSender getRosterSubscriptionSender() {
// TODO Auto-generated method stub
return null;
}
public void removePresenceListener(IPresenceListener listener) {
// TODO Auto-generated method stub
}
public void removeRosterListener(IRosterListener listener) {
// TODO Auto-generated method stub
}
public void removeRosterSubscriptionListener(
IRosterSubscriptionListener listener) {
// TODO Auto-generated method stub
}
public void sendRosterAdd(String userAccount, String nickname,
String[] groups) throws ECFException {
// TODO Auto-generated method stub
}
public void sendRosterRemove(ID userID) throws ECFException {
// TODO Auto-generated method stub
}
public void sendTypingMessage(ID toID, boolean isTyping, String body)
throws ECFException {
// TODO Auto-generated method stub
}
private class Account implements IUser {
private static final long serialVersionUID = 7497082891662391996L;
public Object getAdapter(Class adapter) {
return null;
}
public ID getID() {
return connectID;
}
public Map getProperties() {
return null;
}
public String getName() {
return client.getScreenName();
}
public String getNickname() {
return client.getScreenName();
}
}
private void fireContainerEvent(IContainerEvent event) {
synchronized (containerListeners) {
for (int i = 0; i < containerListeners.size(); i++) {
((IContainerListener) containerListeners.get(i))
.handleEvent(event);
}
}
}
private void fireMessageEvent(ID fromID, String message) {
synchronized (messageListeners) {
for (int i = 0; i < messageListeners.size(); i++) {
((IIMMessageListener) messageListeners.get(i))
.handleMessageEvent(new ChatMessageEvent(fromID,
new ChatMessage(fromID, message)));
}
}
}
private void fireTypingMessageEvent(ID fromID) {/*
synchronized (messageListeners) {
for (int i = 0; i < messageListeners.size(); i++) {
((IIMMessageListener) messageListeners.get(i))
.handleMessageEvent(new TypingMessageEvent(fromID,
new TypingMessage(fromID, true, null)));
}
}
*/}
private void fireRosterUpdate(IRosterItem item) {
synchronized (updateListeners) {
for (int i = 0; i < updateListeners.size(); i++) {
((IRosterListener) updateListeners.get(i)).handleRosterUpdate(
this, item);
}
}
}
private void fireRosterEntryAdded(IRosterEntry entry) {
synchronized (updateListeners) {
for (int i = 0; i < updateListeners.size(); i++) {
((IRosterListener) updateListeners.get(i))
.handleRosterEntryAdd(entry);
}
}
}
private void fireRosterEntryRemoved(IRosterEntry entry) {
synchronized (updateListeners) {
for (int i = 0; i < updateListeners.size(); i++) {
((IRosterListener) updateListeners.get(i))
.handleRosterEntryRemove(entry);
}
}
}
private void firePresence(ID id, IPresence presence) {
synchronized (presenceListeners) {
for (int i = 0; i < presenceListeners.size(); i++) {
((IPresenceListener) presenceListeners.get(i)).handlePresence(
id, presence);
}
}
}
private void fireHandleSubscriptionRequest(ID fromID) {
synchronized (subscriptionListeners) {
for (int i = 0; i < subscriptionListeners.size(); i++) {
((IRosterSubscriptionListener) subscriptionListeners.get(i))
.handleSubscribeRequest(fromID);
}
}
}
private void fireHandleUnsubscribed(ID fromID) {
synchronized (subscriptionListeners) {
for (int i = 0; i < subscriptionListeners.size(); i++) {
((IRosterSubscriptionListener) subscriptionListeners.get(i))
.handleUnsubscribed(fromID);
}
}
}
}