//Sync the chapter list to the origin source. public bool SyncToOrigin() { SetUpdateProgress(0, 0, UpdateStates.Syncing); if (OriginSource == null) { return(false); } ChapterSource[] menuItems = OriginSource.GetMenuURLs(); if (!File.Exists(GetNovelCoverImageLocation())) { OriginSource.DownloadNovelCoverImage(GetNovelCoverImageLocation()); } if (menuItems == null) { SetUpdateProgress(0, 0, UpdateStates.Error); return(false); } List <Chapter> chapterListing = new List <Chapter>(); int updateCount = 0; for (int i = 0; i < menuItems.Length; i++) { try { //add new chapter if url does not exist in database if (!OriginSource.ChapterUrls.Where(originUrl => originUrl.Hash == menuItems[i].UrlHash).Any()) { Chapter newChapter = new Chapter(); newChapter.ChapterTitle = menuItems[i].Title; newChapter.NovelTitle = NovelTitle; newChapter.Read = false; newChapter.Index = i; newChapter.Novel = this; newChapter.Valid = true; newChapter.HashID = menuItems[i].UrlHash.ToString("X"); NovelLibrary.libraryData.Chapters.InsertOnSubmit(newChapter); NovelLibrary.libraryData.SubmitChanges(); //chapterList.Insert(i, newChapter); chapterListing.Add(newChapter); isDirty = true; updateCount++; ChapterUrl newChapterUrl = new ChapterUrl(); newChapterUrl.ChapterID = newChapter.ID; newChapterUrl.Url = menuItems[i].Url; newChapterUrl.Hash = menuItems[i].UrlHash; newChapterUrl.Vip = menuItems[i].Vip; newChapterUrl.SourceID = OriginSource.ID; newChapterUrl.Source = OriginSource; newChapterUrl.Chapter = newChapter; NovelLibrary.libraryData.ChapterUrls.InsertOnSubmit(newChapterUrl); NovelLibrary.libraryData.SubmitChanges(); } //chapter already exist within database. else { Chapter c = (from chapterUrl in OriginSource.ChapterUrls where chapterUrl.Url == menuItems[i].Url select chapterUrl.Chapter).First <Chapter>(); if (c.ChapterTitle != menuItems[i].Title) { c.ChapterTitle = menuItems[i].Title; } if (c.Index != i) { c.Index = i; } chapterListing.Add(c); } } catch (Exception e) { Console.WriteLine(e); return(false); } } chapterList.SyncBindingToConstrain(); //Remove chapter that no longer belong in the list. HashSet <int> includedID = new HashSet <int>(chapterListing.Select(c => c.ID)); Chapter[] allChapters = Chapters.ToArray(); for (int i = 0; i < allChapters.Count(); i++) { if (!includedID.Contains(allChapters[i].ID)) { Console.WriteLine("Deleting " + allChapters[i].ChapterTitle + " " + allChapters[i].ID); DeleteChapter(allChapters[i], false, false); } } RefreshCacheData(); if (updateCount == 0) { SetUpdateProgress(0, 0, UpdateStates.UpToDate); } else { SetUpdateProgress(updateCount, 0, UpdateStates.UpdateAvailable); } return(true); }
public override IEnumerable <IChapter> GetChildren() { return(Chapters.ToArray()); }