Exemple #1
0
        /// <summary>
        /// Activates an application agent depending on the context.
        /// The monitorInfo parameter has all the information to make
        /// this decision. Depending on the active foreground process,
        /// the appropriate agent is activated.  if there is no dedicated
        /// agent for the process, the generic agent is used.
        /// If a dialog the foreground window, the dialog agent is activated.
        /// If a menu has been activated, the menu agent is activated
        /// </summary>
        /// <param name="monitorInfo">Foreground window/process info</param>
        private void activateAppAgent(WindowActivityMonitorInfo monitorInfo)
        {
            if (_inActivateAppAgent)
            {
                Log.Debug("Already inside. returning");
                return;
            }

            Log.Debug("Before syncsetagent");
            lock (_syncActivateAgent)
            {
                _inActivateAppAgent = true;
                Log.Debug("After syncsetagent");
                try
                {
                    bool handled = false;

                    Log.Debug(monitorInfo.ToString());

                    // did a request for displaying the contextual
                    // menu come in?  If so handle it.

                    bool getContextMenu = _getContextMenu;

                    Log.Debug("getContextMenu: " + getContextMenu);

                    _getContextMenu = false;

                    String processName = monitorInfo.FgProcess.ProcessName;

                    // first check if there is an ad-hoc agent, if so,
                    // activate it
                    Log.Debug("Looking for adhoc agent for " + monitorInfo.FgHwnd);
                    IApplicationAgent agent = _agentsCache.GetAgent(monitorInfo.FgHwnd);
                    if (agent == null)
                    {
                        // check if a dialog or menu is active
                        Log.Debug("Adhoc agent not present for " + monitorInfo.FgHwnd);

                        if (EnableContextualMenusForDialogs &&
                            (String.Compare(processName, _currentProcessName, true) != 0) &&
                            isDialog(monitorInfo))
                        {
                            Log.Debug("Fg window is a dialog.  Setting agent to dialog agent");

                            agent = _dialogAgent;
                        }
                        else if (EnableContextualMenusForMenus && isMenu(monitorInfo))
                        {
                            Log.Debug("Fg window is a menu.  Setting agent to menu agent");
                            agent = _menuControlAgent;
                        }
                        else
                        {
                            // check if there is a dedicated agent for
                            // this process
                            Log.Debug("Getting agent for " + processName);
                            agent = _agentsCache.GetAgent(monitorInfo.FgProcess);
                        }
                    }
                    else
                    {
                        Log.Debug("Adhoc agent IS present for " + monitorInfo.FgHwnd);
                    }

                    Log.Debug("Current agent: " + ((_currentAgent != null) ?
                                                   _currentAgent.Name : "null") +
                              ", agent:  " + ((agent != null) ? agent.Name : "null"));

                    // if there is an agent switch, query the current agent
                    // if it is OK to switch. Some agents may not allow
                    // the switch
                    if (_currentAgent != null && _currentAgent != agent)
                    {
                        bool allowSwitch = _currentAgent.QueryAgentSwitch(agent);
                        Log.Debug("CurrentAgent is " + _currentAgent.Name + ", queryAgentSwitch: " + allowSwitch);
                        if (!allowSwitch)
                        {
                            _currentAgent.OnFocusChanged(monitorInfo, ref handled);
                            _textControlAgent = _currentAgent.TextControlAgent;
                            return;
                        }

                        _currentAgent.OnFocusLost();
                    }

                    // if there was a request to display
                    // contextual menu, do so
                    if (getContextMenu)
                    {
                        if (agent == null)
                        {
                            agent = _genericAppAgent;
                        }

                        Log.Debug("agent : " + agent.Name);
                        agent.OnContextMenuRequest(monitorInfo);
                        return;
                    }

                    // Inform the new agent about the current
                    // focused element so it can display the scanner that
                    // is appropriated for the context.
                    if (agent != null)
                    {
                        Log.Debug("Trying agent " + agent.Name);
                        agent.OnFocusChanged(monitorInfo, ref handled);
                        Log.Debug("Returned from agent.OnFOcus");
                    }

                    // If we have reached here, it means there was no
                    // agent. Just use the default generic agent. See
                    // if it will handle it
                    if (!handled)
                    {
                        Log.Debug("Did not find agent for " + processName + ". trying generic app agent");
                        Log.Debug("_genericAppAgent is " + ((_genericAppAgent != null) ? "not null" : "null"));
                        agent = _genericAppAgent;
                        try
                        {
                            agent.OnFocusChanged(monitorInfo, ref handled);
                        }
                        catch (Exception ex)
                        {
                            Log.Exception(ex);
                        }
                    }

                    // even the generic agent refused.  Use the null agent
                    // as the last resort
                    Log.Debug("handled " + handled);
                    if (!handled)
                    {
                        Log.Debug("generic app agent refused. Using null agent");
                        agent = _nullAgent;
                        agent.OnFocusChanged(monitorInfo, ref handled);
                    }

                    updateCurrentAgentAndNotify(agent);
                }
                catch (Exception ex)
                {
                    Log.Debug(ex.ToString());
                }
                finally
                {
                    _inActivateAppAgent = false;
                }
            }

            Log.Debug("Return");
        }