private void bnDetectCoworkers_Click(object sender, EventArgs e)
        {
            if (_backgroundWorker.IsBusy)
            {
                if (_backgroundWorker.WorkerSupportsCancellation)
                {
                    DialogResult answer = MessageBox.Show("Really cancel auto-detection?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (answer == DialogResult.Yes)
                    {
                        _backgroundWorker.CancelAsync();
                    }
                }
            }
            else
            {
                try
                {
                    bool okToDetect = true;

                    string folderPath = folder.Text.Trim(new char[] { '"' });
                    if (okToDetect && !Directory.Exists(folderPath))
                    {
                        MessageBox.Show(this, "Cannot find specified folder", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        okToDetect = false;
                    }

                    SDConnectorUseCaseEngine.Engine sdsEngine = new SDConnectorUseCaseEngine.Engine();

                    if (okToDetect && !sdsEngine.UserIsConnected())
                    {
                        MessageBox.Show(this, "A user must be connected to Stormshield Data Security", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        okToDetect = false;
                    }

                    if (okToDetect)
                    {
                        InitializeWorkInProgress(WorkInProgress.AutoDetectCoworkers);
                        _backgroundWorker.RunWorkerAsync(folderPath);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void bnCreateTeamRule_Click(object sender, EventArgs e)
        {
            InitializeWorkInProgress(WorkInProgress.CreateRule);

            try
            {
                bool okToCreate = true;

                string folderPath = folder.Text;
                if (okToCreate && !Directory.Exists(folderPath))
                {
                    MessageBox.Show(this, "Cannot find specified folder", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    okToCreate = false;
                }

                SDConnectorUseCaseEngine.Engine sdsEngine = new SDConnectorUseCaseEngine.Engine();

                if (okToCreate && !sdsEngine.UserIsConnected())
                {
                    MessageBox.Show(this, "A user must be connected to Stormshield Data Security", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    okToCreate = false;
                }

                if (okToCreate)
                {
                    List <X509Certificate> certificates = new List <X509Certificate>();
                    foreach (KeyValuePair <string, Tuple <string, X509Certificate> > coworker in _coworkers)
                    {
                        certificates.Add(coworker.Value.Item2);
                    }

                    sdsEngine.CreateTeamRule(folderPath, certificates.ToArray());
                    MessageBox.Show(this, "Rule created successfully", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            TerminateWorkInProgress();
        }
        void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker     = sender as BackgroundWorker;
            string           folderPath = e.Argument as string;

            _engine.Worker = worker;

            _coworkers.Clear();
            _notFoundCertificates.Clear();

            SDConnectorUseCaseEngine.Engine sdsEngine = new SDConnectorUseCaseEngine.Engine();

            Dictionary <string, string> users = _engine.FetchUsers(folderPath);

            int i = 0;

            foreach (KeyValuePair <string, string> user in users)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                string name  = user.Key;
                string email = user.Value;

                X509Certificate certificate = sdsEngine.GetCertificate(email);
                if (certificate == null)
                {
                    _notFoundCertificates.Add(email);
                }
                else
                {
                    _coworkers.Add(name, Tuple.Create <string, X509Certificate>(email, certificate));
                }

                worker.ReportProgress(0, string.Format("Checking certificates - {0}/{1}", i + 1, users.Count));

                i++;
            }
        }