Example #1
0
        private async void sendFilesButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DisableAll();
            var statusLabel = new ToolStripStatusLabel("Sending files");

            statusBar.Items.Add(statusLabel);
            var selectedUsers = usersListBox.SelectedItems;

            if (selectedUsers.Count > 0)
            {
                var openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = openFileDialog.FileName;
                    string fileName = openFileDialog.SafeFileName;

                    foreach (User user in selectedUsers)
                    {
                        try
                        {
                            if (!encryptCheckBox.Checked)
                            {
                                statusLabel.Text = string.Format("Uploading {0} to cloud", fileName);
                                await _cloud.UploadFileAsync(filePath, user, fileName);

                                if (user == _currentUser)
                                {
                                    filesListBox.DataSource = await _cloud.GetFilesListAsync(_currentUser);
                                }
                            }
                            else
                            {
                                var tempFile        = Path.GetTempFileName();
                                var tempCertificate = Path.GetTempFileName();
                                statusLabel.Text = string.Format("Downloading certificate ({0})", user.CommonName);
                                await _cloud.DownloadCertificateAsync(tempCertificate, user);

                                statusLabel.Text = string.Format("Encrypting {0}", fileName);
                                await Encrypt.FileWithPublicKey(filePath, tempFile, new UserCertificate(tempCertificate));

                                statusLabel.Text = string.Format("Uploading {0} to cloud", fileName);;
                                await _cloud.UploadFileAsync(tempFile, user, fileName);

                                if (user == _currentUser)
                                {
                                    filesListBox.DataSource = await _cloud.GetFilesListAsync(_currentUser);
                                }
                                File.Delete(tempFile);
                                File.Delete(tempCertificate);
                            }
                        }
                        catch (CloudException cloud)
                        {
                            MessageBox.Show(cloud.Message, "Cloud problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Unexcepted error occured. Click OK to contiune", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Select users first", "Select users", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            EnableAll();
            statusBar.Items.Remove(statusLabel);
            Cursor = Cursors.Arrow;
        }