From 0da6a5fc607ec3c19ba436a0f1d1cf8bcb290a72 Mon Sep 17 00:00:00 2001 From: BONNEVILLE Geoffroy Date: Fri, 7 Sep 2018 18:16:40 +0200 Subject: [PATCH] WIP Import mechanism --- ModernKeePass/Interfaces/IFormat.cs | 9 ++++ ModernKeePass/Interfaces/IImportService.cs | 9 ++++ ModernKeePass/ModernKeePass.App.csproj | 3 ++ ModernKeePass/Services/DatabaseService.cs | 42 ++---------------- ModernKeePass/Services/ImportService.cs | 24 ++++++++++ ModernKeePass/Strings/en-US/Resources.resw | 3 ++ ModernKeePass/Strings/fr-FR/Resources.resw | 3 ++ ModernKeePass/ViewModels/NewVm.cs | 44 ++++++++++++++++++- .../MainPageFrames/ImportExportPage.xaml | 4 +- .../Views/MainPageFrames/NewDatabasePage.xaml | 22 ++++++---- .../MainPageFrames/NewDatabasePage.xaml.cs | 22 ++++++++++ .../en-us/baselisting/releaseNotes.txt | 3 +- .../fr-fr/baselisting/releaseNotes.txt | 3 +- .../Mock/DatabaseServiceMock.cs | 2 +- 14 files changed, 141 insertions(+), 52 deletions(-) create mode 100644 ModernKeePass/Interfaces/IFormat.cs create mode 100644 ModernKeePass/Interfaces/IImportService.cs create mode 100644 ModernKeePass/Services/ImportService.cs diff --git a/ModernKeePass/Interfaces/IFormat.cs b/ModernKeePass/Interfaces/IFormat.cs new file mode 100644 index 0000000..b96cd17 --- /dev/null +++ b/ModernKeePass/Interfaces/IFormat.cs @@ -0,0 +1,9 @@ +using Windows.Storage; + +namespace ModernKeePass.Interfaces +{ + public interface IFormat + { + IPwEntity Import(IStorageFile source); + } +} \ No newline at end of file diff --git a/ModernKeePass/Interfaces/IImportService.cs b/ModernKeePass/Interfaces/IImportService.cs new file mode 100644 index 0000000..226db19 --- /dev/null +++ b/ModernKeePass/Interfaces/IImportService.cs @@ -0,0 +1,9 @@ +using Windows.Storage; + +namespace ModernKeePass.Interfaces +{ + public interface IImportService where T: IFormat + { + void Import(T format, IStorageFile source, IDatabaseService database); + } +} \ No newline at end of file diff --git a/ModernKeePass/ModernKeePass.App.csproj b/ModernKeePass/ModernKeePass.App.csproj index 308ca1a..e69740b 100644 --- a/ModernKeePass/ModernKeePass.App.csproj +++ b/ModernKeePass/ModernKeePass.App.csproj @@ -117,10 +117,13 @@ + + + diff --git a/ModernKeePass/Services/DatabaseService.cs b/ModernKeePass/Services/DatabaseService.cs index 9df5ce2..edc4cc9 100644 --- a/ModernKeePass/Services/DatabaseService.cs +++ b/ModernKeePass/Services/DatabaseService.cs @@ -5,11 +5,9 @@ using ModernKeePass.Exceptions; using ModernKeePass.Interfaces; using ModernKeePass.ViewModels; using ModernKeePassLib; -using ModernKeePassLib.Collections; using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Interfaces; using ModernKeePassLib.Keys; -using ModernKeePassLib.Security; using ModernKeePassLib.Serialization; namespace ModernKeePass.Services @@ -73,7 +71,7 @@ namespace ModernKeePass.Services { _settings = settings; } - + /// /// Open a KeePass database @@ -100,9 +98,7 @@ namespace ModernKeePass.Services if (createNew) { _pwDatabase.New(ioConnection, key); - - //Get settings default values - if (_settings.GetSetting("Sample")) CreateSampleData(); + var fileFormat = _settings.GetSetting("DefaultFileFormat"); switch (fileFormat) { @@ -122,7 +118,7 @@ namespace ModernKeePass.Services throw new ArgumentException(ex.Message, ex); } } - + public void ReOpen() { Open(_databaseFile, _compositeKey); @@ -190,37 +186,5 @@ namespace ModernKeePass.Services _compositeKey = newCompositeKey; _pwDatabase.MasterKey = newCompositeKey; } - - private void CreateSampleData() - { - _pwDatabase.RootGroup.AddGroup(new PwGroup(true, true, "Banking", PwIcon.Count), true); - _pwDatabase.RootGroup.AddGroup(new PwGroup(true, true, "Email", PwIcon.EMail), true); - _pwDatabase.RootGroup.AddGroup(new PwGroup(true, true, "Internet", PwIcon.World), true); - - var pe = new PwEntry(true, true); - pe.Strings.Set(PwDefs.TitleField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectTitle, - "Sample Entry")); - pe.Strings.Set(PwDefs.UserNameField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectUserName, - "Username")); - pe.Strings.Set(PwDefs.UrlField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectUrl, - PwDefs.HomepageUrl)); - pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectPassword, - "Password")); - pe.Strings.Set(PwDefs.NotesField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectNotes, - "You may safely delete this sample")); - _pwDatabase.RootGroup.AddEntry(pe, true); - - pe = new PwEntry(true, true); - pe.Strings.Set(PwDefs.TitleField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectTitle, - "Sample Entry #2")); - pe.Strings.Set(PwDefs.UserNameField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectUserName, - "Michael321")); - pe.Strings.Set(PwDefs.UrlField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectUrl, - PwDefs.HelpUrl + "kb/testform.html")); - pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(_pwDatabase.MemoryProtection.ProtectPassword, - "12345")); - pe.AutoType.Add(new AutoTypeAssociation("*Test Form - KeePass*", string.Empty)); - _pwDatabase.RootGroup.AddEntry(pe, true); - } } } diff --git a/ModernKeePass/Services/ImportService.cs b/ModernKeePass/Services/ImportService.cs new file mode 100644 index 0000000..d29fee9 --- /dev/null +++ b/ModernKeePass/Services/ImportService.cs @@ -0,0 +1,24 @@ +using ModernKeePass.Interfaces; +using Windows.Storage; +using ModernKeePass.ViewModels; + +namespace ModernKeePass.Services +{ + public class ImportService : IImportService + { + public void Import(IFormat format, IStorageFile source, IDatabaseService databaseService) + { + var entities = (GroupVm)format.Import(source); + + foreach (var entry in entities.Entries) + { + databaseService.RootGroup.Entries.Add(entry); + } + + foreach (var subGroup in entities.Groups) + { + databaseService.RootGroup.Groups.Add(subGroup); + } + } + } +} \ No newline at end of file diff --git a/ModernKeePass/Strings/en-US/Resources.resw b/ModernKeePass/Strings/en-US/Resources.resw index 3fb91f0..845621b 100644 --- a/ModernKeePass/Strings/en-US/Resources.resw +++ b/ModernKeePass/Strings/en-US/Resources.resw @@ -483,4 +483,7 @@ Group restored to its original position + + Import existing data + \ No newline at end of file diff --git a/ModernKeePass/Strings/fr-FR/Resources.resw b/ModernKeePass/Strings/fr-FR/Resources.resw index 90e7605..b1870aa 100644 --- a/ModernKeePass/Strings/fr-FR/Resources.resw +++ b/ModernKeePass/Strings/fr-FR/Resources.resw @@ -483,4 +483,7 @@ Groupe replacée à son emplacement d'origine + + Importer des données existantes + \ No newline at end of file diff --git a/ModernKeePass/ViewModels/NewVm.cs b/ModernKeePass/ViewModels/NewVm.cs index 66c5866..bfe2334 100644 --- a/ModernKeePass/ViewModels/NewVm.cs +++ b/ModernKeePass/ViewModels/NewVm.cs @@ -1,7 +1,49 @@ -namespace ModernKeePass.ViewModels +using Windows.Storage; +using Windows.UI.Xaml.Controls; +using ModernKeePass.Interfaces; +using ModernKeePassLib; + +namespace ModernKeePass.ViewModels { public class NewVm : OpenVm { public string Password { get; set; } + + public bool IsImportChecked { get; set; } + + public IStorageFile ImportFile { get; set; } + + public IFormat ImportFormat { get; set; } + + public void PopulateInitialData(IDatabaseService database, ISettingsService settings, IImportService importService) + { + if (settings.GetSetting("Sample") && !IsImportChecked) CreateSampleData(database); + else if (IsImportChecked && ImportFile != null) importService.Import(ImportFormat, ImportFile, database); + } + + private void CreateSampleData(IDatabaseService database) + { + var bankingGroup = database.RootGroup.AddNewGroup("Banking"); + bankingGroup.IconId = (int) Symbol.Calculator; + + var emailGroup = database.RootGroup.AddNewGroup("Email"); + emailGroup.IconId = (int) Symbol.Mail; + + var internetGroup = database.RootGroup.AddNewGroup("Internet"); + internetGroup.IconId = (int) Symbol.World; + + var sample1 = database.RootGroup.AddNewEntry(); + sample1.Name = "Sample Entry"; + sample1.UserName = "Username"; + sample1.Url = PwDefs.HomepageUrl; + sample1.Password = "Password"; + sample1.Notes = "You may safely delete this sample"; + + var sample2 = database.RootGroup.AddNewEntry(); + sample2.Name = "Sample Entry #2"; + sample2.UserName = "Michael321"; + sample2.Url = PwDefs.HelpUrl + "kb/testform.html"; + sample2.Password = "12345"; + } } } diff --git a/ModernKeePass/Views/MainPageFrames/ImportExportPage.xaml b/ModernKeePass/Views/MainPageFrames/ImportExportPage.xaml index 61c8908..91f5c35 100644 --- a/ModernKeePass/Views/MainPageFrames/ImportExportPage.xaml +++ b/ModernKeePass/Views/MainPageFrames/ImportExportPage.xaml @@ -23,7 +23,9 @@ - + + CSV + diff --git a/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml b/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml index 0126cb1..623949e 100644 --- a/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml +++ b/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml @@ -16,20 +16,26 @@ - + - - - - - - - + + + + + + + + + + + CSV + + diff --git a/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs b/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs index 8a0ceed..29d6461 100644 --- a/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs +++ b/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using Windows.Storage.Pickers; using Windows.UI.Xaml; +using ModernKeePass.Events; +using ModernKeePass.Services; using ModernKeePass.ViewModels; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 @@ -33,5 +35,25 @@ namespace ModernKeePass.Views if (file == null) return; Model.OpenFile(file); } + + private async void ImportFileButton_OnClick(object sender, RoutedEventArgs e) + { + var picker = + new FileOpenPicker + { + ViewMode = PickerViewMode.List, + SuggestedStartLocation = PickerLocationId.DocumentsLibrary + }; + picker.FileTypeFilter.Add(".csv"); + + // Application now has read/write access to the picked file + Model.ImportFile = await picker.PickSingleFileAsync(); + } + + private void CompositeKeyUserControl_OnValidationChecked(object sender, PasswordEventArgs e) + { + Model.PopulateInitialData(DatabaseService.Instance, new SettingsService()); + Frame.Navigate(typeof(GroupDetailPage)); + } } } diff --git a/ModernKeePass/appMetadata/en-us/baselisting/releaseNotes.txt b/ModernKeePass/appMetadata/en-us/baselisting/releaseNotes.txt index efc3559..ebf3006 100644 --- a/ModernKeePass/appMetadata/en-us/baselisting/releaseNotes.txt +++ b/ModernKeePass/appMetadata/en-us/baselisting/releaseNotes.txt @@ -1,4 +1,5 @@ Improved search box Changing entry icon creates a new history entry Corrected startup crash on some versions of Windows -Entry delete button now shows up correctly \ No newline at end of file +Entry delete button now shows up correctly +List of icons now only displays valid values \ No newline at end of file diff --git a/ModernKeePass/appMetadata/fr-fr/baselisting/releaseNotes.txt b/ModernKeePass/appMetadata/fr-fr/baselisting/releaseNotes.txt index 8acff76..507719d 100644 --- a/ModernKeePass/appMetadata/fr-fr/baselisting/releaseNotes.txt +++ b/ModernKeePass/appMetadata/fr-fr/baselisting/releaseNotes.txt @@ -1,4 +1,5 @@ Amelioration de la recherche Changer l'icone d'une entree cree un historique Correction de crash lors du lancement avec certaines versions de Windows -Le bouton de suppression d'une entree apparait bien desormais \ No newline at end of file +Le bouton de suppression d'une entree apparait bien desormais +La liste des icones n'affiche desormais que des valeurs valables \ No newline at end of file diff --git a/ModernKeePassApp.Test/Mock/DatabaseServiceMock.cs b/ModernKeePassApp.Test/Mock/DatabaseServiceMock.cs index 856adab..a9a67be 100644 --- a/ModernKeePassApp.Test/Mock/DatabaseServiceMock.cs +++ b/ModernKeePassApp.Test/Mock/DatabaseServiceMock.cs @@ -46,7 +46,7 @@ namespace ModernKeePassApp.Test.Mock { _compositeKey = newCompositeKey; } - + public void CreateRecycleBin(string title) { throw new NotImplementedException();