/*
* 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.globalshortcut;
import java.util.*;
import net.java.sip.communicator.service.globalshortcut.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.keybindings.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;
import org.osgi.framework.*;
/**
* OSGi Activator for global shortcut.
*
* @author Sebastien Vincent
*/
public class GlobalShortcutActivator
implements BundleActivator
{
/**
* The Logger used by the GlobalShortcutActivator class
* and its instances for logging output.
*/
private static final Logger logger
= Logger.getLogger(GlobalShortcutActivator.class);
/**
* The GlobalShortcutServiceImpl.
*/
protected static GlobalShortcutServiceImpl globalShortcutService = null;
/**
* OSGi bundle context.
*/
private static BundleContext bundleContext = null;
/**
* Keybindings service reference.
*/
private static KeybindingsService keybindingsService = null;
/**
* UI service reference.
*/
private static UIService uiService = null;
/**
* Returns the KeybindingsService obtained from the bundle context.
*
* @return the KeybindingsService obtained from the bundle context
*/
public static KeybindingsService getKeybindingsService()
{
if (keybindingsService == null)
{
keybindingsService
= ServiceUtils.getService(
bundleContext,
KeybindingsService.class);
}
return keybindingsService;
}
/**
* Returns the UIService obtained from the bundle context.
*
* @return the UIService obtained from the bundle context
*/
public static UIService getUIService()
{
if (uiService == null)
{
uiService
= ServiceUtils.getService(
bundleContext,
UIService.class);
}
return uiService;
}
/**
* Starts the execution of this service bundle in the specified context.
*
* @param bundleContext the context in which the service bundle is to
* start executing
* @throws Exception if an error occurs while starting the execution of the
* service bundle in the specified context
*/
public void start(BundleContext bundleContext)
throws Exception
{
GlobalShortcutActivator.bundleContext = bundleContext;
globalShortcutService = new GlobalShortcutServiceImpl();
globalShortcutService.start();
bundleContext.registerService(
GlobalShortcutService.class,
globalShortcutService,
null);
globalShortcutService.reloadGlobalShortcuts();
registerListenerWithProtocolProviderService();
bundleContext.addServiceListener(new ServiceListener()
{
public void serviceChanged(ServiceEvent serviceEvent)
{
GlobalShortcutActivator.this.serviceChanged(serviceEvent);
}
});
if (logger.isDebugEnabled())
logger.debug("GlobalShortcut Service ... [REGISTERED]");
}
/**
* Stops the execution of this service bundle in the specified context.
*
* @param bundleContext the context in which this service bundle is to
* stop executing
* @throws Exception if an error occurs while stopping the execution of the
* service bundle in the specified context
*/
public void stop(BundleContext bundleContext)
throws Exception
{
globalShortcutService.stop();
GlobalShortcutActivator.bundleContext = null;
}
/**
* Implements the ServiceListener method. Verifies whether the
* passed event concerns a ProtocolProviderService and adds the
* corresponding UI controls.
*
* @param event The ServiceEvent object.
*/
private void serviceChanged(ServiceEvent event)
{
ServiceReference> serviceRef = event.getServiceReference();
// if the event is caused by a bundle being stopped, we don't want to
// know
if (serviceRef.getBundle().getState() == Bundle.STOPPING)
{
return;
}
Object service = bundleContext.getService(serviceRef);
// we don't care if the source service is not a protocol provider
if (!(service instanceof ProtocolProviderService))
{
return;
}
switch (event.getType())
{
case ServiceEvent.REGISTERED:
this.handleProviderAdded((ProtocolProviderService) service);
break;
case ServiceEvent.UNREGISTERING:
this.handleProviderRemoved((ProtocolProviderService) service);
break;
}
}
/**
* Get all registered ProtocolProviderService and set our listener.
*/
public void registerListenerWithProtocolProviderService()
{
Collection> ppsRefs
= ServiceUtils.getServiceReferences(
bundleContext,
ProtocolProviderService.class);
if(!ppsRefs.isEmpty())
{
for(ServiceReference ppsRef : ppsRefs)
{
ProtocolProviderService pps = bundleContext.getService(ppsRef);
OperationSetBasicTelephony> opSet
= pps.getOperationSet(OperationSetBasicTelephony.class);
if(opSet != null)
{
opSet.addCallListener(
globalShortcutService.getCallShortcut());
}
}
}
}
/**
* Notifies this manager that a specific
* ProtocolProviderService has been registered as a service.
*
* @param provider the ProtocolProviderService which has been
* registered as a service.
*/
private void handleProviderAdded(final ProtocolProviderService provider)
{
OperationSetBasicTelephony> opSet =
provider.getOperationSet(OperationSetBasicTelephony.class);
if(opSet != null)
opSet.addCallListener(globalShortcutService.getCallShortcut());
}
/**
* Notifies this manager that a specific
* ProtocolProviderService has been unregistered as a service.
*
* @param provider the ProtocolProviderService which has been
* unregistered as a service.
*/
private void handleProviderRemoved(ProtocolProviderService provider)
{
OperationSetBasicTelephony> opSet =
provider.getOperationSet(OperationSetBasicTelephony.class);
if(opSet != null)
opSet.removeCallListener(globalShortcutService.getCallShortcut());
}
}