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 problems = new StringBuilder(); var collectionFolder = Path.GetDirectoryName(collectionPath); var collection = new BookCollection(collectionFolder, BookCollection.CollectionType.TheOneEditableCollection, new BookSelection()); var collectionSettings = new CollectionSettings(collectionPath); XMatterPackFinder xmatterFinder = new XMatterPackFinder(new[] { BloomFileLocator.GetInstalledXMatterDirectory() }); var locator = new BloomFileLocator(collectionSettings, xmatterFinder, ProjectContext.GetFactoryFileLocations(), ProjectContext.GetFoundFileLocations(), ProjectContext.GetAfterXMatterFileLocations()); var templateBookInfo = new BookInfo(Path.GetDirectoryName(bookPath), true); var templateBook = new Book.Book(templateBookInfo, new BookStorage(templateBookInfo.FolderPath, locator, new BookRenamedEvent(), collectionSettings), null, collectionSettings, null, null, new BookRefreshEvent()); 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()); //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); } }
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); } }