/// <summary>
        /// Occurs when the import button is clicked.
        /// </summary>
        /// <param name="sender">The object where the event handler is attached.</param>
        /// <param name="e">The event data.</param>
        private async void importButton_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            openPicker.ViewMode         = PickerViewMode.List;
            openPicker.CommitButtonText = "Import";

            // Filter to include a sample subset of file types.
            openPicker.FileTypeFilter.Clear();
            //openPicker.FileTypeFilter.Add(".pfx");
            openPicker.FileTypeFilter.Add(".pem");
            openPicker.FileTypeFilter.Add(".key");
            openPicker.FileTypeFilter.Add(".ssh");

            // Open the file picker.
            var files = await openPicker.PickMultipleFilesAsync();

            // files is null if user cancels the file picker.
            if (files == null || files.Count == 0)
            {
                return;
            }

            var privateKeysDataSource = App.Current.Resources["privateKeysDataSource"] as PrivateKeysDataSource;

            if (privateKeysDataSource == null)
            {
                return;
            }

            foreach (var file in files)
            {
                var buffer = await FileIO.ReadBufferAsync(file);

                using (var stream = new MemoryStream(buffer.ToArray()))
                {
                    PrivateKeyFile.Validate(stream);
                }

                var privateKeysFolder = await PrivateKeysDataSource.GetPrivateKeysFolder();

                var privateKeyFile = await file.CopyAsync(privateKeysFolder, file.Name, NameCollisionOption.GenerateUniqueName);

                var privateKeyData = new PrivateKeyData()
                {
                    FileName = privateKeyFile.Name,
                    Data     = (await FileIO.ReadBufferAsync(privateKeyFile)).ToArray(),
                };

                privateKeysDataSource.PrivateKeys.Remove(PrivateKeysDataSource.GetPrivateKey(privateKeyData.FileName));
                privateKeysDataSource.PrivateKeys.Add(privateKeyData);
            }

            var items = new ObservableCollection <PrivateKeyData>(privateKeysDataSource.PrivateKeys.OrderBy(f => f.FileName));

            this.DefaultViewModel["Items"] = items;
            this.emptyHint.Visibility      = this.itemGridView.Items.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
        }
        /// <summary>
        /// Populates the page with content passed during navigation. Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> 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.</param>
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            PrivateKeysDataSource privateKeysDataSource = (PrivateKeysDataSource)App.Current.Resources["privateKeysDataSource"];

            if (privateKeysDataSource != null)
            {
                var items = new ObservableCollection <PrivateKeyData>(privateKeysDataSource.PrivateKeys.OrderBy(f => f.FileName));
                this.DefaultViewModel["Items"] = items;

                this.emptyHint.Visibility = this.itemGridView.Items.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
                this.SetupAppBar();
            }
        }
示例#3
0
        /// <summary>
        /// Populates the page with content passed during navigation. Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> 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.</param>
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"]
            PrivateKeysDataSource privateKeysDataSource = (PrivateKeysDataSource)App.Current.Resources["privateKeysDataSource"];

            if (privateKeysDataSource != null)
            {
                var keys = new ObservableCollection <PrivateKeyData>(privateKeysDataSource.PrivateKeys.OrderBy(f => f.FileName));
                this.DefaultViewModel["Keys"] = keys;
            }

            this.AgentKeys = new ObservableCollection <PrivateKeyAgentKey>(PrivateKeyAgentManager.PrivateKeyAgent.ListSsh2());
            this.DefaultViewModel["AgentKeys"] = this.AgentKeys;

            this.SetEmptyHintVisibilities();
        }
        /// <summary>
        /// Occurs when the delete button is clicked.
        /// </summary>
        /// <param name="sender">The object where the event handler is attached.</param>
        /// <param name="e">The event data.</param>
        private async void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            PrivateKeysDataSource privateKeysDataSource = (PrivateKeysDataSource)App.Current.Resources["privateKeysDataSource"];

            if (privateKeysDataSource != null)
            {
                var selectedItems = this.itemGridView.SelectedItems.ToArray();
                foreach (PrivateKeyData selectedItem in selectedItems)
                {
                    await privateKeysDataSource.Remove(selectedItem);

                    ((ObservableCollection <PrivateKeyData>) this.DefaultViewModel["Items"]).Remove(selectedItem);
                }

                this.emptyHint.Visibility = this.itemGridView.Items.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
            }
        }
示例#5
0
        /// <summary>
        /// Initializes the connection object with the specified connection data.
        /// </summary>
        /// <param name="connectionData">The connection data for the connection.</param>
        /// <exception cref="ObjectDisposedException">The connection object is already disposed.</exception>
        /// <exception cref="InvalidOperationException">The connection object is currently connected.</exception>
        /// <exception cref="ArgumentException">The <paramref name="connectionData"/> object contains a connection type that is not supported by the connection object.</exception>
        /// <exception cref="Exception">Some other error occured (here: the private key for the SSH authentication could not be found).</exception>
        public void Initialize(ConnectionData connectionData)
        {
            this.CheckDisposed();
            this.MustBeConnected(false);

            if (connectionData.Type != ConnectionType.Ssh)
            {
                throw new ArgumentException("ConnectionData does not use Ssh connection.", "connectionData");
            }

            this.connectionData = connectionData;

            // This is already done here instead of in the ConnectAsync method because here the PrivateKeysDataSource.GetPrivateKey method is able to access the Resources.
            if (connectionData.Authentication == AuthenticationType.PrivateKey)
            {
                this.privateKeyData = PrivateKeysDataSource.GetPrivateKey(connectionData.PrivateKeyName);
                if (this.privateKeyData == null)
                {
                    throw new Exception("Private Key '" + connectionData.PrivateKeyName + "' not found. Please correct the authentication details of the connection.");
                }
            }
        }
示例#6
0
        /// <summary>
        /// Invoked when the application is launched normally by the end user. Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected async override void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active

            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                //Associate the frame with a SuspensionManager key
                SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
                // Set the default language
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                rootFrame.NavigationFailed += OnNavigationFailed;

                await ApplicationData.Current.SetVersionAsync(0, SetVersionHandler);

                FavoritesDataSource favoritesDataSource = (FavoritesDataSource)App.Current.Resources["favoritesDataSource"];
                if (favoritesDataSource != null)
                {
                    if (favoritesDataSource.Favorites.Count == 0)
                    {
                        favoritesDataSource.GetFavorites();
                    }
                }

                PrivateKeysDataSource privateKeysDataSource = (PrivateKeysDataSource)App.Current.Resources["privateKeysDataSource"];
                if (privateKeysDataSource != null)
                {
                    if (privateKeysDataSource.PrivateKeys.Count == 0)
                    {
                        await privateKeysDataSource.GetPrivateKeys();
                    }
                }

                ColorThemesDataSource colorThemesDataSource = (ColorThemesDataSource)App.Current.Resources["colorThemesDataSource"];
                if (colorThemesDataSource != null)
                {
                    if (colorThemesDataSource.CustomTheme == null)
                    {
                        colorThemesDataSource.GetColorThemes();
                    }
                }

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // Restore the saved session state only when appropriate
                    try
                    {
                        await SuspensionManager.RestoreAsync();
                    }
                    catch (SuspensionManagerException)
                    {
                        //Something went wrong restoring state.
                        //Assume there is no state and continue
                    }
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }
            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(FavoritesPage), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }
        /// <summary>
        /// Populates the page with content passed during navigation. Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> 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.</param>
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            this.mode = ConnectionDataMode.Edit;

            string id = e.NavigationParameter as string;

            this.sshRadioButton.IsChecked = true;
            this.authenticationMethodComboBox.SelectedIndex = 0;
            PrivateKeysDataSource privateKeysDataSource = (PrivateKeysDataSource)App.Current.Resources["privateKeysDataSource"];

            if (privateKeysDataSource != null)
            {
                var privateKeys = from privateKey in privateKeysDataSource.PrivateKeys
                                  orderby privateKey.FileName
                                  select privateKey.FileName;
                this.privateKeyComboBox.ItemsSource = privateKeys;
            }

            if (id == null)
            {
                this.mode = ConnectionDataMode.QuickConnect;
                this.nameOptions.Visibility = Visibility.Collapsed;
            }
            else if (id.Length == 0)
            {
                this.mode = ConnectionDataMode.New;
            }
            else
            {
                ConnectionData connectionData = FavoritesDataSource.GetFavorite(id);
                if (connectionData == null)
                {
                    this.Frame.GoBack();
                    return;
                }

                this.nameTextBox.Text = connectionData.Name;
                switch (connectionData.Type)
                {
                case ConnectionType.Ssh:
                    this.sshRadioButton.IsChecked    = true;
                    this.telnetRadioButton.IsChecked = false;
                    break;

                case ConnectionType.Telnet:
                    this.sshRadioButton.IsChecked    = false;
                    this.telnetRadioButton.IsChecked = true;
                    break;
                }
                this.hostTextBox.Text     = connectionData.Host;
                this.portTextBox.Text     = connectionData.Port.ToString();
                this.usernameTextBox.Text = connectionData.Username;
                this.authenticationMethodComboBox.SelectedIndex  = (int)connectionData.Authentication;
                this.privateKeyComboBox.SelectedItem             = connectionData.PrivateKeyName;
                this.privateKeyAgentForwardingCheckBox.IsChecked = connectionData.PrivateKeyAgentForwarding;
                this.id = connectionData.Id;
            }

            this.SetupPageTitle();
            this.SetupAppBar();
        }