Add option to always merge on conflict, closes https://github.com/PhilippC/keepass2android/issues/1218

This commit is contained in:
Philipp Crocoll
2022-01-15 15:55:34 +01:00
parent daecd42d8e
commit b4a82511ff
4 changed files with 40 additions and 13 deletions

View File

@@ -21,6 +21,7 @@ using System.Security.Cryptography;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using Java.Lang;
using KeePassLib;
using KeePassLib.Serialization;
@@ -37,7 +38,7 @@ namespace keepass2android
private readonly Database _db;
private readonly bool _dontSave;
/// <summary>
/// <summary>
/// stream for reading the data from the original file. If this is set to a non-null value, we know we need to sync
/// </summary>
private readonly Stream _streamForOrigFile;
@@ -51,7 +52,7 @@ namespace keepass2android
_ctx = ctx;
_app = app;
_dontSave = dontSave;
}
}
/// <summary>
/// Constructor for sync
@@ -129,6 +130,17 @@ namespace keepass2android
{
Kp2aLog.Log("Conflict. " + hasStreamForOrigFile + " " + hasChangeFast + " " + hasHashChanged);
bool alwaysMerge = (PreferenceManager.GetDefaultSharedPreferences(Application.Context)
.GetBoolean("AlwaysMergeOnConflict", false));
if (alwaysMerge)
{
MergeAndFinish(fileStorage, ioc);
}
else
{
//ask user...
_app.AskYesNoCancel(UiStringKey.TitleSyncQuestion, UiStringKey.MessageSyncQuestion,
UiStringKey.YesSynchronize,
@@ -136,16 +148,7 @@ namespace keepass2android
//yes = sync
(sender, args) =>
{
Action runHandler = () =>
{
//note: when synced, the file might be downloaded once again from the server. Caching the data
//in the hashing function would solve this but increases complexity. I currently assume the files are
//small.
MergeIn(fileStorage, ioc);
PerformSaveWithoutCheck(fileStorage, ioc);
_db.UpdateGlobals();
Finish(true);
};
Action runHandler = () => { MergeAndFinish(fileStorage, ioc); };
RunInWorkerThread(runHandler);
},
//no = overwrite
@@ -164,6 +167,8 @@ namespace keepass2android
},
_ctx
);
}
}
else
{
@@ -193,7 +198,18 @@ namespace keepass2android
}
private void RunInWorkerThread(Action runHandler)
private void MergeAndFinish(IFileStorage fileStorage, IOConnectionInfo ioc)
{
//note: when synced, the file might be downloaded once again from the server. Caching the data
//in the hashing function would solve this but increases complexity. I currently assume the files are
//small.
MergeIn(fileStorage, ioc);
PerformSaveWithoutCheck(fileStorage, ioc);
_db.UpdateGlobals();
Finish(true);
}
private void RunInWorkerThread(Action runHandler)
{
try
{