예제 #1
0
        public void Save()
        {
            try
            {
                // Give a slight preference to USB keys, though if they used a different directory last time, we favor that.

                if (string.IsNullOrEmpty(_lastDirectory) || !Directory.Exists(_lastDirectory))
                {
                    var drives = SIL.UsbDrive.UsbDriveInfo.GetDrives();
                    if (drives != null && drives.Count > 0)
                    {
                        _lastDirectory = drives[0].RootDirectory.FullName;
                    }
                }

                var portion = "";
                switch (BookletPortion)
                {
                case BookletPortions.None:
                    Debug.Fail("Save should not be enabled");
                    return;

                case BookletPortions.AllPagesNoBooklet:
                    portion = "Pages";
                    break;

                case BookletPortions.BookletCover:
                    portion = "Cover";
                    break;

                case BookletPortions.BookletPages:
                    portion = "Inside";
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                string forPrintShop =
                    _currentlyLoadedBook.UserPrefs.CmykPdf || _currentlyLoadedBook.UserPrefs.FullBleed
                                                ? "-printshop"
                                                : "";
                string suggestedName = string.Format($"{Path.GetFileName(_currentlyLoadedBook.FolderPath)}-{_currentlyLoadedBook.GetFilesafeLanguage1Name("en")}-{portion}{forPrintShop}.pdf");
                var    pdfFileLabel  = L10NSharp.LocalizationManager.GetString(@"PublishTab.PdfMaker.PdfFile",
                                                                               "PDF File",
                                                                               @"displayed as file type for Save File dialog.");

                pdfFileLabel = pdfFileLabel.Replace("|", "");
                var pdfFilter = String.Format("{0}|*.pdf", pdfFileLabel);

                var startingFolder = (!string.IsNullOrEmpty(_lastDirectory) && Directory.Exists(_lastDirectory)) ?
                                     _lastDirectory :
                                     Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                var initialPath = Path.Combine(startingFolder, suggestedName);

                var destFileName = Utils.MiscUtils.GetOutputFilePathOutsideCollectionFolder(initialPath, pdfFilter);
                if (String.IsNullOrEmpty(destFileName))
                {
                    return;
                }

                _lastDirectory = Path.GetDirectoryName(destFileName);
                if (_currentlyLoadedBook.UserPrefs.CmykPdf)
                {
                    // PDF for Printshop (CMYK US Web Coated V2)
                    ProcessPdfFurtherAndSave(ProcessPdfWithGhostscript.OutputType.Printshop, destFileName);
                }
                else
                {
                    // we want the simple PDF we already made.
                    RobustFile.Copy(PdfFilePath, destFileName, true);
                }
                Analytics.Track("Save PDF", new Dictionary <string, string>()
                {
                    { "Portion", Enum.GetName(typeof(BookletPortions), BookletPortion) },
                    { "Layout", PageLayout.ToString() },
                    { "BookId", BookSelection.CurrentSelection.ID },
                    { "Country", _collectionSettings.Country }
                });
            }
            catch (Exception err)
            {
                SIL.Reporting.ErrorReport.NotifyUserOfProblem("Bloom was not able to save the PDF.  {0}", err.Message);
            }
        }