KeeAgent.KeeAgentExt.Initialize C# (CSharp) Method

Initialize() public method

public Initialize ( IPluginHost host ) : bool
host IPluginHost
return bool
        public override bool Initialize(IPluginHost host)
        {
            pluginHost = host;
              uiHelper = new UIHelper(pluginHost);
              removeKeyList = new List<ISshKey>();
              debug = (pluginHost
              .CommandLineArgs[AppDefs.CommandLineOptions.Debug] != null);

              LoadOptions();

              if (debug) Log("Loading KeeAgent...");

              var isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
              var domainSocketPath =
            Environment.GetEnvironmentVariable (UnixClient.SshAuthSockName);
              try {
            if (Options.AgentMode != AgentMode.Client) {
              if (isWindows) {
            // In windows, try to start an agent. If Pageant is running, we will
            // get an exception.
            try {
              var pagent = new PageantAgent();
              pagent.Locked += PageantAgent_Locked;
              pagent.KeyUsed += PageantAgent_KeyUsed;
              pagent.KeyAdded += PageantAgent_KeyAdded;
              pagent.KeyRemoved += PageantAgent_KeyRemoved;
              pagent.MessageReceived += PageantAgent_MessageReceived;
              // IMPORTANT: if you change either of these callbacks, you need
              // to make sure that they do not block the main event loop.
              pagent.FilterKeyListCallback = FilterKeyList;
              pagent.ConfirmUserPermissionCallback = Default.ConfirmCallback;
              agent = pagent;
              if (Options.UseCygwinSocket) {
                StartCygwinSocket();
              }
              if (Options.UseMsysSocket) {
                StartMsysSocket();
              }
            } catch (PageantRunningException) {
              if (Options.AgentMode != AgentMode.Auto) {
                throw;
              }
            }
              } else {
            // In Unix, we only try to start an agent if Agent mode was explicitly
            // selected or there is no agent running (indicated by environment variable).
            if (Options.AgentMode == AgentMode.Server || string.IsNullOrWhiteSpace (domainSocketPath)) {
              var unixAgent = new UnixAgent();
              unixAgent.Locked += PageantAgent_Locked;
              unixAgent.KeyUsed += PageantAgent_KeyUsed;
              unixAgent.KeyAdded += PageantAgent_KeyAdded;
              unixAgent.KeyRemoved += PageantAgent_KeyRemoved;
              unixAgent.MessageReceived += PageantAgent_MessageReceived;
              // IMPORTANT: if you change either of these callbacks, you need
              // to make sure that they do not block the main event loop.
              unixAgent.FilterKeyListCallback = FilterKeyList;
              unixAgent.ConfirmUserPermissionCallback = Default.ConfirmCallback;
              agent = unixAgent;
              try {
                var socketPath = Options.UnixSocketPath.ExpandEnvironmentVariables();
                unixAgent.StartUnixSocket (socketPath);
              } catch (ArgumentNullException) {
                var autoModeMessage = Options.AgentMode == AgentMode.Auto
                  ? " to use KeeAgent in Agent mode or enable an external SSH agent in your " +
                  "desktop session manager to use KeeAgent in Client mode."
                  : ".";
                MessageService.ShowWarning("KeeAgent: No path specified for Agent socket file.",
                  "Please enter a file in the KeeAgent options (Tools > Options... > KeeAgent tab)" +
                  autoModeMessage);
              } catch (Exception ex) {
                MessageService.ShowWarning(ex.Message);
              }
            }
              }
            }
            if (agent == null) {
              if (isWindows) {
            agent = new PageantClient();
              } else {
            agent = new UnixClient();
              }
            }
            pluginHost.MainWindow.FileOpened += MainForm_FileOpened;
            pluginHost.MainWindow.FileClosingPost += MainForm_FileClosing;
            pluginHost.MainWindow.FileClosed += MainForm_FileClosed;
            // load all database that are already opened
            foreach (var database in pluginHost.MainWindow.DocumentManager.Documents) {
              MainForm_FileOpened(this, new FileOpenedEventArgs(database.Database));
            }
            AddMenuItems();
            GlobalWindowManager.WindowAdded += WindowAddedHandler;
            MessageService.MessageShowing += MessageService_MessageShowing;
            columnProvider = new KeeAgentColumnProvider(this);
            host.ColumnProviderPool.Add(columnProvider);
            SprEngine.FilterCompile += SprEngine_FilterCompile;
            SprEngine.FilterPlaceholderHints.Add(keyFilePathSprPlaceholder);
            SprEngine.FilterPlaceholderHints.Add(identFileOptSprPlaceholder);
            return true;
              } catch (PageantRunningException) {
            ShowPageantRunningErrorMessage();
              } catch (Exception ex) {
            MessageService.ShowWarning("KeeAgent failed to load:", ex.Message);
              }
              Terminate();
              return false;
        }

Usage Example

示例#1
0
        public void InitializeTest()
        {
            const string initalizeResultName = "KEEAGENT_INIT_RESULT";

              using (KeePassAppDomain testDomain1 = new KeePassAppDomain()) {
            testDomain1.StartKeePass(true, true, 1, true);
            testDomain1.DoCallBack(delegate()
            {
              IPluginHost td1PluginHost = KeePass.Program.MainForm.PluginHost;
              try {
            KeeAgentExt td1KeeAgentExt = new KeeAgentExt();
            KeePass.Program.MainForm.Invoke((MethodInvoker)delegate()
            {
              bool td1InitalizeResult = td1KeeAgentExt.Initialize(td1PluginHost);
              td1KeeAgentExt.Terminate();
              AppDomain.CurrentDomain.SetData(initalizeResultName,
                td1InitalizeResult);
            });
              } catch (Exception) {
            // TODO do we want to pass this exception back to test?
              }
            });

            bool expected = true;
            bool actual = (bool)testDomain1.GetData(initalizeResultName);
            Assert.AreEqual(expected, actual);
              }
        }
All Usage Examples Of KeeAgent.KeeAgentExt::Initialize