Example #1
0
        public static int Handle(HydrateParameters options)
        {
            if (!Directory.Exists(options.Path))
            {
                if (options.Path.Contains(".htm"))
                {
                    Debug.WriteLine("Supply only the directory, not the path to the file.");
                    Console.Error.WriteLine("Supply only the directory, not the path to the file.");
                }
                else
                {
                    Debug.WriteLine("Could not find " + options.Path);
                    Console.Error.WriteLine("Could not find " + options.Path);
                }
                return(1);
            }
            Console.WriteLine("Starting Hydrating.");

            var layout = new Layout
            {
                SizeAndOrientation = SizeAndOrientation.FromString(options.SizeAndOrientation)
            };

            if (File.Exists(TeamCollectionManager.GetTcLinkPathFromLcPath(Path.GetDirectoryName(options.Path))))
            {
                throw new ApplicationException("Hydrate command cannot currently be used in Team Collections");
                // To make this possible, we'd need to spin up a TeamCollectionManager and TeamCollection and pass the latter
                // to the Book as its SaveContext and still changes would be forbidden unless the book was checked out.
            }

            var collectionSettings = new CollectionSettings
            {
                XMatterPackName     = options.XMatter,
                Language1Iso639Code = options.VernacularIsoCode,
                Language2Iso639Code = string.IsNullOrWhiteSpace(options.NationalLanguage1IsoCode) ? options.VernacularIsoCode : options.NationalLanguage1IsoCode,
                Language3Iso639Code = options.NationalLanguage2IsoCode
            };

            XMatterPackFinder xmatterFinder = new XMatterPackFinder(new[] { BloomFileLocator.GetFactoryXMatterDirectory(), });
            var locator = new BloomFileLocator(collectionSettings, xmatterFinder, ProjectContext.GetFactoryFileLocations(),
                                               ProjectContext.GetFoundFileLocations(), ProjectContext.GetAfterXMatterFileLocations());

            // alwaysSaveable is fine here, as we already checked it's not a TC.
            var bookInfo = new BookInfo(options.Path, true, new AlwaysEditSaveContext());
            var book     = new Book.Book(bookInfo, new BookStorage(options.Path, locator, new BookRenamedEvent(), collectionSettings),
                                         null, collectionSettings, null, null, new BookRefreshEvent(), new BookSavedEvent());
            // This was added as part of the phase 1 changes towards the new language system, where book languages
            // are more clearly distinct from collection languages, and there's no sense (except underlying storage) in which
            // a book has languages that are not selected for display. This made it necessary to decide explicitly
            // whether passing the national language options implies that a book is bi- or tri-lingual. Andrew and I (JohnT)
            // could not think of any reason to pass the arguments at all except to achieve that, so I made it so.
            var langs = new List <string>();

            langs.Add(options.VernacularIsoCode);
            if (!string.IsNullOrEmpty(options.NationalLanguage1IsoCode) && options.NationalLanguage1IsoCode != options.VernacularIsoCode)
            {
                langs.Add(options.NationalLanguage1IsoCode);
            }
            if (!string.IsNullOrEmpty(options.NationalLanguage2IsoCode))
            {
                langs.Add(options.NationalLanguage2IsoCode);
            }
            book.SetMultilingualContentLanguages(langs.ToArray());

            //we might change this later, or make it optional, but for now, this will prevent surprises to processes
            //running this CLI... the folder name won't change out from under it.
            book.LockDownTheFileAndFolderName = true;

            book.SetLayout(layout);
            book.BringBookUpToDate(new NullProgress());
            Console.WriteLine("Finished Hydrating.");
            Debug.WriteLine("Finished Hydrating.");
            return(0);
        }
        public static void ChangeLayoutForAllContentPagesInAllBooks(IProgress progress, string collectionPath, string bookPath, string pageGuid)
        {
            if (!File.Exists(bookPath))
            {
                MessageBox.Show("Could not find template book " + bookPath);
                return;
            }
            if (!File.Exists(collectionPath))
            {
                MessageBox.Show("Could not find collection file " + collectionPath);
                return;
            }
            var collectionFolder = Path.GetDirectoryName(collectionPath);

            if (File.Exists(TeamCollectionManager.GetTcLinkPathFromLcPath(collectionFolder)))
            {
                MessageBox.Show("Change Layout command cannot currently be used in Team Collections");
                return;
                // To make this possible, we'd need to spin up a TeamCollectionManager and TeamCollection and pass the latter
                // to the Book as its SaveContext and still changes would be forbidden unless every book was checked out.
            }

            var problems                    = new StringBuilder();
            var collection                  = new BookCollection(collectionFolder, BookCollection.CollectionType.TheOneEditableCollection, new BookSelection(), null);
            var collectionSettings          = new CollectionSettings(collectionPath);
            XMatterPackFinder xmatterFinder = new XMatterPackFinder(new[]
            {
                BloomFileLocator.GetFactoryXMatterDirectory()
            });
            var locator = new BloomFileLocator(collectionSettings, xmatterFinder, ProjectContext.GetFactoryFileLocations(),
                                               ProjectContext.GetFoundFileLocations(), ProjectContext.GetAfterXMatterFileLocations());

            // AlwaysSaveable is fine here, as we checked above that it's not a TC.
            var templateBookInfo = new BookInfo(Path.GetDirectoryName(bookPath), true, new AlwaysEditSaveContext());
            var templateBook     = new Book.Book(templateBookInfo, new BookStorage(templateBookInfo.FolderPath, locator, new BookRenamedEvent(), collectionSettings),
                                                 null, collectionSettings, null, null, new BookRefreshEvent(), new BookSavedEvent(), new NoEditSaveContext());

            var   pageDictionary = templateBook.GetTemplatePagesIdDictionary();
            IPage page           = null;

            if (!pageDictionary.TryGetValue(pageGuid, out page))
            {
                MessageBox.Show("Could not find template page " + pageGuid);
                return;
            }

            int i = 0;

            foreach (var bookInfo in collection.GetBookInfos())
            {
                i++;
                try
                {
                    var book = new Book.Book(bookInfo,
                                             new BookStorage(bookInfo.FolderPath, locator, new BookRenamedEvent(), collectionSettings),
                                             null, collectionSettings, null, null, new BookRefreshEvent(), new BookSavedEvent());
                    //progress.WriteMessage("Processing " + book.TitleBestForUserDisplay + " " + i + "/" + collection.GetBookInfos().Count());
                    progress.ProgressIndicator.PercentCompleted = i * 100 / collection.GetBookInfos().Count();

                    book.ChangeLayoutForAllContentPages(page);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                    problems.AppendLine(Path.GetFileName(bookInfo.FolderPath));
                }
            }
            if (problems.Length == 0)
            {
                MessageBox.Show("All books converted successfully");
            }
            else
            {
                MessageBox.Show("Bloom had problems converting the following books; please check them:\n" + problems);
            }
        }