public Book CreateFromSourceBook(string sourceBookFolder, string containingDestinationFolder)
{
string pathToFolderOfNewBook = null;
Logger.WriteMinorEvent("Starting CreateFromSourceBook({0})", sourceBookFolder);
try
{
var starter = _bookStarterFactory();
pathToFolderOfNewBook = starter.CreateBookOnDiskFromTemplate(sourceBookFolder, containingDestinationFolder);
if (Configurator.IsConfigurable(pathToFolderOfNewBook))
{
var c = _configuratorFactory(containingDestinationFolder);
if (DialogResult.Cancel == c.ShowConfigurationDialog(pathToFolderOfNewBook))
{
SIL.IO.RobustIO.DeleteDirectory(pathToFolderOfNewBook, true);
return(null); // the template had a configuration page and they clicked "cancel"
}
c.ConfigureBook(BookStorage.FindBookHtmlInFolder(pathToFolderOfNewBook));
}
var newBookInfo = new BookInfo(pathToFolderOfNewBook, true); // _bookInfos.Find(b => b.FolderPath == pathToFolderOfNewBook);
if (newBookInfo is ErrorBookInfo)
{
throw ((ErrorBookInfo)newBookInfo).Exception;
}
Book newBook = GetBookFromBookInfo(newBookInfo);
//Hack: this is a bit of a hack, to handle problems where we make the book with the suggested initial name, but the title is still something else
var name = Path.GetFileName(newBookInfo.FolderPath); // this way, we get "my book 1", "my book 2", etc.
newBook.SetTitle(name);
Logger.WriteMinorEvent("Finished CreateFromnewBook({0})", newBook.FolderPath);
Logger.WriteEvent("CreateFromSourceBook({0})", newBook.FolderPath);
return(newBook);
}
catch (Exception)
{
Logger.WriteEvent("Cleaning up after error CreateFromSourceBook({0})", pathToFolderOfNewBook);
//clean up this ill-fated book folder up
if (!string.IsNullOrEmpty(pathToFolderOfNewBook) && Directory.Exists(pathToFolderOfNewBook))
{
SIL.IO.RobustIO.DeleteDirectory(pathToFolderOfNewBook, true);
}
throw;
}
}