/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.utils;

import java.util.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.service.notification.*;
import net.java.sip.communicator.service.protocol.*;

public class NotificationManager
{
    public static final String INCOMING_MESSAGE = "IncomingMessage";
    
    public static final String INCOMING_CALL = "IncomingCall";
    
    public static final String OUTGOING_CALL = "OutgoingCall";
    
    public static final String BUSY_CALL = "BusyCall";
    
    private static Hashtable soundHandlers = new Hashtable();
    
    public static void registerGuiNotifications()
    {
        NotificationService notificationService
            = GuiActivator.getNotificationService();
        
        if(notificationService == null)
            return;
        
        // Register incoming message notifications.
        notificationService.registerNotificationForEvent(
                INCOMING_MESSAGE,
                NotificationService.ACTION_POPUP_MESSAGE,
                null,
                null);

        notificationService.registerNotificationForEvent(
                INCOMING_MESSAGE,
                NotificationService.ACTION_SOUND,
                Sounds.INCOMING_MESSAGE,
                null);
        
        // Register incoming call notifications.
        notificationService.registerNotificationForEvent(
                INCOMING_CALL,
                NotificationService.ACTION_POPUP_MESSAGE,
                null,
                null);
    
        SoundNotificationHandler inCallSoundHandler
            = (SoundNotificationHandler) notificationService
                .createSoundNotificationHandler(Sounds.INCOMING_CALL, 2000);
        
        notificationService.registerNotificationForEvent(
                INCOMING_CALL,
                NotificationService.ACTION_SOUND,
                inCallSoundHandler);

        soundHandlers.put(INCOMING_CALL, inCallSoundHandler);
        
        // Register outgoing call notifications.
        SoundNotificationHandler outCallSoundHandler
            = (SoundNotificationHandler) notificationService
                .createSoundNotificationHandler(Sounds.OUTGOING_CALL, 3000);
        
        notificationService.registerNotificationForEvent(
                OUTGOING_CALL,
                NotificationService.ACTION_SOUND,
                outCallSoundHandler);
    
        soundHandlers.put(OUTGOING_CALL, outCallSoundHandler);

        // Register busy call notifications.
        SoundNotificationHandler busyCallSoundHandler
            = (SoundNotificationHandler) notificationService
                .createSoundNotificationHandler(Sounds.BUSY, 1);
        
        notificationService.registerNotificationForEvent(
                BUSY_CALL,
                NotificationService.ACTION_SOUND,
                busyCallSoundHandler);
    
        soundHandlers.put(OUTGOING_CALL, outCallSoundHandler);

    }

    /**
     * Fires a message notification for the given event type through the
     * <tt>NotificationService</tt>.
     * 
     * @param eventType the event type for which we fire a notification
     * @param messageTitle the title of the message
     * @param message the content of the message
     */
    public static void fireNotification(String eventType,
                                        String messageTitle,
                                        String message)
    {
        NotificationService notificationService
            = GuiActivator.getNotificationService();
        
        if(notificationService == null)
            return;

        notificationService.fireNotification(eventType, messageTitle, message);

    }

    /**
     * Fires a chat message notification for the given event type through the
     * <tt>NotificationService</tt>.
     * 
     * @param contact the contact to which the chat message corresponds
     * @param eventType the event type for which we fire a notification
     * @param messageTitle the title of the message
     * @param message the content of the message
     */
    public static void fireChatNotification(Contact contact,
                                            String eventType,
                                            String messageTitle,
                                            String message)
    {
        NotificationService notificationService
            = GuiActivator.getNotificationService();

        if(notificationService == null)
            return;

        NotificationActionHandler popupActionHandler = null;

        if(eventType.equals(INCOMING_MESSAGE)
            && GuiActivator.getUIService().getChat(contact).isChatFocused())
        {
            popupActionHandler = notificationService
                .getEventNotificationActionHandler(
                                    eventType,
                                    NotificationService.ACTION_POPUP_MESSAGE);

            popupActionHandler.setEnabled(false);
        }

        notificationService.fireNotification(eventType, messageTitle, message);

        if(popupActionHandler != null)
            popupActionHandler.setEnabled(true);
    }

    /**
     * Fires a notification for the given event type through the
     * <tt>NotificationService</tt>. The event type is one of the static
     * constants defined in this class.
     * 
     * @param eventType the event type for which we want to fire a notification
     */
    public static void fireNotification(String eventType)
    {
        NotificationService notificationService
            = GuiActivator.getNotificationService();
        
        if(notificationService == null)
            return;
        
        notificationService.fireNotification(eventType);
    }

    /**
     * Stops all sounds for the given event type.
     * 
     * @param eventType the event type for which we should stop sounds. One of
     * the static event types defined in this class.
     */
    public static void stopSound(String eventType)
    {
        NotificationService notificationService
            = GuiActivator.getNotificationService();

        if(notificationService == null)
            return;

        SoundNotificationHandler soundHandler
            = (SoundNotificationHandler) notificationService
                .getEventNotificationActionHandler(
                    eventType, NotificationService.ACTION_SOUND);

        soundHandler.stop();
    }
}

@@ -265,24 +265,72 @@
 impl.gui.*;%0A
+import net.java.sip.communicator.service.gui.*;%0A
 import net.j
@@ -4070,24 +4070,29 @@
 contact the 
+chat 
 contact to w
@@ -4124,16 +4124,75 @@
 responds
+;%0A     * the chat contact could be a Contact or a ChatRoom.
 %0A     * 
@@ -4409,21 +4409,20 @@
 ication(
-Conta
+Obje
 ct conta
@@ -4855,105 +4855,568 @@
     
-if(eventType.equals(INCOMING_MESSAGE)%0A            && GuiActivator.getUIService().getChat(contact)
+Chat chatPanel = null;%0A%0A        if (contact instanceof Contact)%0A            chatPanel = GuiActivator.getUIService().getChat((Contact) contact);%0A        else if (contact instanceof ChatRoom)%0A        %7B%0A            // For system rooms we don't want to send notification events.%0A            if ((contact instanceof ChatRoom)%0A                && ((ChatRoom) contact).isSystem())%0A                return;%0A%0A            chatPanel = GuiActivator.getUIService().getChat((ChatRoom) contact);%0A        %7D%0A%0A        if(eventType.equals(INCOMING_MESSAGE)%0A            && chatPanel
 .isC

Disable notifications for system chat rooms.