示例#1
0
        /// <summary>
        /// Handles the OnClick event of the ActivateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void ActivateButtonOnClick(object sender, RoutedEventArgs e)
        {
            activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = false;

            Log.Info("Activating the software...");
            SetStatus2("Activating software...", 1);

            var name = emailTextBox.Text.ToLower().Trim();
            var key  = keyTextBox.Text.Trim();

            Task.Factory.StartNew(() =>
            {
                object identity;
                Generic <string> resp;
                byte[] license;

                try
                {
                    identity = Signature.GetComputerInfo();
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Log.Error("Error while generating machine identity from WMI.", ex);
                        SetStatus2("Error while preparing to activate. Please inspect the software logs.", 3, true);
                        activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;
                    });
                    return;
                }

                try
                {
                    resp = API.ActivateMachine(name, key, identity);
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Log.Error("Error while sending request to the server.", ex);
                        SetStatus2("Error while contacting server. Please inspect the software logs.", 3, true);
                        activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;
                    });
                    return;
                }

                int code;
                if (!string.IsNullOrWhiteSpace(resp.Result) && Regex.IsMatch(resp.Result, @"^\d+$") && int.TryParse(resp.Result, out code))
                {
                    Dispatcher.Invoke(() =>
                    {
                        Log.Error("The activation server returned KeyStatus=" + ((Signature.KeyStatus)code) + (!string.IsNullOrWhiteSpace(resp.Error) ? " and error message:" + Environment.NewLine + resp.Error : "."));
                        SetStatus2("The specified key has been rejected by the server: " + ((Signature.KeyStatus)code), 3, true);
                        activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;

                        var reason = string.Empty;

                        switch ((Signature.KeyStatus)code)
                        {
                        case Signature.KeyStatus.Unchecked:
                            reason = "There was an internal error while preforming the check.";
                            break;

                        case Signature.KeyStatus.Invalid:
                            reason = "The key is not registered on the server, and it is not even cryptographically valid.";
                            break;

                        case Signature.KeyStatus.Unrecognized:
                            reason = "The key is not registered on the server, however it is cryptographically valid.";
                            break;

                        case Signature.KeyStatus.Revoked:
                            reason = "The license under this key was revoked by the user or system.";
                            break;

                        case Signature.KeyStatus.Suspended:
                            reason = "The key was suspended, possibly due to suspicious activity and is pending manual approval.";
                            break;

                        case Signature.KeyStatus.Disabled:
                            reason = "The key was disabled.";
                            break;

                        case Signature.KeyStatus.Exhausted:
                            reason = "The key has reached its machine activation limit.\r\nLogin at https://tvshowtracker.net/donate/login in order to deactivate earlier installations.";
                            break;
                        }

                        TaskDialog.Show(new TaskDialogOptions
                        {
                            MainIcon                = VistaTaskDialogIcon.Error,
                            Title                   = "Activation Error",
                            MainInstruction         = "Activation Error",
                            Content                 = "Activation request rejected by server:" + Environment.NewLine + Environment.NewLine + reason,
                            AllowDialogCancellation = true,
                            CustomButtons           = new[] { "OK" }
                        });
                    });
                    return;
                }

                if (!string.IsNullOrWhiteSpace(resp.Error))
                {
                    Dispatcher.Invoke(() =>
                    {
                        Log.Error("Error received from server while activating:" + Environment.NewLine + resp.Error);
                        SetStatus2("Error received from server. Please inspect the software logs.", 3, true);
                        activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;
                    });
                    return;
                }

                try
                {
                    license = Convert.FromBase64String(resp.Result);
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Log.Error("Error while decrypting server response:" + Environment.NewLine + resp.Result, ex);
                        SetStatus2("Error while decrypting license. Please inspect the software logs.", 3, true);
                        activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;
                    });
                    return;
                }

                try
                {
                    Signature.SaveLicense(name, key, license);
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Log.Error("Error while saving license to disk.", ex);
                        SetStatus2("Error while saving license. Please inspect the software logs.", 3, true);
                        activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;
                    });
                    return;
                }

                try
                {
                    Signature.InitLicense();
                }
                catch (Exception ex)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Log.Error("Error while loading license into software.", ex);
                        SetStatus2("Error while loading license. Please inspect the software logs.", 3, true);
                        activateButton.IsEnabled = closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;
                    });
                    return;
                }

                Dispatcher.Invoke(() =>
                {
                    SetStatus(true);
                    closeButton.IsEnabled = emailTextBox.IsEnabled = keyTextBox.IsEnabled = true;
                    MainWindow.Active.ReindexDownloadPaths.IsEnabled = true;
                    MainWindow.Active.ReindexDownloadPathsClick();
                });
            });
        }