Skip to content

Commit

Permalink
Version 2.0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KoenZomers committed Jun 2, 2019
1 parent ffe9cc6 commit 78cc0a0
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 57 deletions.
Binary file modified KeeOneDriveSync.plgx
Binary file not shown.
5 changes: 5 additions & 0 deletions KoenZomers.KeePass.OneDriveSync/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class Configuration : ICloneable
/// </summary>
public KeePassLib.PwDatabase KeePassDatabase { get; set; }

/// <summary>
/// Boolean indicating if this database is currently synchronizing
/// </summary>
public bool IsCurrentlySyncing { get; set; }

#endregion

#region Serializable Properties
Expand Down
7 changes: 6 additions & 1 deletion KoenZomers.KeePass.OneDriveSync/Enums/CloudStorageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public enum CloudStorageType : short
/// <summary>
/// Microsoft SharePoint
/// </summary>
SharePoint
SharePoint = 3,

/// <summary>
/// Do not sync
/// </summary>
None = 99
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private void OKButton_Click(object sender, EventArgs e)
{
_configuration.DoNotSync = NoNeverAskAgainRadio.Checked;
Configuration.Save();
DialogResult = DialogResult.Cancel;
DialogResult = DialogResult.OK;
}

Close();
Expand Down
63 changes: 39 additions & 24 deletions KoenZomers.KeePass.OneDriveSync/KeePassDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,42 +85,51 @@ public static async Task SyncDatabase(string localKeePassDatabasePath, Action<st
{
try
{
// If something is already syncing, abort this attempt. This happens when the Import saves the resulting KeePass database. That by itself triggers another sync which doesn't have to go through the sync process as it just regards the temp database.
if (KoenZomersKeePassOneDriveSyncExt.IsSomethingStillRunning)
{
UpdateStatus("A synchronization is already in progress");
return;
}

// Set flag to block exiting KeePass until this is done
KoenZomersKeePassOneDriveSyncExt.IsSomethingStillRunning = true;

UpdateStatus("Starting database synchronization");

// Retrieve the KeePassOneDriveSync settings
if (databaseConfig == null)
{
databaseConfig = Configuration.GetPasswordDatabaseConfiguration(localKeePassDatabasePath);
}

// If this database is already syncing, abort this attempt. This happens when the Import saves the resulting KeePass database. That by itself triggers another sync which doesn't have to go through the sync process as it just regards the temp database.
if (databaseConfig.IsCurrentlySyncing)
{
UpdateStatus(string.Format("A synchronization is already in progress for database {0}", databaseConfig.KeePassDatabase.Name));
return;
}

// Check if this database explicitly does not allow to be synced with OneDrive and if syncing is enabled on this database
if (databaseConfig.DoNotSync || !databaseConfig.SyncingEnabled)
{
// Set flag to allow exiting KeePass
KoenZomersKeePassOneDriveSyncExt.IsSomethingStillRunning = false;

return;
}

UpdateStatus(string.Format("Starting database synchronization for database {0}", databaseConfig.KeePassDatabase.Name));
databaseConfig.IsCurrentlySyncing = true;

// Check if the current database is synced with OneDrive
if (string.IsNullOrEmpty(databaseConfig.RemoteDatabasePath) && string.IsNullOrEmpty(databaseConfig.RemoteFolderId) && string.IsNullOrEmpty(databaseConfig.RemoteFileName))
{
var cloudStorageType = AskIfShouldBeSynced(databaseConfig);

if(!cloudStorageType.HasValue)
{
// Set flag to allow exiting KeePass
KoenZomersKeePassOneDriveSyncExt.IsSomethingStillRunning = false;
// No decission has been taken whether to sync the database or not
// Set flag to indicate that the database is currently not syncing
databaseConfig.IsCurrentlySyncing = false;

UpdateStatus(string.Format("Canceled database synchronization for database {0}", databaseConfig.KeePassDatabase.Name));

return;
}
if (cloudStorageType.Value == CloudStorageType.None)
{
// Database should not be synchronized
// Set flag to indicate that the database is currently not syncing
databaseConfig.IsCurrentlySyncing = false;

UpdateStatus(string.Format("Database {0} will not be synchronized", databaseConfig.KeePassDatabase.Name));

return;
}

Expand All @@ -147,14 +156,14 @@ public static async Task SyncDatabase(string localKeePassDatabasePath, Action<st
if(!syncSuccessful)
{
// Set flag to allow exiting KeePass
KoenZomersKeePassOneDriveSyncExt.IsSomethingStillRunning = false;
databaseConfig.IsCurrentlySyncing = false;
return;
}

// Update the KeePass UI so it shows the new entries
KoenZomersKeePassOneDriveSyncExt.Host.MainWindow.UpdateUI(false, null, true, null, true, null, false);

updateStatus("KeePass database has successfully been synchronized and uploaded");
updateStatus(string.Format("KeePass database {0} has successfully been synchronized and uploaded", databaseConfig.KeePassDatabase.Name));

databaseConfig.LocalFileHash = Utilities.GetDatabaseFileHash(localKeePassDatabasePath);
//databaseConfig.ETag = uploadResult.ETag;
Expand All @@ -164,11 +173,11 @@ public static async Task SyncDatabase(string localKeePassDatabasePath, Action<st
}
catch(Exception e)
{
updateStatus(string.Concat("Failed to sync database: ", e.Message));
updateStatus(string.Format("Failed to sync database {0}: {1}", databaseConfig.KeePassDatabase.Name, e.Message));
}

// Set flag to allow exiting KeePass
KoenZomersKeePassOneDriveSyncExt.IsSomethingStillRunning = false;
databaseConfig.IsCurrentlySyncing = false;
}

/// <summary>
Expand All @@ -186,8 +195,14 @@ public static async Task SyncDatabase(string localKeePassDatabasePath, Action<st
return null;
}

if(databaseConfig.DoNotSync)
{
// Don't sync and don't ask again has been chosen
return CloudStorageType.None;
}

// Ask which cloud service to connect to
var oneDriveCloudTypeForm = new OneDriveCloudTypeForm { ExplanationText = "Choose the cloud service you wish to store the KeePass database on:" };
var oneDriveCloudTypeForm = new OneDriveCloudTypeForm { ExplanationText = string.Format("Choose the cloud service you wish to store the KeePass database {0} on:", databaseConfig.KeePassDatabase.Name) };
if (oneDriveCloudTypeForm.ShowDialog() != DialogResult.OK)
{
// Dialog has been canceled. Return NULL to indicate no choice has been made.
Expand All @@ -212,7 +227,7 @@ public static bool MergeDatabases(Configuration databaseConfig, string tempDatab
databaseConfig.SyncingEnabled = false;

// Import the current database with the downloaded database. Import causes a one way sync, syncing would try to update both ends which won't work for OneDrive.
var importSuccessful = ImportUtil.Import(KoenZomersKeePassOneDriveSyncExt.Host.Database, formatter, new[] { connectionInfo }, true, KoenZomersKeePassOneDriveSyncExt.Host.MainWindow, false, KoenZomersKeePassOneDriveSyncExt.Host.MainWindow);
var importSuccessful = ImportUtil.Import(databaseConfig.KeePassDatabase, formatter, new[] { connectionInfo }, true, KoenZomersKeePassOneDriveSyncExt.Host.MainWindow, false, KoenZomersKeePassOneDriveSyncExt.Host.MainWindow);

// Enable syncing of this database again
databaseConfig.SyncingEnabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ public class KoenZomersKeePassOneDriveSyncExt : Plugin
/// <summary>
/// Boolean to indicate if some process is still running that we need to wait for before we shut down KeePass
/// </summary>
internal static bool IsSomethingStillRunning { get; set; }
internal static bool IsSomethingStillRunning
{
get
{
return Configuration.PasswordDatabases.Any(db => db.Value.IsCurrentlySyncing);
}
}

#endregion

Expand Down Expand Up @@ -209,7 +215,12 @@ private async void MainWindowOnFileSaved(object sender, FileSavedEventArgs fileS
config.KeePassDatabase = fileSavedEventArgs.Database;

// Check if we should sync this database
if (config.DoNotSync || !_fileOfflineMenuItem.Checked) return;
if (config.DoNotSync) return;
if(!_fileOfflineMenuItem.Checked)
{
Host.MainWindow.SetStatusEx(string.Format("OneDriveSync has been set to offline, skipping sync for database {0}", fileSavedEventArgs.Database.Name));
return;
}

// Make sure it's not a remote database on i.e. an FTP or website
if (!fileSavedEventArgs.Database.IOConnectionInfo.IsLocalFile())
Expand Down Expand Up @@ -274,7 +285,12 @@ private async void OnKeePassDatabaseOpened(object sender, FileOpenedEventArgs fi
config.KeePassDatabase = fileOpenedEventArgs.Database;

// Check if we should sync this database
if (config.DoNotSync || !fileOpenedEventArgs.Database.IOConnectionInfo.IsLocalFile() || !_fileOfflineMenuItem.Checked) return;
if (config.DoNotSync || !fileOpenedEventArgs.Database.IOConnectionInfo.IsLocalFile()) return;
if(!_fileOfflineMenuItem.Checked)
{
Host.MainWindow.SetStatusEx(string.Format("OneDriveSync has been set to offline, skipping sync for database {0}", fileOpenedEventArgs.Database.Name));
return;
}

// Check if the database configuration of the opened KeePass database is set to retrieve the OneDrive Refresh Token from the KeePass database itself
if (config.RefreshTokenStorage == OneDriveRefreshTokenStorage.KeePassDatabase && string.IsNullOrEmpty(config.RefreshToken))
Expand Down
2 changes: 1 addition & 1 deletion KoenZomers.KeePass.OneDriveSync/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
// 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("2.0.4.2")]
[assembly: AssemblyVersion("2.0.5.0")]
Loading

0 comments on commit 78cc0a0

Please sign in to comment.