aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/branding/BrandingActivator.java
blob: 6d8bdfd87576a32454b044753960207bb6205ac2 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.plugin.branding;

import java.lang.reflect.*;
import java.util.*;

import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*;

import org.jitsi.service.configuration.*;
import org.jitsi.service.resources.*;
import org.osgi.framework.*;

/**
 * Branding bundle activator.
 */
public class BrandingActivator
    extends AbstractServiceDependentActivator
    implements BundleListener
{
    private final Logger logger = Logger.getLogger(BrandingActivator.class);

    /**
     * The name of the boolean property which indicates whether the splash
     * screen (i.e. <code>WelcomeWindow</code>) is to be shown or to not be
     * utilized for the sake of better memory consumption and faster startup.
     */
    private static final String PNAME_SHOW_SPLASH_SCREEN
        = "net.java.sip.communicator.plugin.branding.SHOW_SPLASH_SCREEN";

    private static BundleContext bundleContext;

    private static ResourceManagementService resourcesService;

    /**
     * The welcome window.
     */
    private WelcomeWindow welcomeWindow;

    @Override
    public void start(BundleContext bc) throws Exception
    {
        super.start(bc);

        ConfigurationService config = getConfigurationService();
        boolean showSplashScreen
            = (config == null)
                ? true /*
                        * Having no ConfigurationService reference is not good
                        * for the application so we are better off with the
                        * splash screen to actually see which bundles get loaded
                        * and maybe be able to debug the problem.
                        */
                : config.getBoolean(PNAME_SHOW_SPLASH_SCREEN, false);

        /*
         * WelcomeWindow is huge because it has a large image spread all over it
         * so, given it's only necessary before the UIService gets activated, we
         * certainly don't want to keep it around (e.g. as an instance field or
         * as a final variable used inside a BundleListener which never gets
         * removed).
         */
        if (showSplashScreen)
        {
            welcomeWindow = new WelcomeWindow();
            welcomeWindow.pack();
            welcomeWindow.setVisible(true);
        }
        else
            welcomeWindow = null;

        if (getResources().getSettingsString(
                "service.gui.APPLICATION_NAME").equals("SIP Communicator"))
            new JitsiWarningWindow(null).setVisible(true);

        bundleContext.addBundleListener(this);
    }

    /**
     * Bundle has been started if welcome window is available and visible
     * update it to show the bundle activity.
     * @param evt
     */
    public synchronized void bundleChanged(BundleEvent evt)
    {
        if (welcomeWindow != null
            && welcomeWindow.isShowing()
            && (evt.getType() == BundleEvent.STARTED))
        {
            /*
             * The IBM JRE on GNU/Linux reports the Bundle-Name as null while
             * the SUN JRE reports it as non-null. Just prevent the throwing of
             * a NullPointerException because displaying the Bundle-Name isn't
             * vital anyway.
             */
            Object bundleName = evt.getBundle().getHeaders().get("Bundle-Name");

            welcomeWindow.setBundle(
                (bundleName == null) ? null : bundleName.toString());
        }
    }

    /**
     * Setting context to the activator, as soon as we have one.
     *
     * @param context the context to set.
     */
    @Override
    public void setBundleContext(BundleContext context)
    {
        bundleContext = context;
    }

    /**
     * This activator depends on UIService.
     * @return the class name of uiService.
     */
    @Override
    public Class<?> getDependentServiceClass()
    {
        return UIService.class;
    }

    /**
     * The dependent service is available and the bundle will start.
     * @param dependentService the UIService this activator is waiting.
     */
    @Override
    public void start(Object dependentService)
    {
        // UI-Service started.

        /*
         * Don't let bundleContext retain a reference to this
         * listener because it'll retain a reference to
         * welcomeWindow. Besides, we're no longer interested in
         * handling events so it doesn't make sense to even retain
         * this listener.
         */
        bundleContext.removeBundleListener(this);

        // register the about dialog menu entry
        registerMenuEntry((UIService)dependentService);

        if (welcomeWindow != null)
        {
            synchronized(this)
            {
                welcomeWindow.close();
                welcomeWindow = null;
            }
        }
    }

    public void stop(BundleContext arg0) throws Exception
    {
    }

    /**
     * Register the about menu entry.
     * @param uiService
     */
    private void registerMenuEntry(UIService uiService)
    {
        if ((uiService == null)
                || !uiService.useMacOSXScreenMenuBar()
                || !registerMenuEntryMacOSX(uiService))
        {
            registerMenuEntryNonMacOSX(uiService);
        }
    }

    private boolean registerMenuEntryMacOSX(UIService uiService)
    {
        Exception exception = null;
        try
        {
            Class<?> clazz =
                Class
                    .forName("net.java.sip.communicator.plugin.branding.MacOSXAboutRegistration");
            Method method = clazz.getMethod("run", (Class<?>[]) null);
            Object result = method.invoke(null, (Object[]) null);

            if (result instanceof Boolean)
                return ((Boolean) result).booleanValue();
        }
        catch (ClassNotFoundException ex)
        {
            exception = ex;
        }
        catch (IllegalAccessException ex)
        {
            exception = ex;
        }
        catch (InvocationTargetException ex)
        {
            exception = ex;
        }
        catch (NoSuchMethodException ex)
        {
            exception = ex;
        }
        if (exception != null)
            logger.error(
                "Failed to register Mac OS X-specific About handling.",
                exception);
        return false;
    }

    private void registerMenuEntryNonMacOSX(UIService uiService)
    {
        // Register the about window plugin component in the main help menu.
        Hashtable<String, String> helpMenuFilter
            = new Hashtable<String, String>();
        helpMenuFilter.put( Container.CONTAINER_ID,
                            Container.CONTAINER_HELP_MENU.getID());

        bundleContext.registerService(
            PluginComponentFactory.class.getName(),
            new PluginComponentFactory(
                    Container.CONTAINER_HELP_MENU)
            {
                @Override
                protected PluginComponent getPluginInstance()
                {
                    return new AboutWindowPluginComponent(getContainer(), this);
                }
            },
            helpMenuFilter);

        if (logger.isInfoEnabled())
            logger.info("ABOUT WINDOW ... [REGISTERED]");

        // Register the about window plugin component in the chat help menu.
        Hashtable<String, String> chatHelpMenuFilter
            = new Hashtable<String, String>();
        chatHelpMenuFilter.put( Container.CONTAINER_ID,
                                Container.CONTAINER_CHAT_HELP_MENU.getID());

        bundleContext.registerService(
            PluginComponentFactory.class.getName(),
            new PluginComponentFactory(
                    Container.CONTAINER_CHAT_HELP_MENU)
            {
                @Override
                protected PluginComponent getPluginInstance()
                {
                    return new AboutWindowPluginComponent(getContainer(), this);
                }
            },
            chatHelpMenuFilter);

        if (logger.isInfoEnabled())
            logger.info("CHAT ABOUT WINDOW ... [REGISTERED]");
    }

    static BundleContext getBundleContext()
    {
        return bundleContext;
    }

    private static ConfigurationService getConfigurationService()
    {
        ServiceReference serRef
            = bundleContext
                .getServiceReference(ConfigurationService.class.getName());
        return
            (serRef == null)
                ? null
                : (ConfigurationService) bundleContext.getService(serRef);
    }

    /**
     * Returns the <tt>ResourceManagementService</tt>.
     *
     * @return the <tt>ResourceManagementService</tt>.
     */
    public static ResourceManagementService getResources()
    {
        if (resourcesService == null)
            resourcesService
                = ResourceManagementServiceUtils.getService(bundleContext);
        return resourcesService;
    }
}