From 8764348c51e1d68ef77e0360f64b02ed62ca7789 Mon Sep 17 00:00:00 2001 From: Evgeny Gryaznov Date: Thu, 27 Mar 2008 21:30:36 +0000 Subject: [PATCH] disconnect on computer lock git-svn-id: https://webim.svn.sourceforge.net/svnroot/webim/trunk@48 c66351dc-e62f-0410-b875-e3a5c0b9693f --- .../WebIMTray/LockNotificationForm.cs | 98 +++++++++++++++++++ src/webimtray/WebIMTray/Main.cs | 18 +++- src/webimtray/WebIMTray/Main.resx | 54 +++++----- src/webimtray/WebIMTray/Options.cs | 9 ++ .../OptionsConnectionPanel.Designer.cs | 2 +- .../options/OptionsConnectionPanel.cs | 7 ++ src/webimtray/WebIMTray/webImTray.csproj | 5 +- 7 files changed, 164 insertions(+), 29 deletions(-) create mode 100644 src/webimtray/WebIMTray/LockNotificationForm.cs diff --git a/src/webimtray/WebIMTray/LockNotificationForm.cs b/src/webimtray/WebIMTray/LockNotificationForm.cs new file mode 100644 index 00000000..a38535d5 --- /dev/null +++ b/src/webimtray/WebIMTray/LockNotificationForm.cs @@ -0,0 +1,98 @@ +using System; +using System.Drawing; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace webImTray { + + /// + /// Base class for a form that wants to be notified of Windows + /// session lock / unlock events + /// + public abstract class LockNotificationForm : Form { + // from wtsapi32.h + private const int NotifyForThisSession = 0; + + // from winuser.h + private const int SessionChangeMessage = 0x02B1; + private const int SessionLockParam = 0x7; + private const int SessionUnlockParam = 0x8; + + [DllImport("wtsapi32.dll")] + private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags); + + [DllImport("wtsapi32.dll")] + private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd); + + // flag to indicate if we've registered for notifications or not + private bool registered = false; + + /// + /// Is this form receiving lock / unlock notifications + /// + protected bool ReceivingLockNotifications { + get { return registered; } + } + + /// + /// Unregister for event notifications + /// + protected override void Dispose(bool disposing) { + if (registered) { + WTSUnRegisterSessionNotification(Handle); + registered = false; + } + + base.Dispose(disposing); + return; + } + + /// + /// Register for event notifications + /// + protected override void OnHandleCreated(EventArgs e) { + base.OnHandleCreated(e); + + // WtsRegisterSessionNotification requires Windows XP or higher + bool haveXp = Environment.OSVersion.Platform == PlatformID.Win32NT && + (Environment.OSVersion.Version.Major > 5 || + (Environment.OSVersion.Version.Major == 5 && + Environment.OSVersion.Version.Minor >= 1)); + + if (haveXp) + registered = WTSRegisterSessionNotification(Handle, NotifyForThisSession); + + return; + } + + /// + /// The windows session has been locked + /// + protected virtual void OnSessionLock() { + return; + } + + /// + /// The windows session has been unlocked + /// + protected virtual void OnSessionUnlock() { + return; + } + + /// + /// Process windows messages + /// + protected override void WndProc(ref Message m) { + // check for session change notifications + if (m.Msg == SessionChangeMessage) { + if (m.WParam.ToInt32() == SessionLockParam) + OnSessionLock(); + else if (m.WParam.ToInt32() == SessionUnlockParam) + OnSessionUnlock(); + } + + base.WndProc(ref m); + return; + } + } +} diff --git a/src/webimtray/WebIMTray/Main.cs b/src/webimtray/WebIMTray/Main.cs index fa614cdc..11bb2084 100644 --- a/src/webimtray/WebIMTray/Main.cs +++ b/src/webimtray/WebIMTray/Main.cs @@ -9,7 +9,7 @@ using System.Data; namespace webImTray { - public partial class MainWindow : System.Windows.Forms.Form { + public partial class MainWindow : LockNotificationForm { public MainWindow() { InitializeComponent(); @@ -46,6 +46,10 @@ namespace webImTray { #endif } + void navigateBlank() { + webBrowser1.Navigate("about:blank"); + } + private void showWindow() { this.Visible = true; this.Activate(); @@ -146,5 +150,17 @@ namespace webImTray { private void toolHideWindow_Click(object sender, EventArgs e) { hideWindow(); } + + protected override void OnSessionLock() { + if (Options.DisconnectOnLock) { + navigateBlank(); + } + } + + protected override void OnSessionUnlock() { + if (Options.DisconnectOnLock) { + navigateThere(); + } + } } } diff --git a/src/webimtray/WebIMTray/Main.resx b/src/webimtray/WebIMTray/Main.resx index aff1f91d..8965f0bd 100644 --- a/src/webimtray/WebIMTray/Main.resx +++ b/src/webimtray/WebIMTray/Main.resx @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bceRWtaLk0VO1XW72D5Vckd2gMBfq8zd + pmUG62NJvKM4+XyziDk24xmfWoGEs1c0gHPmbrPTpHNJKOCo2G1mZs20zcwUJ5yp1AB5+8/zEwgF5GMV + Dxh4AAAAAElFTkSuQmCC diff --git a/src/webimtray/WebIMTray/Options.cs b/src/webimtray/WebIMTray/Options.cs index d7ded258..45ce1805 100644 --- a/src/webimtray/WebIMTray/Options.cs +++ b/src/webimtray/WebIMTray/Options.cs @@ -38,6 +38,15 @@ namespace webImTray { } } + public static bool DisconnectOnLock { + get { + return Application.UserAppDataRegistry.GetValue("disconnectonlock", "true").ToString().ToLower().Equals("true"); + } + set { + Application.UserAppDataRegistry.SetValue("disconnectonlock", value.ToString()); + } + } + public static bool ShowInTaskBar { get { return Application.UserAppDataRegistry.GetValue("showintaskbar", "false").ToString().ToLower().Equals("true"); diff --git a/src/webimtray/WebIMTray/options/OptionsConnectionPanel.Designer.cs b/src/webimtray/WebIMTray/options/OptionsConnectionPanel.Designer.cs index fe620d95..98536eb6 100644 --- a/src/webimtray/WebIMTray/options/OptionsConnectionPanel.Designer.cs +++ b/src/webimtray/WebIMTray/options/OptionsConnectionPanel.Designer.cs @@ -144,7 +144,6 @@ namespace webImTray { // autoDisconnect // this.autoDisconnect.AutoSize = true; - this.autoDisconnect.Enabled = false; this.autoDisconnect.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.autoDisconnect.Location = new System.Drawing.Point(23, 96); this.autoDisconnect.Margin = new System.Windows.Forms.Padding(4); @@ -153,6 +152,7 @@ namespace webImTray { this.autoDisconnect.TabIndex = 3; this.autoDisconnect.Text = "Become idle if the computer is locked"; this.autoDisconnect.UseVisualStyleBackColor = true; + this.autoDisconnect.CheckedChanged += new System.EventHandler(this.autoDisconnect_CheckedChanged); // // groupBox2 // diff --git a/src/webimtray/WebIMTray/options/OptionsConnectionPanel.cs b/src/webimtray/WebIMTray/options/OptionsConnectionPanel.cs index b6a9a14c..557944e0 100644 --- a/src/webimtray/WebIMTray/options/OptionsConnectionPanel.cs +++ b/src/webimtray/WebIMTray/options/OptionsConnectionPanel.cs @@ -17,6 +17,7 @@ namespace webImTray { void OptionsPanel.apply() { if (modified) { Options.WebIMServer = webimServer.Text; + Options.DisconnectOnLock = autoDisconnect.Checked; if (forceRefresh.Checked) { Options.ForceRefreshTime = forceRefreshTime.Value; } else { @@ -27,6 +28,7 @@ namespace webImTray { void OptionsPanel.initialize() { webimServer.Text = Options.WebIMServer; + autoDisconnect.Checked = Options.DisconnectOnLock; decimal refreshTime = Options.ForceRefreshTime; forceRefreshTime.Enabled = forceRefresh.Checked = refreshTime != 0; @@ -60,5 +62,10 @@ namespace webImTray { private void showUserPropertiesOnline(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start(Options.WebIMServer + Options.SETTINGS_PAGE); } + + private void autoDisconnect_CheckedChanged(object sender, EventArgs e) { + modified = true; + PanelModified.Invoke(); + } } } diff --git a/src/webimtray/WebIMTray/webImTray.csproj b/src/webimtray/WebIMTray/webImTray.csproj index 00acdc13..ea9bfd15 100644 --- a/src/webimtray/WebIMTray/webImTray.csproj +++ b/src/webimtray/WebIMTray/webImTray.csproj @@ -91,6 +91,9 @@ + + Form + UserControl @@ -165,4 +168,4 @@ - + \ No newline at end of file