Bug 11222
authordadubrow
Fri, 25 Jun 2010 15:35:46 -0500
changeset 1544 93a010393cd9
parent 1540 b2cc7e8de6a8
child 1545 3eb6c57e65c0
Bug 11222
connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/agent/PCCSDiscoveryAgent.java
connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/messages.properties
connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/pccsnative/PCCSConnection.java
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/RemoteConnectionsActivator.java
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ToggleDiscoveryAgentAction.java
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/api/IDeviceDiscoveryAgent.java
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java
--- a/connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/agent/PCCSDiscoveryAgent.java	Wed Jun 23 15:28:44 2010 -0500
+++ b/connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/agent/PCCSDiscoveryAgent.java	Fri Jun 25 15:35:46 2010 -0500
@@ -353,4 +353,12 @@
 		}
 		return loadStatus;
 	}
+
+	public boolean isRunning() {
+		return pccsConnection != null && pccsConnection.isOpen();
+	}
+
+	public String getId() {
+		return getClass().getName();
+	}
 }
--- a/connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/messages.properties	Wed Jun 23 15:28:44 2010 -0500
+++ b/connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/messages.properties	Fri Jun 25 15:35:46 2010 -0500
@@ -1,4 +1,4 @@
-Activator_Agent_Name=PC Suite Device Discovery Agent
+Activator_Agent_Name=PC Suite Device Discovery
 Activator_PCCS_Location=http://www2.connecting.nokia.com/nmp/swp/pcsuite.nsf/document/ES336ZAHVY?OpenDocument
 Activator_PCSuite_Location=https://tools.ext.nokia.com/PCsuite
 ConnAPILibrary_PCCS_Not_Found_Error=Carbide could not find the PC Connectivity Solution (PCCS) installation. For more information go to: 
--- a/connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/pccsnative/PCCSConnection.java	Wed Jun 23 15:28:44 2010 -0500
+++ b/connectivity/com.nokia.carbide.remoteConnections.discovery.pccs/src/com/nokia/carbide/remoteconnections/discovery/pccs/pccsnative/PCCSConnection.java	Fri Jun 25 15:35:46 2010 -0500
@@ -1300,4 +1300,11 @@
     		throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, PCCS_WRONG_VERSION, msg, null));
     	}
 	}
+	
+	/**
+	 * @return whether we have a valid handle
+	 */
+	public boolean isOpen() {
+		return dmHandle != APIHANDLE.INVALID_HANDLE_VALUE;
+	}
 }
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/RemoteConnectionsActivator.java	Wed Jun 23 15:28:44 2010 -0500
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/RemoteConnectionsActivator.java	Fri Jun 25 15:35:46 2010 -0500
@@ -18,6 +18,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -27,6 +28,7 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.viewers.IFilter;
 import org.eclipse.swt.widgets.Control;
@@ -40,11 +42,12 @@
 import com.nokia.carbide.remoteconnections.interfaces.IConnectionTypeProvider;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
 import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent;
+import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent.IPrerequisiteStatus;
 import com.nokia.carbide.remoteconnections.internal.api.IStatusDisplay;
-import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent.IPrerequisiteStatus;
 import com.nokia.carbide.remoteconnections.internal.registry.Registry;
 import com.nokia.carbide.remoteconnections.internal.ui.DeviceDiscoveryPrequisiteErrorDialog;
 import com.nokia.carbide.remoteconnections.internal.ui.StatusDisplay;
+import com.nokia.cpp.internal.api.utils.core.ListenerList;
 import com.nokia.cpp.internal.api.utils.core.Logging;
 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
 
@@ -85,6 +88,10 @@
 				runnable.run();
 		}
 	}
+	
+	public interface IDiscoveryAgentsLoadedListener {
+		void agentsAreLoaded();
+	}
 
 	// The plug-in ID
 	public static final String PLUGIN_ID = "com.nokia.carbide.remoteConnections"; //$NON-NLS-1$
@@ -95,13 +102,16 @@
 	private static RemoteConnectionsActivator plugin;
 
 	private Collection<IDeviceDiscoveryAgent> discoveryAgents;
+	private ListenerList<IDiscoveryAgentsLoadedListener> listeners;
 
 	private static final String IGNORE_AGENT_LOAD_ERRORS_KEY = "ignoreAgentLoadErrors"; //$NON-NLS-1$
+	private static final String AGENT_STATE_KEY_PREFIX = "agentState."; //$NON-NLS-1$
 
 	/**
 	 * The constructor
 	 */
 	public RemoteConnectionsActivator() {
+		listeners = new ListenerList<IDiscoveryAgentsLoadedListener>();
 	}
  
 	public void start(BundleContext context) throws Exception {
@@ -176,6 +186,28 @@
 			logError(e);
 		}
 	}
+	
+	private void storeAgentRunningStates() {
+		IPreferenceStore preferenceStore = getPreferenceStore();
+		for (IDeviceDiscoveryAgent agent : discoveryAgents) {
+			String agentKey = AGENT_STATE_KEY_PREFIX + agent.getId();
+			if (!preferenceStore.contains(agentKey))
+				preferenceStore.setDefault(agentKey, true);
+			preferenceStore.setValue(agentKey, agent.isRunning());
+		}
+		try {
+			new InstanceScope().getNode(PLUGIN_ID).flush();
+		} catch (BackingStoreException e) {
+			logError(e);
+		}
+	}
+	
+	private boolean getStoredAgentRunningState(IDeviceDiscoveryAgent agent) {
+		String agentKey = AGENT_STATE_KEY_PREFIX + agent.getId();
+		if (!getPreferenceStore().contains(agentKey))
+			return true;
+		return getPreferenceStore().getBoolean(agentKey);
+	}
 
 	/**
 	 * Returns the shared instance
@@ -232,7 +264,9 @@
 			public boolean select(Object toTest) {
 				if (toTest instanceof IDeviceDiscoveryAgent) {
 					try {
-						((IDeviceDiscoveryAgent) toTest).start();
+						IDeviceDiscoveryAgent discoveryAgent = (IDeviceDiscoveryAgent) toTest;
+						if (getStoredAgentRunningState(discoveryAgent))
+							discoveryAgent.start();
 						return true;
 					} catch (Throwable e) {
 						// since we launch arbitrary code, catch any exception to prevent killing the view
@@ -242,13 +276,15 @@
 				return false;
 			}
 		});
-		
+		fireDiscoveryAgentsLoaded();
 	}
 
 	private void stopDeviceDiscoveryAgents() {
+		storeAgentRunningStates();
 		for (IDeviceDiscoveryAgent agent : discoveryAgents) {
 			try {
-				agent.stop();
+				if (agent.isRunning())
+					agent.stop();
 			} catch (CoreException e) {
 				logError(e);
 			}
@@ -287,4 +323,24 @@
 	public static IStatusDisplay getStatusDisplay() {
 		return new StatusDisplay();
 	}
+
+	public Collection<IDeviceDiscoveryAgent> getDiscoveryAgents() {
+		if (discoveryAgents != null)
+			return Collections.unmodifiableCollection(discoveryAgents);
+		return Collections.emptySet();
+	}
+
+	public void addDiscoveryAgentsLoadedListener(IDiscoveryAgentsLoadedListener listener) {
+		listeners.add(listener);
+	}
+	
+	public void removeDiscoveryAgentsLoadedListener(IDiscoveryAgentsLoadedListener listener) {
+		listeners.remove(listener);
+	}
+	
+	private void fireDiscoveryAgentsLoaded() {
+		for (IDiscoveryAgentsLoadedListener listener : listeners) {
+			listener.agentsAreLoaded();
+		}
+	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ToggleDiscoveryAgentAction.java	Fri Jun 25 15:35:46 2010 -0500
@@ -0,0 +1,59 @@
+/**
+* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of the License "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - initial contribution.
+*
+* Contributors:
+*
+* Description: 
+*
+*/
+
+package com.nokia.carbide.remoteconnections.internal;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+
+import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
+import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent;
+
+public class ToggleDiscoveryAgentAction extends Action {
+
+	private final IDeviceDiscoveryAgent agent;
+
+	public ToggleDiscoveryAgentAction(IDeviceDiscoveryAgent agent) {
+		this.agent = agent;
+		setChecked(agent.isRunning());
+	}
+
+	@Override
+	public void run() {
+		try {
+			if (agent.isRunning())
+				agent.stop();
+			else
+				agent.start();
+			setChecked(agent.isRunning());
+		} catch (CoreException e) {
+			RemoteConnectionsActivator.logError(e);
+		}
+	}
+	
+	@Override
+	public int getStyle() {
+		return IAction.AS_CHECK_BOX;
+	}
+	
+	@Override
+	public String getText() {
+		return agent.getDisplayName();
+	}
+}
+
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/api/IDeviceDiscoveryAgent.java	Wed Jun 23 15:28:44 2010 -0500
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/api/IDeviceDiscoveryAgent.java	Fri Jun 25 15:35:46 2010 -0500
@@ -87,6 +87,18 @@
 	 */
 	IPrerequisiteStatus getPrerequisiteStatus();
 	
+	/**
+	 * Return whether this discovery agent is running
+	 * @return boolean
+	 */
+	boolean isRunning();
+	
+	/**
+	 * The unique id for this agent
+	 * @return String
+	 */
+	String getId();
+	
 	// In addition, there may need to be an additional API to do a deeper form of discovery for
 	// connection mechanisms that require pairing (like BT or Wifi). In these cases, normal discovery
 	// will probably be for already paired devices, however, the user will want to discover all 
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java	Wed Jun 23 15:28:44 2010 -0500
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java	Fri Jun 25 15:35:46 2010 -0500
@@ -76,19 +76,22 @@
 
 import com.nokia.carbide.remoteconnections.Messages;
 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
+import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator.IDiscoveryAgentsLoadedListener;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
+import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
+import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
+import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectionType;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
-import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
-import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
-import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
+import com.nokia.carbide.remoteconnections.internal.ToggleDiscoveryAgentAction;
 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
+import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus;
 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener;
-import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus;
+import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent;
 import com.nokia.carbide.remoteconnections.internal.registry.Registry;
 import com.nokia.carbide.remoteconnections.internal.ui.ConnectionUIUtils;
 import com.nokia.carbide.remoteconnections.settings.ui.SettingsWizard;
@@ -111,6 +114,7 @@
 	private List<Action> actions;
 	private List<Action> connectionSelectedActions;
 	private List<Action> serviceSelectedActions;
+	private List<Action> discoveryAgentActions;
 	private static final String UID = ".uid"; //$NON-NLS-1$
 
 	private static final ImageDescriptor CONNECTION_NEW_IMGDESC = RemoteConnectionsActivator.getImageDescriptor("icons/connectionNew.png"); //$NON-NLS-1$
@@ -131,7 +135,7 @@
 
 	// handle, do not dispose
 	private Font boldViewerFont;
-	
+
 	private TreeNode[] loadConnections() {
 		if (serviceToListenerMap == null)
 			serviceToListenerMap = new HashMap<IConnectedService, IStatusChangedListener>();
@@ -624,8 +628,30 @@
 		fillLocalToolBar(bars.getToolBarManager());
 	}
 
-	private void fillLocalPullDown(IMenuManager manager) {
-		// nothing for now
+	private void fillLocalPullDown(final IMenuManager manager) {
+		if (discoveryAgentActions.isEmpty()) {
+			IDiscoveryAgentsLoadedListener listener = new IDiscoveryAgentsLoadedListener() {
+				public void agentsAreLoaded() {
+					makeToggleDiscoveryAgentActions();
+					addDiscoveryAgentActions(manager);
+					Display.getDefault().asyncExec(new Runnable() {
+						public void run() {
+							manager.update(true);
+						}
+					});
+				}
+			};
+			RemoteConnectionsActivator.getDefault().addDiscoveryAgentsLoadedListener(listener);
+		}
+		else {
+			addDiscoveryAgentActions(manager);
+		}
+	}
+
+	private void addDiscoveryAgentActions(IMenuManager manager) {
+		for (Action action : discoveryAgentActions) {
+			manager.add(action);
+		}
 	}
 
 	private void fillContextMenu(IMenuManager manager) {
@@ -827,8 +853,19 @@
 		
 		enableConnectionSelectedActions(false);
 		enableServiceSelectedActions(false);
+		
+		makeToggleDiscoveryAgentActions();
 	}
 	
+	private void makeToggleDiscoveryAgentActions() {
+		discoveryAgentActions = new ArrayList<Action>();
+		Collection<IDeviceDiscoveryAgent> discoveryAgents = RemoteConnectionsActivator.getDefault().getDiscoveryAgents();
+		for (IDeviceDiscoveryAgent agent : discoveryAgents) {
+			discoveryAgentActions.add(new ToggleDiscoveryAgentAction(agent));
+		}
+		
+	}
+
 	private void enableConnectionSelectedActions(boolean enable) {
 		for (Action action : connectionSelectedActions) {
 			action.setEnabled(enable);