Package gui.action

Source Code of gui.action.IMListenerForGui

/*
* Hamsam - Instant Messaging API
* Copyright (C) 2003 Raghu K
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package gui.action;

import java.util.Date;
import java.util.Enumeration;

import hamsam.api.Response;
import hamsam.api.IMListener;
import hamsam.api.Buddy;
import hamsam.api.Message;
import hamsam.api.Conference;
import hamsam.api.MessageComponent;
import hamsam.api.TextComponent;
import hamsam.api.SmileyComponent;
import hamsam.api.URLComponent;

import hamsam.protocol.Protocol;

import gui.MainFrame;

import gui.tools.PropertiesManager;
import gui.tools.Traces;

public class IMListenerForGui
    implements IMListener {

  private boolean isConnected = false;
  private PropertiesManager propertiesMgr = null;
  private Buddy[] buddiesReceived = null;
  private Buddy[] buddiesIgnored = null;

  public void setPropertiesMgr(PropertiesManager p) {
    this.propertiesMgr = p;
  }

  public Buddy[] getBuddyListReceived() {
    return this.buddiesReceived;
  }

  public Buddy[] getBuddyListIgnored() {
    return this.buddiesIgnored;
  }

  public void connecting(Protocol protocol) {
    Traces.printTraces("Connecting...");
  }

  public void connected(Protocol protocol) {
    Traces.printTraces("Connected");
    this.isConnected = true;
    MainFrame.myRef.enableContactPanel(true);
    MainFrame.myRef.setStatus(propertiesMgr.getValue("connected"));
    MainFrame.myRef.updateBuddyListReceived(this.buddiesReceived);
  }

  public void connectFailed(Protocol protocol, String message) {
    Traces.printTraces("Connect failed: " + message);
  }

  public void disconnected(Protocol protocol) {
    Traces.printTraces("Disconnected: "/* + Thread.currentThread().activeCount()*/);
  }

  public void buddyListReceived(Protocol protocol, Buddy[] buddies) {
    buddiesReceived = buddies;
    Traces.printTraces("Buddy list received: ");
    for (int i = 0; i < buddies.length; i++)
      System.out.println(buddies[i].getUsername());
  }

  public void ignoreListReceived(Protocol protocol, Buddy[] buddies) {
    buddiesIgnored = buddies;
    Traces.printTraces("Ignore list received: ");
    for (int i = 0; i < buddies.length; i++)
      System.out.println(buddies[i].getUsername());
  }

  public void protocolMessageReceived(Protocol protocol, Message msg) {
    printMessage("Protocol Message: ", msg);
  }

  public void instantMessageReceived(Buddy buddy, Message msg) {
    printMessage(buddy.getUsername() + ": ", msg);
  }

  public void offlineMessageReceived(Buddy buddy, Date time, Message msg) {
    printMessage(buddy.getUsername() + " [offline @ " + time + "] : ", msg);
  }

  public void mailNotificationReceived(Protocol protocol, int count,
                                       String[] from, String[] subject) {
    if (count == -1)
      Traces.printTraces("You have new mail");
    else
      Traces.printTraces("You have " + count + " new mail(s).");

    if (from != null && subject != null) {
      for (int i = 0; i < from.length || i < subject.length; i++)
        Traces.printTraces("\t(" + (i + 1) + ") from: " + from[i] +
                           " subject: " + subject[i]);
    }
  }

  public void conferenceInvitationAccepted(Conference conf, Buddy buddy) {
    System.out.println(buddy.getUsername() + " joined the conference: " + conf);
  }

  public void conferenceInvitationDeclined(Conference conf, Buddy buddy,
                                           String message) {
    System.out.println(buddy.getUsername() +
                       " declined to join the conference: " + conf +
                       " with the message: " + message);
  }

  public Response conferenceInvitationReceived(Conference conf, String message) {
    Buddy[] parts = conf.getParticipants();
    System.out.print("Conference invitation: ");

    for (int i = 0; i < parts.length; i++)
      System.out.print(parts[i].getUsername() + " ");

    System.out.println();

    return new Response();
  }

  public void conferenceMessageReceived(Conference conf, Buddy buddy,
                                        Message message) {
    System.out.print(conf);
    printMessage(" conference: " + buddy.getUsername() + ": ", message);
  }

  public void buddyStatusChanged(Buddy buddy) {
    Traces.printTraces("Status of " + buddy.getUsername() + " is now " +
                       buddy.getStatus());
  }

  public void conferenceParticipantLeft(Conference conf, Buddy buddy) {
    System.out.println(buddy.getUsername() + " left the conference: " + conf);
  }

  public void typingStarted(Buddy buddy) {
    System.out.println(buddy.getUsername() + " is typing");
  }

  public void typingStopped(Buddy buddy) {
    System.out.println(buddy.getUsername() + " stopped typing");
  }

  public void buddyAdded(Buddy buddy) {
    System.out.println(buddy.getUsername() + " is added to the buddy list");
  }

  public void buddyAddRejected(Buddy buddy, String reasonMessage) {
    System.out.println(buddy.getUsername() + " rejected to be a buddy: " +
                       reasonMessage);
  }

  public void buddyAddFailed(Buddy buddy, String reasonMessage) {
    Traces.printTraces("Attempt to add " + buddy.getUsername() + " failed: " +
                       reasonMessage);
  }

  public Response buddyAddRequest(Buddy buddy, Buddy myself, String message) {
    System.out.println(buddy.getUsername() + " is trying to add " +
                       myself.getUsername() + " to his buddy list: " + message);
    return new Response();
  }

  public void buddyDeleted(Buddy buddy) {
    System.out.println(buddy.getUsername() + " is deleted from the buddy list");
  }

  public void buddyDeleteFailed(Buddy buddy, String reasonMessage) {
    Traces.printTraces("Attempt to delete " + buddy.getUsername() + " failed: " +
                       reasonMessage);
  }

  public void buddyIgnored(Buddy buddy) {
    System.out.println(buddy.getUsername() + " is ignored now");
  }

  public void buddyIgnoreFailed(Buddy buddy, String reasonMessage) {
    Traces.printTraces("Attempt to ignore " + buddy.getUsername() + " failed: " +
                       reasonMessage);
  }

  public void buddyUnignored(Buddy buddy) {
    System.out.println(buddy.getUsername() + " is unignored now");
  }

  public void buddyUnignoreFailed(Buddy buddy, String reasonMessage) {
    Traces.printTraces("Attempt to unignore " + buddy.getUsername() +
                       " failed: " + reasonMessage);
  }

  private void printMessage(String header, Message msg) {
    System.out.print(header);

    Enumeration e = msg.getComponents();
    while (e.hasMoreElements()) {
      MessageComponent comp = (MessageComponent) e.nextElement();
      if (comp instanceof TextComponent) {
        TextComponent txt = (TextComponent) comp;
        System.out.print("<text>" + new String(txt.getSequence()));
      }
      else if (comp instanceof SmileyComponent) {
        SmileyComponent sml = (SmileyComponent) comp;
        System.out.print("<smiley>" + sml.getName());
      }
      else if (comp instanceof URLComponent) {
        URLComponent url = (URLComponent) comp;
        System.out.print("<url>" + url.getLinkText());
      }
      else
        System.out.println(comp);
    }

    System.out.println();
  }

/* (non-Javadoc)
* @see hamsam.api.IMListener#conferenceParticipantJoined(hamsam.api.Conference, hamsam.api.Buddy)
*/
public void conferenceParticipantJoined(Conference arg0, Buddy arg1)
{
  // TODO Auto-generated method stub
 
}

/* (non-Javadoc)
* @see hamsam.api.IMListener#conferenceClosed(hamsam.api.Conference)
*/
public void conferenceClosed(Conference arg0)
{
  // TODO Auto-generated method stub
 
}
}
TOP

Related Classes of gui.action.IMListenerForGui

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.