aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/growlnotification/GrowlNotificationServiceImpl.java
blob: 0b8c178f96eda85846cec43bb098dc0baead8546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Jitsi, 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.growlnotification;

import net.java.sip.communicator.service.systray.*;
import net.java.sip.communicator.service.systray.event.*;
import net.java.sip.communicator.util.*;

import org.apache.commons.lang3.*;
import org.growl4j.*;
import org.jitsi.service.resources.*;
import org.osgi.framework.*;

/**
 * The Growl Notification Service displays on-screen information such as
 * messages or call received, etc.
 *
 * @author Romain Kuntz
 * @author Egidijus Jankauskas
 * @author Lyubomir Marinov
 */
public class GrowlNotificationServiceImpl
    extends AbstractPopupMessageHandler
    implements GrowlCallbacksListener
{
    /**
     * The <tt>Logger</tt> used by the <tt>GrowlNotificationServiceImpl</tt>
     * class and its instance for logging output.
     */
    private static final Logger logger
        = Logger.getLogger(GrowlNotificationServiceImpl.class);

    /**
     * The notification type (in Growl terms) to be specified to
     * {@link Growl#notifyGrowlOf(String, String, String, byte[], Object)}
     * when called by {@link #showPopupMessage(PopupMessage)}.
     */
    private static final String SHOW_POPUP_MESSAGE_TYPE = "Default";

    /**
     * The <tt>Growl</tt> object.
     */
    private Growl growl;

    /**
     * Starts the service. Creates a Growl notifier, and check the current
     * registered protocol providers which supports BasicIM and adds message
     * listener to them.
     *
     * @param bc a currently valid bundle context
     */
    public void start(BundleContext bc)
    {
        if (logger.isDebugEnabled())
            logger.debug("Starting the Growl Notification implementation.");

        ResourceManagementService resources
            = GrowlNotificationActivator.getResources();
        byte[] sipIcon
            = resources.getImageInBytes(
                    "service.gui.SIP_COMMUNICATOR_LOGO_45x45");
        String[] dict = { SHOW_POPUP_MESSAGE_TYPE };

        growl
            = new Growl(
                    resources.getSettingsString("service.gui.APPLICATION_NAME"),
                    "net.sip-communicator",
                    sipIcon,
                    dict,
                    dict);
        growl.addClickedNotificationsListener(this);
    }

    /**
     * Stops the service.
     *
     * @param bc BundleContext
     */
    public void stop(BundleContext bc)
    {
        if (growl != null)
        {
            growl.doFinalCleanUp();
            growl = null;
        }
    }

    /**
     * Implements <tt>PopupMessageHandler#showPopupMessage()</tt>
     *
     * @param popupMessage the message we will show
     */
    public void showPopupMessage(PopupMessage popupMessage)
    {
        String messageBody = popupMessage.getMessage();
        String messageTitle = popupMessage.getMessageTitle();

        // remove eventual HTML code before showing the pop-up message
        messageBody = messageBody.replaceAll("</?\\w++[^>]*+>", "");
        messageTitle = messageTitle.replaceAll("</?\\w++[^>]*+>", "");

        // unescape any chars that can be escaped inside the text
        messageBody = StringEscapeUtils.unescapeHtml4(messageBody);
        messageTitle = StringEscapeUtils.unescapeHtml4(messageTitle);

        growl.notifyGrowlOf(
                messageTitle,
                messageBody,
                SHOW_POPUP_MESSAGE_TYPE,
                popupMessage.getIcon(),
                popupMessage.getTag());
    }

    /**
     * This method is called by Growl when the Growl notification is not clicked
     *
     * @param context an object identifying the notification
     */
    public void growlNotificationTimedOut(Object context)
    {
        if (logger.isTraceEnabled())
            logger.trace("Growl notification timed out: " + context);
    }

    /**
     * This method is called by Growl when the Growl notification is clicked
     *
     * @param context an object identifying the notification
     */
    public void growlNotificationWasClicked(final Object context)
    {
        // release the native thread
        new Thread(new Runnable()
        {
            public void run()
            {
                firePopupMessageClicked(
                    new SystrayPopupMessageEvent(this, context));
                if (logger.isTraceEnabled())
                    logger.trace("Growl notification clicked: " + context);
            }
        }).start();
    }

    /**
     * Implements <tt>toString</tt> from <tt>PopupMessageHandler</tt>.
     *
     * @return a description of this handler
     */
    @Override
    public String toString()
    {
        return
            GrowlNotificationActivator.getResources().getI18NString(
                    "impl.growlnotification.POPUP_MESSAGE_HANDLER");
    }

    /**
     * Implements <tt>getPreferenceIndex</tt> from <tt>PopupMessageHandler</tt>.
     * This handler is able to show images, detect clicks, match a click to a
     * message, and use a native popup mechanism, thus the index is 4.
     *
     * @return a preference index
     */
    public int getPreferenceIndex()
    {
        return 4;
    }
}