private void _uploadButton_Click(object sender, EventArgs e)
        {
            if (_uploadSource.SelectedIndex == 1)
            {
                BulkUploadThisCollection();
                return;
            }
            if (_uploadSource.SelectedIndex == 2)
            {
                SelectFolderAndUploadCollectionsWithinIt();
                return;
            }
            if (_uploadWorker != null)
            {
                // We're already doing an upload, this is now the Cancel button.
                _progressBox.CancelRequested = true;
                return;
            }
            _progressBox.CancelRequested = false;
            ScrollControlIntoView(_progressBox);
            _progressBox.Clear();

            if (_signLanguageCheckBox.Checked && string.IsNullOrEmpty(CurrentSignLanguageName))
            {
                // report error in progress and bail
                _progressBox.WriteMessageWithColor(Color.Red,
                                                   LocalizationManager.GetString("PublishTab.Upload.ChooseSignLanguageWarning",
                                                                                 "Please choose the sign language for this book"));
                return;
            }

            if (_model.IsTemplate)
            {
                var msg = LocalizationManager.GetString("PublishTab.Upload.Template",
                                                        "This book seems to be a template, that is, it contains blank pages for authoring a new book "
                                                        + "rather than content to translate into other languages. "
                                                        + "If that is not what you intended, you should get expert help before uploading this book."
                                                        + "\n\n"
                                                        + "Do you want to go ahead?");
                var warning = LocalizationManager.GetString("Warning", "Warning");
                if (MessageBox.Show(Form.ActiveForm, msg,
                                    warning, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }
            }

            try
            {
                _progressBox.WriteMessage("Checking bloom version eligibility...");
                if (!_model.IsThisVersionAllowedToUpload)
                {
                    MessageBox.Show(this,
                                    LocalizationManager.GetString("PublishTab.Upload.OldVersion",
                                                                  "Sorry, this version of Bloom Desktop is not compatible with the current version of BloomLibrary.org. Please upgrade to a newer version."),
                                    LocalizationManager.GetString("PublishTab.Upload.UploadNotAllowed", "Upload Not Allowed"),
                                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    _progressBox.WriteMessage("Canceled.");
                    return;
                }

                // Todo: try to make sure it has a thumbnail.

                _progressBox.WriteMessage("Checking for existing copy on server...");
                if (_model.BookIsAlreadyOnServer)
                {
                    using (var dlg = new OverwriteWarningDialog())
                    {
                        if (dlg.ShowDialog() == DialogResult.Cancel)
                        {
                            _progressBox.WriteMessage("Canceled.");
                            return;
                        }
                    }
                }
            }
            catch (Exception)
            {
                ReportTryAgainDuringUpload();
                _uploadButton.Enabled = true;
                return;
            }
            _progressBox.WriteMessage("Starting...");
            _uploadWorker         = new BackgroundWorker();
            _uploadWorker.DoWork += BackgroundUpload;
            _uploadWorker.WorkerReportsProgress = true;
            _uploadWorker.RunWorkerCompleted   += (theWorker, completedEvent) =>
            {
                // Return all controls to normal state. (Do this first, just in case we get some further exception somehow.)
                // I believe the event is guaranteed to be raised, even if something in the worker thread throws,
                // so there should be no way to get stuck in the state where the tabs etc. are disabled.
                SetStateOfNonUploadControls(true);
                // Don't call UpdateDisplay, it will wipe out the progress messages.
                if (_progressBox.CancelRequested)
                {
                    _progressBox.WriteMessageWithColor(Color.Red, LocalizationManager.GetString("PublishTab.Upload.Cancelled", "Upload was cancelled"));
                }
                else
                {
                    if (completedEvent.Error != null)
                    {
                        string errorMessage = GetBasicErrorUploadingMessage();
                        _progressBox.WriteError(errorMessage, _model.Title);
                        _progressBox.WriteException(completedEvent.Error);
                    }
                    else if ((string)completedEvent.Result == "quiet")
                    {
                        // no more reporting, sufficient message already given.
                    }
                    else if (string.IsNullOrEmpty((string)completedEvent.Result))
                    {
                        // Something went wrong, possibly already reported.
                        if (!_model.PdfGenerationSucceeded)
                        {
                            ReportPdfGenerationFailed();
                        }
                        else
                        {
                            ReportTryAgainDuringUpload();
                        }
                    }
                    else
                    {
                        var    url             = BloomLibraryUrlPrefix + "/my-books/book/" + _parseId;
                        string congratsMessage = LocalizationManager.GetString("PublishTab.Upload.UploadCompleteNotice",
                                                                               "Congratulations, \"{0}\" is now available on BloomLibrary.org ({1})",
                                                                               "{0} is the book title; {1} is a clickable url which will display the book on the website");
                        _progressBox.WriteMessageWithColor(Color.Blue, congratsMessage, _model.Title, url);
                    }
                }
                _uploadWorker = null;
            };
            SetStateOfNonUploadControls(false);             // Last thing we do before launching the worker, so we can't get stuck in this state.
            _uploadWorker.RunWorkerAsync(_model.Book);
        }
Exemplo n.º 2
0
        private void _uploadButton_Click(object sender, EventArgs e)
        {
            _uploadButton.Enabled = false;             // can't start another until done.
            ScrollControlIntoView(_progressBox);
            _progressBox.Clear();
            var info = _book.BookInfo;

            if (string.IsNullOrEmpty(info.Id))
            {
                info.Id = Guid.NewGuid().ToString();
            }
            info.Uploader = _bookTransferrer.UserId;

            _progressBox.WriteMessage("Checking bloom version eligibility...");
            if (!_bookTransferrer.IsThisVersionAllowedToUpload())
            {
                MessageBox.Show(this,
                                LocalizationManager.GetString("PublishTab.Upload.OldVersion",
                                                              "Sorry, this version of Bloom Desktop is not compatible with the current version of BloomLibrary.org. Please upgrade to a newer version."),
                                LocalizationManager.GetString("PublishTab.Upload.UploadNotAllowed", "Upload Not Allowed"),
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                _progressBox.WriteMessage("Canceled.");
                return;
            }

            // Todo: try to make sure it has a thumbnail.

            _progressBox.WriteMessage("Checking for existing copy on server...");
            if (_bookTransferrer.IsBookOnServer(_book.FolderPath))
            {
                using (var dlg = new OverwriteWarningDialog())
                {
                    if (dlg.ShowDialog() == DialogResult.Cancel)
                    {
                        _progressBox.WriteMessage("Canceled.");
                        return;
                    }
                }
            }
            _progressBox.WriteMessage("Starting...");
            var worker = new BackgroundWorker();

            worker.DoWork += BackgroundUpload;
            worker.WorkerReportsProgress = true;
            worker.RunWorkerCompleted   += (theWorker, completedEvent) =>
            {
                if (completedEvent.Error != null)
                {
                    string errorMessage = LocalizationManager.GetString("PublishTab.Upload.ErrorUploading", "Sorry, there was a problem uploading {0}. Some details follow. You may need technical help.");
                    _progressBox.WriteError(errorMessage, _book.Title);
                    _progressBox.WriteException(completedEvent.Error);
                }
                else if (string.IsNullOrEmpty((string)completedEvent.Result))
                {
                    // Something went wrong, typically already reported.
                    string sorryMessage = LocalizationManager.GetString("PublishTab.Upload.FinalUploadFailureNotice", "Sorry, \"{0}\" was not successfully uploaded");
                    _progressBox.WriteError(sorryMessage, _book.Title);
                }
                else
                {
                    var    url             = BloomLibraryUrlPrefix + "/browse/detail/" + _parseId;
                    string congratsMessage = LocalizationManager.GetString("PublishTab.Upload.UploadCompleteNotice", "Congratulations, \"{0}\" is now available on BloomLibrary.org ({1})");
                    _progressBox.WriteMessageWithColor(Color.Blue, congratsMessage, _book.Title, url);
                }
                _uploadButton.Enabled = true;                 // Don't call UpdateDisplay, it will wipe out the progress messages.
            };
            worker.RunWorkerAsync(_book);
            //_bookTransferrer.UploadBook(_book.FolderPath, AddNotification);
        }
        private void _uploadButton_Click(object sender, EventArgs e)
        {
            if (_uploadWorker != null)
            {
                // We're already doing an upload, this is now the Cancel button.
                _progressBox.CancelRequested = true;
                return;
            }
            _progressBox.CancelRequested = false;
            ScrollControlIntoView(_progressBox);
            _progressBox.Clear();
            var info = _book.BookInfo;

            if (string.IsNullOrEmpty(info.Id))
            {
                info.Id = Guid.NewGuid().ToString();
            }
            info.Uploader = _bookTransferrer.UserId;

            if (_book.BookInfo.IsSuitableForMakingShells)
            {
                var msg = LocalizationManager.GetString("PublishTab.Upload.Template",
                                                        "This book seems to be a template, that is, it contains blank pages for authoring a new book "
                                                        + "rather than content to translate into other languages. "
                                                        + "If that is not what you intended, you should get expert help before uploading this book."
                                                        + "\n\n"
                                                        + "Do you want to go ahead?");
                var warning = LocalizationManager.GetString("Warning", "Warning");
                if (MessageBox.Show(Form.ActiveForm, msg,
                                    warning, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }
            }

            try
            {
                _progressBox.WriteMessage("Checking bloom version eligibility...");
                if (!_bookTransferrer.IsThisVersionAllowedToUpload())
                {
                    MessageBox.Show(this,
                                    LocalizationManager.GetString("PublishTab.Upload.OldVersion",
                                                                  "Sorry, this version of Bloom Desktop is not compatible with the current version of BloomLibrary.org. Please upgrade to a newer version."),
                                    LocalizationManager.GetString("PublishTab.Upload.UploadNotAllowed", "Upload Not Allowed"),
                                    MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    _progressBox.WriteMessage("Canceled.");
                    return;
                }

                // Todo: try to make sure it has a thumbnail.

                _progressBox.WriteMessage("Checking for existing copy on server...");
                if (_bookTransferrer.IsBookOnServer(_book.FolderPath))
                {
                    using (var dlg = new OverwriteWarningDialog())
                    {
                        if (dlg.ShowDialog() == DialogResult.Cancel)
                        {
                            _progressBox.WriteMessage("Canceled.");
                            return;
                        }
                    }
                }
            }
            catch (Exception)
            {
                ReportTryAgainDuringUpload();
                _uploadButton.Enabled = true;
                return;
            }
            _progressBox.WriteMessage("Starting...");
            _uploadWorker         = new BackgroundWorker();
            _uploadWorker.DoWork += BackgroundUpload;
            _uploadWorker.WorkerReportsProgress = true;
            _uploadWorker.RunWorkerCompleted   += (theWorker, completedEvent) =>
            {
                // Return all controls to normal state. (Do this first, just in case we get some further exception somehow.)
                // I believe the event is guaranteed to be raised, even if something in the worker thread throws,
                // so there should be no way to get stuck in the state where the tabs etc. are disabled.
                SetStateOfNonUploadControls(true);
                // Don't call UpdateDisplay, it will wipe out the progress messages.
                if (_progressBox.CancelRequested)
                {
                    _progressBox.WriteMessageWithColor(Color.Red, LocalizationManager.GetString("PublishTab.Upload.Cancelled", "Upload was cancelled"));
                }
                else
                {
                    if (completedEvent.Error != null)
                    {
                        string errorMessage = LocalizationManager.GetString("PublishTab.Upload.ErrorUploading",
                                                                            "Sorry, there was a problem uploading {0}. Some details follow. You may need technical help.");
                        _progressBox.WriteError(errorMessage, _book.Title);
                        _progressBox.WriteException(completedEvent.Error);
                    }
                    else if (string.IsNullOrEmpty((string)completedEvent.Result))
                    {
                        // Something went wrong, typically already reported.
                        ReportTryAgainDuringUpload();
                    }
                    else
                    {
                        var    url             = BloomLibraryUrlPrefix + "/browse/detail/" + _parseId;
                        string congratsMessage = LocalizationManager.GetString("PublishTab.Upload.UploadCompleteNotice",
                                                                               "Congratulations, \"{0}\" is now available on BloomLibrary.org ({1})");
                        _progressBox.WriteMessageWithColor(Color.Blue, congratsMessage, _book.Title, url);
                    }
                }
                _uploadWorker = null;
            };
            SetStateOfNonUploadControls(false);             // Last thing we do before launching the worker, so we can't get stuck in this state.
            _uploadWorker.RunWorkerAsync(_book);
            //_bookTransferrer.UploadBook(_book.FolderPath, AddNotification);
        }