From 51e134fa5bb72ca7d361f0a865ede9868add488d Mon Sep 17 00:00:00 2001 From: Ben Hutchison Date: Tue, 27 Apr 2021 09:42:41 -0700 Subject: [PATCH] Added update checking. --- .../KeePassTrayIconLockState.csproj | 6 ++- .../KeePassTrayIconLockStateExt.cs | 38 +++++++++---------- .../Properties/AssemblyInfo.cs | 23 ++--------- KeePassTrayIconLockState/version.txt | 3 ++ 4 files changed, 31 insertions(+), 39 deletions(-) create mode 100644 KeePassTrayIconLockState/version.txt diff --git a/KeePassTrayIconLockState/KeePassTrayIconLockState.csproj b/KeePassTrayIconLockState/KeePassTrayIconLockState.csproj index f418bed..932cfa5 100644 --- a/KeePassTrayIconLockState/KeePassTrayIconLockState.csproj +++ b/KeePassTrayIconLockState/KeePassTrayIconLockState.csproj @@ -12,6 +12,7 @@ v4.8 512 true + 8524;8509 true @@ -58,7 +59,7 @@ - 2.0.18.1 + 2.0.18.2 @@ -79,5 +80,8 @@ + + + \ No newline at end of file diff --git a/KeePassTrayIconLockState/KeePassTrayIconLockStateExt.cs b/KeePassTrayIconLockState/KeePassTrayIconLockStateExt.cs index b7f8a13..b0ddc5d 100644 --- a/KeePassTrayIconLockState/KeePassTrayIconLockStateExt.cs +++ b/KeePassTrayIconLockState/KeePassTrayIconLockStateExt.cs @@ -1,7 +1,6 @@ #nullable enable using System; -using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Threading.Tasks; @@ -17,17 +16,13 @@ public class KeePassTrayIconLockStateExt: Plugin { internal static readonly TimeSpan STARTUP_DURATION = TimeSpan.FromMilliseconds(2000); - private readonly IDictionary iconsByFileOpenState = new Dictionary(); - private readonly StoredProperty databaseOpenState = new StoredProperty(DatabaseOpenState.CLOSED); + private readonly StoredProperty databaseOpenState = new(); private IPluginHost keePassHost = null!; private Property trayIcon = null!; - public KeePassTrayIconLockStateExt() { - iconsByFileOpenState.Add(DatabaseOpenState.CLOSED, Resources.locked); - iconsByFileOpenState.Add(DatabaseOpenState.OPENING, Resources.unlocking); - iconsByFileOpenState.Add(DatabaseOpenState.OPEN, Resources.unlocked); - } + public override Image SmallIcon => Resources.plugin_image; + public override string UpdateUrl { get; } = @"https://raw.githubusercontent.com/Aldaviva/KeePassTrayIconLockState/master/KeePassTrayIconLockState/version.txt"; public override bool Initialize(IPluginHost host) { keePassHost = host; @@ -41,7 +36,7 @@ public override bool Initialize(IPluginHost host) { Property statusBarText = new NativeReadableProperty(statusBarInfo, nameof(ToolStripItem.Text), nameof(ToolStripItem.TextChanged)); Property isProgressBarVisible = new NativeReadableProperty(progressBar, nameof(ToolStripItem.Visible), nameof(ToolStripItem.VisibleChanged)); - statusBarText.PropertyChanged += (sender, args) => { + statusBarText.PropertyChanged += (_, args) => { if (args.NewValue == KPRes.OpeningDatabase2) { /* * When the status bar text changes to "Opening database...", set the db open state to OPENING. @@ -50,15 +45,15 @@ public override bool Initialize(IPluginHost host) { } }; - isProgressBarVisible.PropertyChanged += (sender, args) => { + isProgressBarVisible.PropertyChanged += (_, args) => { if (!args.NewValue && databaseOpenState.Value == DatabaseOpenState.OPENING) { /* * When the database is being opened and the progress bar gets hidden, it means the database was finished being decrypted, but was it successful or unsuccessful? * Sadly there is no good way to tell, so instead we wait 100 ms for the FileOpen event to be fired. - * If it is fired, the db open state moves from OPENING to OPEN. + * If it is fired, the db open state moves from OPENING to OPEN (above). * Otherwise, assume that the database failed to decrypt and set the db open state to CLOSED. */ - Task.Delay(100).ContinueWith(task => { + Task.Delay(100).ContinueWith(_ => { if (databaseOpenState.Value == DatabaseOpenState.OPENING) { // failed to decrypt, otherwise this would have been set to OPEN by the FileOpen event above. databaseOpenState.Value = DatabaseOpenState.CLOSED; } @@ -66,23 +61,23 @@ public override bool Initialize(IPluginHost host) { } else if (args.NewValue && databaseOpenState.Value == DatabaseOpenState.CLOSED && statusBarText.Value == KPRes.OpeningDatabase2) { /* - * When the database is closed and the status bar says "Opening database..." and the progress bar is shown, it means the user already failed the previous decryption attempt - * and is retrying after submitting another password, so set the db opening state to OPENING. + * When the database is closed and the status bar says "Opening database..." and the progress bar gets shown, it means the user already failed the previous decryption + * attempt and is retrying after submitting another password, so set the db opening state to OPENING. */ databaseOpenState.Value = DatabaseOpenState.OPENING; } }; - trayIcon = DerivedProperty.Create(databaseOpenState, openState => new TrayIcon(iconsByFileOpenState[openState], openState != DatabaseOpenState.CLOSED)); + trayIcon = DerivedProperty.Create(databaseOpenState, openState => new TrayIcon(getIcon(openState), openState != DatabaseOpenState.CLOSED)); - trayIcon.PropertyChanged += (sender, args) => renderTrayIcon(); + trayIcon.PropertyChanged += (_, _) => renderTrayIcon(); /* * KeePass sets its own icon at some indeterminate time after startup, so repeatedly set our own icon every 8 ms for 2 seconds to make sure our icon isn't overridden. */ - Timer startupTimer = new Timer { Enabled = true, Interval = 8 }; + Timer startupTimer = new() { Enabled = true, Interval = 8 }; startupTimer.Tick += delegate { renderTrayIcon(); }; - Task.Delay(STARTUP_DURATION).ContinueWith(task => startupTimer.Stop()); + Task.Delay(STARTUP_DURATION).ContinueWith(_ => startupTimer.Stop()); renderTrayIcon(); return true; @@ -96,7 +91,12 @@ private void renderTrayIcon() { keepassIcon.Visible = iconToRender.isVisible; } - public override Image SmallIcon => Resources.plugin_image; + private static Icon getIcon(DatabaseOpenState databaseOpenState) => databaseOpenState switch { + DatabaseOpenState.CLOSED => Resources.locked, + DatabaseOpenState.OPENING => Resources.unlocking, + DatabaseOpenState.OPEN => Resources.unlocked, + _ => throw new ArgumentOutOfRangeException(nameof(databaseOpenState), databaseOpenState, nameof(getIcon)) + }; } diff --git a/KeePassTrayIconLockState/Properties/AssemblyInfo.cs b/KeePassTrayIconLockState/Properties/AssemblyInfo.cs index 084f9f3..39a9a05 100644 --- a/KeePassTrayIconLockState/Properties/AssemblyInfo.cs +++ b/KeePassTrayIconLockState/Properties/AssemblyInfo.cs @@ -2,9 +2,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. [assembly: AssemblyTitle("KeePassTrayIconLockState")] [assembly: AssemblyDescription("Show the locked symbol in the KeePass tray icon when the password database is closed or being decrypted.")] [assembly: AssemblyConfiguration("")] @@ -14,25 +11,13 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] -// The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("e980abdc-e56d-4e9c-a322-afbe9d9092b4")] -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.1.0")] -[assembly: AssemblyFileVersion("1.0.1.0")] +// Remember to also update version.txt when changing these version attributes so that online update checking works. +// https://keepass.info/help/v2_dev/plg_index.html#upd +[assembly: AssemblyVersion("1.0.2.0")] +[assembly: AssemblyFileVersion("1.0.2.0")] [assembly: InternalsVisibleTo("Test")] \ No newline at end of file diff --git a/KeePassTrayIconLockState/version.txt b/KeePassTrayIconLockState/version.txt new file mode 100644 index 0000000..0c6cfc0 --- /dev/null +++ b/KeePassTrayIconLockState/version.txt @@ -0,0 +1,3 @@ +: +KeePassTrayIconLockState:1.0.2 +: \ No newline at end of file