Beispiel #1
0
        private void button_RefreshGroups_Click(object sender, EventArgs e)
        {
            // Before we do anything, we need to be authenticated
            string reviewboardServer = Names.Url[(int)Names.Type.Reviewboard];

            if (Credentials.Available(reviewboardServer) == false)
            {
                m_logger.Log("Requesting Reviewboard credentials");

                DialogResult dialogResult = MessageBox.Show(this, "You must be authenticated with the Reviewboard server before refreshing the review groups.\n\nDo you want to authenticate now?", "Authentication Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dialogResult == DialogResult.Yes)
                {
                    RB_Tools.Shared.Authentication.Targets.Reviewboard.Authenticate(m_logger);
                }

                // Check if we're still unauthenticated
                if (Credentials.Available(reviewboardServer) == false)
                {
                    m_logger.Log("Credentials still unavailable");
                    return;
                }
            }

            // Turn off the buttons and lock the settings
            UpdateCreateReviewDialogState(State.RefreshGroups);

            // Lose everything in the box
            checkedListBox_ReviewGroups.Items.Clear();
            m_reviewGroups = new Reviewboard.ReviewGroup[0];

            Settings.Settings.Default.Selected = string.Empty;
            Settings.Settings.Default.Groups   = string.Empty;

            Settings.Settings.Default.Save();

            // Build up the background work
            BackgroundWorker updateThread = new BackgroundWorker();

            // Called when we need to trigger the request
            updateThread.DoWork += (object objectSender, DoWorkEventArgs args) =>
            {
                m_logger.Log("Starting group refresh");

                // Get our credentials
                Simple credentials = Credentials.Create(reviewboardServer, m_logger) as Simple;
                if (credentials == null)
                {
                    throw new FileNotFoundException(@"Unable to find the credentials for " + reviewboardServer);
                }

                // Get the groups
                Reviewboard.ReviewGroup[] result = Reviewboard.GetReviewGroups(m_requestDirectory, credentials.Server, credentials.User, credentials.Password, m_logger);

                // Save the result
                args.Result = result;
            };
            // Called when the thread is complete
            updateThread.RunWorkerCompleted += (object objectSender, RunWorkerCompletedEventArgs args) =>
            {
                // Check if we had an error
                if (args.Error != null)
                {
                    m_logger.Log("Error raised when updating review groups - {0}", args.Error.Message);
                    string body = string.Format("Exception thrown when trying to retrive the review groups\n\nException: {0}\n\nDescription: {1}", args.Error.GetType().Name, args.Error.Message);

                    // Was it an authentication error?
                    bool authenticationRequired = false;
                    if (args.Error.Message.Contains("username or password was not correct") == true)
                    {
                        authenticationRequired = true;
                        body += "\n\nDo you want to attempt to reauthenticate with the server?";
                    }

                    // Show the options
                    MessageBoxButtons buttonTypes = (authenticationRequired == true ? MessageBoxButtons.YesNo : MessageBoxButtons.OK);
                    DialogResult      result      = MessageBox.Show(this, body, @"Unable to update group list", buttonTypes, MessageBoxIcon.Error);

                    // Continue?
                    if (result == DialogResult.Yes && authenticationRequired == true)
                    {
                        RB_Tools.Shared.Authentication.Targets.Reviewboard.Authenticate(m_logger);
                    }
                }
                else
                {
                    m_logger.Log("Review groups successfully updated");

                    // Update the list
                    m_reviewGroups = args.Result as Reviewboard.ReviewGroup[];
                    UpdateSelectedReviewGroups(null);

                    // Save the groups we have returned
                    string groupsJson = JsonConvert.SerializeObject(m_reviewGroups);
                    Settings.Settings.Default.Groups = groupsJson;
                    Settings.Settings.Default.Save();
                }

                // Set the button state back
                UpdateCreateReviewDialogState(State.Idle);
            };

            // Kick off the request
            updateThread.RunWorkerAsync();
        }