Added Save As function

This commit is contained in:
2017-10-12 11:45:00 +02:00
committed by BONNEVILLE Geoffroy
parent 676365460c
commit 10b6330ac6
6 changed files with 39 additions and 48 deletions

View File

@@ -63,7 +63,14 @@ namespace ModernKeePass.Common
} }
return string.Empty; return string.Empty;
} }
internal void Save(StorageFile file)
{
DatabaseFile = file;
_pwDatabase.SaveAs(IOConnectionInfo.FromFile(DatabaseFile), true, new NullStatusLogger());
Status = DatabaseStatus.Opened;
}
public void Save() public void Save()
{ {
if (_pwDatabase != null && _pwDatabase.IsOpen) if (_pwDatabase != null && _pwDatabase.IsOpen)

View File

@@ -156,7 +156,6 @@
<Compile Include="ViewModels\EntryVm.cs" /> <Compile Include="ViewModels\EntryVm.cs" />
<Compile Include="ViewModels\GroupVm.cs" /> <Compile Include="ViewModels\GroupVm.cs" />
<Compile Include="ViewModels\MainVm.cs" /> <Compile Include="ViewModels\MainVm.cs" />
<Compile Include="ViewModels\NewVm.cs" />
<Compile Include="ViewModels\OpenVm.cs" /> <Compile Include="ViewModels\OpenVm.cs" />
<Compile Include="ViewModels\RecentVm.cs" /> <Compile Include="ViewModels\RecentVm.cs" />
<Compile Include="ViewModels\SaveVm.cs" /> <Compile Include="ViewModels\SaveVm.cs" />

View File

@@ -6,13 +6,12 @@
xmlns:viewModels="using:ModernKeePass.ViewModels" xmlns:viewModels="using:ModernKeePass.ViewModels"
x:Class="ModernKeePass.Pages.SaveDatabasePage" x:Class="ModernKeePass.Pages.SaveDatabasePage"
mc:Ignorable="d"> mc:Ignorable="d">
<Page.DataContext> <Page.DataContext>
<viewModels:SaveVm/> <viewModels:SaveVm/>
</Page.DataContext> </Page.DataContext>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<HyperlinkButton x:Name="SaveButton" Content="Save and close" Click="SaveButton_OnClick" VerticalAlignment="Top" IsEnabled="{Binding IsSaveEnabled}" /> <HyperlinkButton x:Name="SaveButton" Content="Save and close" Click="SaveButton_OnClick" VerticalAlignment="Top" IsEnabled="{Binding IsSaveEnabled}" />
<HyperlinkButton x:Name="SaveAsButton" Content="Save as..." VerticalAlignment="Top" IsEnabled="False" /> <HyperlinkButton x:Name="SaveAsButton" Content="Save as..." Click="SaveAsButton_OnClick" VerticalAlignment="Top" IsEnabled="{Binding IsSaveEnabled}" />
</StackPanel> </StackPanel>
</Page> </Page>

View File

@@ -1,4 +1,7 @@
using Windows.UI.Xaml; using System;
using System.Collections.Generic;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; using Windows.UI.Xaml.Navigation;
using ModernKeePass.ViewModels; using ModernKeePass.ViewModels;
@@ -30,5 +33,22 @@ namespace ModernKeePass.Pages
viewModel.Save(); viewModel.Save();
_mainFrame.Navigate(typeof(MainPage)); _mainFrame.Navigate(typeof(MainPage));
} }
private async void SaveAsButton_OnClick(object sender, RoutedEventArgs e)
{
var savePicker = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
SuggestedFileName = "New Database"
};
savePicker.FileTypeChoices.Add("KeePass 2.x database", new List<string> { ".kdbx" });
var file = await savePicker.PickSaveFileAsync();
if (file == null) return;
var viewModel = DataContext as SaveVm;
viewModel.Save(file);
_mainFrame.Navigate(typeof(MainPage));
}
} }
} }

View File

@@ -1,43 +0,0 @@
using System.ComponentModel;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Common;
namespace ModernKeePass.ViewModels
{
public class NewVm : INotifyPropertyChanged
{
public bool ShowPasswordBox
{
get { return ((App)Application.Current).Database.Status == DatabaseHelper.DatabaseStatus.Opening; }
}
public string Name
{
get { return ((App)Application.Current).Database.Name; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void OpenFile(StorageFile file)
{
var database = ((App)Application.Current).Database;
database.DatabaseFile = file;
NotifyPropertyChanged("Name");
NotifyPropertyChanged("ShowPasswordBox");
AddToRecentList(file);
}
private void AddToRecentList(StorageFile file)
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
mru.Add(file, file.DisplayName);
}
}
}

View File

@@ -1,6 +1,8 @@
using System.ComponentModel; using System.ComponentModel;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using ModernKeePass.Common; using ModernKeePass.Common;
using System;
using Windows.Storage;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
@@ -29,5 +31,12 @@ namespace ModernKeePass.ViewModels
app.Database.Close(); app.Database.Close();
NotifyPropertyChanged("IsSaveEnabled"); NotifyPropertyChanged("IsSaveEnabled");
} }
internal void Save(StorageFile file)
{
var app = (App)Application.Current;
app.Database.Save(file);
NotifyPropertyChanged("IsSaveEnabled");
}
} }
} }