private void PairingManager_PairingComplete(object sender, ServerManagement.ServerConnectionInfoEventArgs e)
 {
     ThreadUtility.RunOnUIThread(() =>
     {
         ServerManager.ChooseServer(e.ConnectionInfo);
         Hide();
         NavigationManager.GoToFirstPage();
     });
 }
        private void LibraryList_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ServerConnectionInfo info = LibraryList.SelectedItem as ServerConnectionInfo;

            if (info == null)
            {
                return;
            }

            ServerManager.ChooseServer(info);
            NavigationManager.GoToFirstPage();
        }
        protected async void ConnectToServer()
        {
            if (_server != null)
            {
                return;
            }

            if (!HasValidData())
            {
                return;
            }

            string host;
            int    port;

            ParseHostname(out host, out port);

            string pairingCode = string.Format("{0:0000}{0:0000}{0:0000}{0:0000}", pinTextBox.IntValue.Value);

            try
            {
                _server = new DACPServer(host, port, pairingCode);
            }
            catch
            {
                MessageBox.Show(LocalizedStrings.LibraryConnectionErrorBody, LocalizedStrings.LibraryConnectionErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                return;
            }

            UpdateWizardItem(true);

            var result = await _server.ConnectAsync();

            switch (result)
            {
            case ConnectionResult.Success:
                // Save the server connection info
                ServerConnectionInfo info = new ServerConnectionInfo();
                info.Name          = _server.LibraryName;
                info.PairingCode   = _server.PairingCode;
                info.LastHostname  = _server.Hostname;
                info.LastIPAddress = _server.Hostname;
                info.LastPort      = _server.Port;

                // Get the service ID for Bonjour
                // In iTunes 10.1 and later, the service name comes from ServiceID (parameter aeIM).
                // In foo_touchremote the service name is the same as the database ID (parameter mper).
                // In MonkeyTunes, the service ID is not available from the database query. TODO.
                if (_server.MainDatabase.ServiceID > 0)
                {
                    info.ServiceID = _server.MainDatabase.ServiceID.ToString("x16").ToUpper();
                }
                else
                {
                    info.ServiceID = _server.MainDatabase.PersistentID.ToString("x16").ToUpper();
                }

                ServerManager.AddServerInfo(info);
                ServerManager.ChooseServer(info);

                Hide();

                NavigationManager.GoToFirstPage();
                break;

            case ConnectionResult.InvalidPIN:
                MessageBox.Show(LocalizedStrings.LibraryPINErrorBody, LocalizedStrings.LibraryPINErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                break;

            case ConnectionResult.ConnectionError:
                MessageBox.Show(LocalizedStrings.LibraryConnectionErrorBody, LocalizedStrings.LibraryConnectionErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                break;
            }
        }
        private async void HandleServerConnection(Task <ConnectionResult> task)
        {
            var result = await task;

            switch (result)
            {
            case ConnectionResult.Success:
                _log.Info("Successfully connected to server at {0}:{1}", _server.Hostname, _server.Port);

                // Notify the pairing utility so it can close
                if (_currentUtility != null)
                {
                    _currentUtility.SendPairedNotification(_server.PairingCode);
                }

                // Save the server connection info
                ServerConnectionInfo info = new ServerConnectionInfo();
                info.Name          = _server.LibraryName;
                info.ServiceID     = _libraryService.Name;
                info.PairingCode   = _server.PairingCode;
                info.LastHostname  = _libraryService.Hostname;
                info.LastIPAddress = _server.Hostname;
                info.LastPort      = _server.Port;

                _server = null;

                ServerManager.AddServerInfo(info);
                ServerManager.ChooseServer(info);

                Hide();

                NavigationManager.GoToFirstPage();
                break;

            case ConnectionResult.InvalidPIN:
                MessageBox.Show(LocalizedStrings.LibraryPINErrorBody, LocalizedStrings.LibraryPINErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                break;

            case ConnectionResult.ConnectionError:
                // Check whether there are any other IP addresses we could try
                var ipStrings = _libraryService.IPAddresses.Select(ip => ip.ToString()).ToList();
                var ipIndex   = ipStrings.IndexOf(_server.Hostname);
                if (ipIndex >= 0 && ipIndex < (ipStrings.Count - 1))
                {
                    ipIndex++;
                    string nextIP = ipStrings[ipIndex];
                    _server.Hostname = nextIP;
                    _server.Port     = _libraryService.Port;
                    _log.Info("Retrying connection on new IP: {0}:{1}", _server.Hostname, _server.Port);
                    HandleServerConnection(_server.ConnectAsync());
                    return;
                }

                // No other IPs, so we can't do anything else
                // TODO: Display error
                _server = null;
                UpdateWizardItem(true);
                break;
            }
        }