Skip to content

Commit

Permalink
Added update checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aldaviva committed Apr 27, 2021
1 parent d53f5af commit 51e134f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
6 changes: 5 additions & 1 deletion KeePassTrayIconLockState/KeePassTrayIconLockState.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<NoWarn>8524;8509</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -58,7 +59,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="ILRepack.Lib.MSBuild.Task">
<Version>2.0.18.1</Version>
<Version>2.0.18.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand All @@ -79,5 +80,8 @@
<ItemGroup>
<None Include="Resources\plugin image.png" />
</ItemGroup>
<ItemGroup>
<None Include="version.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
38 changes: 19 additions & 19 deletions KeePassTrayIconLockState/KeePassTrayIconLockStateExt.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -17,17 +16,13 @@ public class KeePassTrayIconLockStateExt: Plugin {

internal static readonly TimeSpan STARTUP_DURATION = TimeSpan.FromMilliseconds(2000);

private readonly IDictionary<DatabaseOpenState, Icon> iconsByFileOpenState = new Dictionary<DatabaseOpenState, Icon>();
private readonly StoredProperty<DatabaseOpenState> databaseOpenState = new StoredProperty<DatabaseOpenState>(DatabaseOpenState.CLOSED);
private readonly StoredProperty<DatabaseOpenState> databaseOpenState = new();

private IPluginHost keePassHost = null!;
private Property<TrayIcon> 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;
Expand All @@ -41,7 +36,7 @@ public override bool Initialize(IPluginHost host) {
Property<string> statusBarText = new NativeReadableProperty<string>(statusBarInfo, nameof(ToolStripItem.Text), nameof(ToolStripItem.TextChanged));
Property<bool> isProgressBarVisible = new NativeReadableProperty<bool>(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.
Expand All @@ -50,39 +45,39 @@ 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;
}
});

} 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<TrayIcon>.Create(databaseOpenState, openState => new TrayIcon(iconsByFileOpenState[openState], openState != DatabaseOpenState.CLOSED));
trayIcon = DerivedProperty<TrayIcon>.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;
Expand All @@ -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))
};

}

Expand Down
23 changes: 4 additions & 19 deletions KeePassTrayIconLockState/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")]
Expand All @@ -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")]
3 changes: 3 additions & 0 deletions KeePassTrayIconLockState/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:
KeePassTrayIconLockState:1.0.2
:

0 comments on commit 51e134f

Please sign in to comment.