Skip to content

Commit

Permalink
Improve KeePass restart after updating plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Rookiestyle committed Sep 9, 2020
1 parent 47c505e commit 5b7aac2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
39 changes: 30 additions & 9 deletions src/EarlyUpdateCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private enum UpdateCheckStatus
private IStatusLogger m_slUpdateCheck = null;
private Form m_CheckProgress = null;
private bool m_bRestartInvoke = false;
private bool m_bRestartTriggered = false;
private IStatusLogger m_slUpdatePlugins = null;
private string m_LanguageIso = null;
private bool m_bUpdateCheckDone = false;
Expand Down Expand Up @@ -136,7 +137,7 @@ private void MainWindow_FormLoadPost(object sender, EventArgs e)
private void WindowAdded(object sender, GwmWindowEventArgs e)
{
if (!PluginConfig.Active) return;
PluginDebug.AddInfo("Form added", e.Form.Name, e.Form.GetType().FullName, DebugPrint);
PluginDebug.AddInfo("Form added", 0, e.Form.Name, e.Form.GetType().FullName, DebugPrint);
if (e.Form is UpdateCheckForm)
{
if (m_CheckProgress != null)
Expand Down Expand Up @@ -177,8 +178,7 @@ private void LanguageFormAdded(object sender, EventArgs e)

private void WindowRemoved(object sender, GwmWindowEventArgs e)
{
if ((m_kpf != null) && (e.Form is KeyPromptForm))
m_kpf = null;
if ((m_kpf != null) && (e.Form is KeyPromptForm)) m_kpf = null;
}

#region Check for updates
Expand Down Expand Up @@ -235,6 +235,11 @@ private void KeyPromptFormAdded()
PluginDebug.AddInfo("UpdateCheck finished ", 0, DebugPrint);
}
if ((m_UpdateCheckStatus == UpdateCheckStatus.NotChecked) || (m_UpdateCheckStatus == UpdateCheckStatus.Error)) UpdateCheckEx.Run(false, null);
if (m_bRestartTriggered)
{
m_bRestartTriggered = false;
return;
}
CheckPluginLanguages();
}

Expand Down Expand Up @@ -501,7 +506,7 @@ private void OnUpdateCheckFormShown(object sender, EventArgs e)

foreach (ListViewItem item in lvPlugins.Items)
{
PluginDebug.AddInfo("Check plugin update status", item.SubItems[0].Text, item.SubItems[1].Text);
PluginDebug.AddInfo("Check plugin update status", 0, item.SubItems[0].Text, item.SubItems[1].Text);
if (!item.SubItems[1].Text.Contains(KeePass.Resources.KPRes.NewVersionAvailable)) continue;
foreach (UpdateInfo upd in Plugins)
{
Expand Down Expand Up @@ -928,6 +933,7 @@ private void Restart()
PluginDebug.AddInfo("Restart started", DebugPrint);
if (m_kpf != null)
{
PluginDebug.AddInfo("Closing KeyPromptForm", 0, DebugPrint);
m_kpf.DialogResult = DialogResult.Cancel;
m_kpf.Close();
m_kpf.Dispose();
Expand All @@ -936,6 +942,7 @@ private void Restart()
}
if (m_slUpdateCheck != null)
{
PluginDebug.AddInfo("Closing update check progress form", 0, DebugPrint);
m_slUpdateCheck.EndLogging();
}
FieldInfo fi = m_host.MainWindow.GetType().GetField("m_bRestart", BindingFlags.NonPublic | BindingFlags.Instance);
Expand All @@ -944,6 +951,7 @@ private void Restart()
PluginDebug.AddInfo("Restart started, m_bRestart found", DebugPrint);
HandleMutex(true);
fi.SetValue(m_host.MainWindow, true);
m_bRestartTriggered = true;
m_host.MainWindow.ProcessAppMessage((IntPtr)KeePass.Program.AppMessage.Exit, IntPtr.Zero);
HandleMutex(false);
}
Expand All @@ -962,6 +970,19 @@ private void HandleMutex(bool release)
lStrings.Add("Mutex: " + KeePass.App.AppDefs.MutexName);
lStrings.Add("Release: " + release.ToString());
lStrings.Add("Single Instance: " + KeePass.Program.Config.Integration.LimitToSingleInstance.ToString());

//Get number of loaded databases
//KeePass creates an initial PwDocument during startup which must NOT be considered here
//
//Initial = No LockedIoc and PwDatabase is not opened (!IsOpen)
int iOpenedDB = 0;
foreach (PwDocument pd in KeePass.Program.MainForm.DocumentManager.Documents)
{
if (!string.IsNullOrEmpty(pd.LockedIoc.Path)) iOpenedDB++;
else if ((pd.Database != null) && pd.Database.IsOpen) iOpenedDB++;
}
lStrings.Add("Opened databases: " + iOpenedDB.ToString());

if (!KeePass.Program.Config.Integration.LimitToSingleInstance)
{
lStrings.Add("Action required: No");
Expand All @@ -976,7 +997,7 @@ private void HandleMutex(bool release)
PluginDebug.AddInfo("Handle global mutex", 0, lStrings.ToArray());
return;
}
else if (m_host.MainWindow.DocumentManager.Documents.Count > 0)
else if (iOpenedDB > 0)
{
//Only recreate mutex if at least document is loaded (lock flag is not relevant here)
//If no db is open, Restart is in progress already
Expand All @@ -985,9 +1006,9 @@ private void HandleMutex(bool release)
PluginDebug.AddInfo("Handle global mutex", 0, lStrings.ToArray());
Thread t = new Thread(new ThreadStart(() =>
{
Thread.Sleep(1000);
Thread.Sleep(PluginConfig.RestoreMutexThreshold);
bool bSuccess = GlobalMutexPool.CreateMutex(KeePass.App.AppDefs.MutexName, true);
PluginDebug.AddInfo("Handle global mutex", 0, "Recreate mutex sucfessful: " + bSuccess.ToString());
PluginDebug.AddInfo("Handle global mutex", 0, "Recreate mutex sucessful: " + bSuccess.ToString());
}));
t.Start();
return;
Expand Down Expand Up @@ -1060,10 +1081,10 @@ private string DebugPrint
{
get
{
string result = "DifferentThread: {0}, Check status: {1}";
string result = "DifferentThread: {0}, Check status: {1}, UI interaction blocked: {2}";
lock(m_lock)
{
result = string.Format(result, m_bRestartInvoke.ToString(), m_UpdateCheckStatus.ToString());
result = string.Format(result, m_bRestartInvoke.ToString(), m_UpdateCheckStatus.ToString(), KeePass.Program.MainForm.UIIsInteractionBlocked().ToString());
}
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("2.1.2")]
[assembly: AssemblyFileVersion("2.1.2")]
[assembly: AssemblyVersion("2.1.3")]
[assembly: AssemblyFileVersion("2.1.3")]
[assembly: Guid("672570AF-CC57-4980-86F9-D48FD1CC707D")]
27 changes: 19 additions & 8 deletions src/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,36 @@ public static string GetName(UpdateInfo ui)

public static class PluginConfig
{
private static KeePass.App.Configuration.AceCustomConfig Config = KeePass.Program.Config.CustomConfig;
public static bool Active = true;
public static bool CheckSync = true;
public static bool OneClickUpdate = true;
public static bool DownloadActiveLanguage = true;

public static int RestoreMutexThreshold
{
get
{
int t = (int)Config.GetLong("EarlyUpdateCheck.RestoreMutexThreshold", 2000);
Config.SetLong("EarlyUpdateCheck.RestoreMutexThreshold", t);
return t;
}
}

public static void Read()
{
PluginConfig.Active = KeePass.Program.Config.CustomConfig.GetBool("EarlyUpdateCheck.Active", PluginConfig.Active);
PluginConfig.CheckSync = KeePass.Program.Config.CustomConfig.GetBool("EarlyUpdateCheck.CheckSync", PluginConfig.CheckSync);
PluginConfig.OneClickUpdate = KeePass.Program.Config.CustomConfig.GetBool("EarlyUpdateCheck.OneClickUpdate", PluginConfig.OneClickUpdate);
PluginConfig.DownloadActiveLanguage = KeePass.Program.Config.CustomConfig.GetBool("EarlyUpdateCheck.DownloadActiveLanguage", PluginConfig.DownloadActiveLanguage);
PluginConfig.Active = Config.GetBool("EarlyUpdateCheck.Active", PluginConfig.Active);
PluginConfig.CheckSync = Config.GetBool("EarlyUpdateCheck.CheckSync", PluginConfig.CheckSync);
PluginConfig.OneClickUpdate = Config.GetBool("EarlyUpdateCheck.OneClickUpdate", PluginConfig.OneClickUpdate);
PluginConfig.DownloadActiveLanguage = Config.GetBool("EarlyUpdateCheck.DownloadActiveLanguage", PluginConfig.DownloadActiveLanguage);
}

public static void Write()
{
KeePass.Program.Config.CustomConfig.SetBool("EarlyUpdateCheck.Active", PluginConfig.Active);
KeePass.Program.Config.CustomConfig.SetBool("EarlyUpdateCheck.CheckSync", PluginConfig.CheckSync);
KeePass.Program.Config.CustomConfig.SetBool("EarlyUpdateCheck.OneClickUpdate", PluginConfig.OneClickUpdate);
KeePass.Program.Config.CustomConfig.SetBool("EarlyUpdateCheck.DownloadActiveLanguage", PluginConfig.DownloadActiveLanguage);
Config.SetBool("EarlyUpdateCheck.Active", PluginConfig.Active);
Config.SetBool("EarlyUpdateCheck.CheckSync", PluginConfig.CheckSync);
Config.SetBool("EarlyUpdateCheck.OneClickUpdate", PluginConfig.OneClickUpdate);
Config.SetBool("EarlyUpdateCheck.DownloadActiveLanguage", PluginConfig.DownloadActiveLanguage);
}
}

Expand Down
2 changes: 1 addition & 1 deletion version.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
:
Early update check:2.1.2
Early update check:2.1.3
Early update check!de:3
Early update check!ru:2
:

0 comments on commit 5b7aac2

Please sign in to comment.