Click here to Skip to main content
15,896,726 members
Articles / Programming Languages / C#

MinLock, a KeePass 2.x Plugin to keep minimized KeePass Locked

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Jun 2012CPOL2 min read 55.5K   2.2K   2  
MinLock a KeePass 2.x plugin to keep it locked when minimized.
using System;
using System.Windows.Forms;

using KeePass.Plugins;

namespace MinLock
{
  public sealed class MinLockExt : Plugin
  {
    IPluginHost m_Host;
    Timer m_Timer;

    public override bool Initialize(IPluginHost host)
    {
      m_Host = host;

      // Upon unlocking, the database is opened and this event fires;
      // it likely fires other times too (e.g. opening db from menu).
      m_Host.MainWindow.FileOpened += MainWindow_FileOpened;

      return base.Initialize(host);
    }

    public override void Terminate()
    {
      KillTimer();
      base.Terminate();
    }

    void KillTimer()
    {
      if (m_Timer != null)
      {
        m_Timer.Dispose();
        m_Timer = null;
      }
    }

    void MainWindow_FileOpened(object sender, KeePass.Forms.FileOpenedEventArgs e)
    {
      if (m_Timer == null && m_Host.MainWindow.WindowState == FormWindowState.Minimized)
      {
        // Start a Windows.Forms.Timer, because it's based on the event loop it
        // can't interrupt current calls, though calls to Application.DoEvents
        // could wreak havoc, but that doesn't appear to be a problem here.
        m_Timer = new Timer();
        m_Timer.Interval = 1;
        m_Timer.Tick += Timer_Tick;
        m_Timer.Start();
      }
    }

    void Timer_Tick(object sender, EventArgs e)
    {
      KillTimer();

      if (m_Host.MainWindow.WindowState == FormWindowState.Minimized &&
          m_Host.MainWindow.IsAtLeastOneFileOpen())
      {
        m_Host.MainWindow.LockAllDocuments();
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions