@@ -531,6 +531,34 @@ private void EnsureRecycleBin(ref PwGroup pgRecycleBin)
531531 else { Debug . Assert ( pgRecycleBin . Uuid . Equals ( this . RecycleBinUuid ) ) ; }
532532 }
533533
534+ /// <summary>
535+ /// Remove RecycleBin before merge. RecycleBin should be kept locally and should not be merged.
536+ /// </summary>
537+ /// <param name="pwDb"></param>
538+ /// <returns></returns>
539+ private bool DeleteRecycleBin ( PwDatabase pwDb )
540+ {
541+ if ( pwDb == null ) { return false ; }
542+
543+ PwGroup pgRecycleBin = pwDb . RootGroup . FindGroup ( pwDb . RecycleBinUuid , true ) ;
544+
545+ if ( pgRecycleBin != null )
546+ {
547+ pwDb . RootGroup . Groups . Remove ( pgRecycleBin ) ;
548+ pgRecycleBin . DeleteAllObjects ( pwDb ) ;
549+ PwDeletedObject pdo = new PwDeletedObject ( pgRecycleBin . Uuid , DateTime . UtcNow ) ;
550+ pwDb . DeletedObjects . Add ( pdo ) ;
551+ Debug . WriteLine ( "DeleteRecycleBin successfully." ) ;
552+ return true ;
553+ }
554+ else
555+ {
556+ Debug . WriteLine ( "DeleteRecycleBin failure." ) ;
557+ return false ;
558+ }
559+ }
560+
561+
534562 /// <summary>
535563 /// Find an entry or a group.
536564 /// </summary>
@@ -966,6 +994,58 @@ orderby e.LastModificationTime descending
966994 return resultsList ;
967995 }
968996
997+ public bool Merge ( string path , PwMergeMethod mm )
998+ {
999+ var pwImp = new PwDatabase ( ) ;
1000+ var ioInfo = IOConnectionInfo . FromPath ( path ) ;
1001+
1002+ var compositeKey = MasterKey ;
1003+
1004+ KPCLibLogger swLogger = new KPCLibLogger ( ) ;
1005+ try
1006+ {
1007+ swLogger . StartLogging ( "Merge: Opening database ..." , true ) ;
1008+ pwImp . Open ( ioInfo , compositeKey , swLogger ) ;
1009+ swLogger . EndLogging ( ) ;
1010+ }
1011+ catch ( Exception e )
1012+ {
1013+ Debug . WriteLine ( $ "$Failed to open database: { e . Message } .") ;
1014+ return false ;
1015+ }
1016+
1017+ // We only merge, if these are the same database with different versions.
1018+ if ( RootGroup . EqualsGroup ( pwImp . RootGroup , ( PwCompareOptions . IgnoreLastBackup |
1019+ PwCompareOptions . IgnoreHistory | PwCompareOptions . IgnoreParentGroup |
1020+ PwCompareOptions . IgnoreTimes | PwCompareOptions . PropertiesOnly ) , MemProtCmpMode . None ) )
1021+ {
1022+ Debug . WriteLine ( $ "Merge: Root group are the same. Merge method is { mm } .") ;
1023+ }
1024+ else
1025+ {
1026+ Debug . WriteLine ( $ "Merge: Root group are different DBase={ RootGroup } , pwImp={ pwImp . RootGroup } .") ;
1027+ pwImp . Close ( ) ;
1028+ return false ;
1029+ }
1030+
1031+ try
1032+ {
1033+ // Need to remove RecycleBin first before merge.
1034+ DeleteRecycleBin ( pwImp ) ;
1035+
1036+ MergeIn ( pwImp , mm , swLogger ) ;
1037+ DescriptionChanged = DateTime . UtcNow ;
1038+ Save ( swLogger ) ;
1039+ pwImp . Close ( ) ;
1040+ }
1041+ catch ( Exception exMerge )
1042+ {
1043+ Debug . WriteLine ( $ "Merge failed { exMerge } ") ;
1044+ return false ;
1045+ }
1046+ return true ;
1047+ }
1048+
9691049 // The end of PxDatabase
9701050 }
9711051
0 commit comments