using ModernKeePass.Common;
using ModernKeePassLib;
using ModernKeePassLib.Interfaces;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using System.Linq;
using System;
// The Hub Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=321224
namespace ModernKeePass.Pages
{
///
/// A page that displays a grouped collection of items.
///
public sealed partial class DatabaseViewPage : Page
{
private NavigationHelper navigationHelper;
private ObservableDictionary defaultViewModel = new ObservableDictionary();
private PwDatabase _database = new PwDatabase();
///
/// This can be changed to a strongly typed view model.
///
public ObservableDictionary DefaultViewModel
{
get { return this.defaultViewModel; }
}
///
/// NavigationHelper is used on each page to aid in navigation and
/// process lifetime management
///
public NavigationHelper NavigationHelper
{
get { return this.navigationHelper; }
}
public DatabaseViewPage()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += navigationHelper_LoadState;
}
///
/// Populates the page with content passed during navigation. Any saved state is also
/// provided when recreating a page from a prior session.
///
///
/// The source of the event; typically
///
/// Event data that provides both the navigation parameter passed to
/// when this page was initially requested and
/// a dictionary of state preserved by this page during an earlier
/// session. The state will be null the first time a page is visited.
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// TODO: Assign a collection of bindable groups to this.DefaultViewModel["Groups"]
}
#region NavigationHelper registration
/// The methods provided in this section are simply used to allow
/// NavigationHelper to respond to the page's navigation methods.
///
/// Page specific logic should be placed in event handlers for the
///
/// and .
/// The navigation parameter is available in the LoadState method
/// in addition to page state preserved during an earlier session.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
navigationHelper.OnNavigatedTo(e);
if (e.Parameter is StorageFile)
{
var file = e.Parameter as StorageFile;
var key = new CompositeKey();
key.AddUserKey(new KcpPassword("test"));
try
{
_database.Open(IOConnectionInfo.FromPath(file.Path), key, new NullStatusLogger());
DefaultViewModel["Groups"] = _database.RootGroup.GetGroups(false).Select(g => g.Name);
}
finally
{
_database.Close();
}
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
navigationHelper.OnNavigatedFrom(e);
}
#endregion
}
}