/// <summary> /// Loads the content of the web. /// </summary> /// <remarks>Documented by Dev05, 2009-06-26</remarks> public void LoadWebContent(object path) { IsListLoaded = false; IsLoaded = false; string configPath = path as string; nodes = new Dictionary<int, FeedCategoryTreeNode>(); if (TreeView.InvokeRequired) TreeView.Invoke((MethodInvoker)delegate { Nodes.Clear(); }); else Nodes.Clear(); try { if (TreeView.InvokeRequired) TreeView.Invoke((MethodInvoker)delegate { Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name; }); else Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name; #region read categories XmlReader categoryFeedReader = XmlReader.Create(Feed.CategoriesUri); SyndicationFeed categoryFeed = SyndicationFeed.Load(categoryFeedReader); List<ModuleCategory> categoriesToAdd = new List<ModuleCategory>(); foreach (SyndicationItem item in categoryFeed.Items) { ModuleCategory category = new ModuleCategory() { Id = Convert.ToInt32(item.Id), Title = item.Title.Text, ParentCategory = Convert.ToInt32(item.Links[0].Title) }; categoriesToAdd.Add(category); } categoriesToAdd.Sort((a, b) => a.Id.CompareTo(b.Id)); categoriesToAdd.ForEach(c => AddCategoryNode(c)); #endregion #region read modules XmlReader moduleFeedReader = XmlReader.Create(Feed.ModulesUri); SyndicationFeed moduleFeed = SyndicationFeed.Load(moduleFeedReader); List<ModuleInfo> modules = new List<ModuleInfo>(); XmlSerializer infoSerializer = new XmlSerializer(typeof(ModuleInfo)); Dictionary<string, SyndicationItem> items = new Dictionary<string, SyndicationItem>(); foreach (SyndicationItem item in moduleFeed.Items) { ModuleInfo info; if (item.Summary != null) info = (ModuleInfo)infoSerializer.Deserialize(XmlReader.Create(new StringReader(WebUtility.HtmlDecode(item.Summary.Text)))); else info = new ModuleInfo(); info.Id = item.Id; info.Title = item.Title.Text; info.EditDate = item.LastUpdatedTime.ToString(); if (item.Contributors.Count > 0) { info.Author = item.Contributors[0].Name; info.AuthorMail = item.Contributors[0].Email; info.AuthorUrl = item.Contributors[0].Uri; } if (item.Content is TextSyndicationContent) info.Description = (item.Content as TextSyndicationContent).Text; info.Categories = new SerializableList<string>(); foreach (SyndicationCategory cat in item.Categories) info.Categories.Add(cat.Label); foreach (SyndicationLink link in item.Links) { if (link.RelationshipType == AtomLinkRelationshipType.Module.ToString()) { info.Size = link.Length; info.DownloadUrl = link.Uri.AbsoluteUri; } } modules.Add(info); items.Add(info.Id, item); } categories.ForEach(c => nodes[c.Id].SetModuleList(modules.FindAll(p => p.Categories.Contains(c.Id.ToString())))); #endregion if (TreeView.InvokeRequired) TreeView.Invoke((MethodInvoker)delegate { Expand(); }); else Expand(); IsListLoaded = true; OnContentLoaded(EventArgs.Empty); #region read images using (PersistentMemoryCache<ModuleInfoCacheItem> cache = new PersistentMemoryCache<ModuleInfoCacheItem>("Feed_" + moduleFeed.Id)) { WebClient webClient = new WebClient(); foreach (ModuleInfo basicInfo in modules) { try { ModuleInfo info = basicInfo; SyndicationItem item = items[basicInfo.Id]; string cacheKey = String.Format("{0}##{1}##{2}", moduleFeed.Id, item.Id, item.LastUpdatedTime); if (cache.Contains(cacheKey)) { ModuleInfoCacheItem cacheItem = (ModuleInfoCacheItem)cache[cacheKey]; info.IconSmall = Convert.FromBase64String(cacheItem.IconSmall); info.IconBig = Convert.FromBase64String(cacheItem.IconBig); info.Preview = Convert.FromBase64String(cacheItem.Preview); } else { ModuleInfoCacheItem cacheItem = new ModuleInfoCacheItem(info.Id); foreach (SyndicationLink link in item.Links) { if (link.RelationshipType == AtomLinkRelationshipType.IconSmall.ToString()) cacheItem.IconSmall = Convert.ToBase64String(info.IconSmall = webClient.DownloadData(link.Uri)); if (link.RelationshipType == AtomLinkRelationshipType.IconBig.ToString()) cacheItem.IconBig = Convert.ToBase64String(info.IconBig = webClient.DownloadData(link.Uri)); if (link.RelationshipType == AtomLinkRelationshipType.Preview.ToString()) cacheItem.Preview = Convert.ToBase64String(info.Preview = webClient.DownloadData(link.Uri)); } cache.Set(cacheKey, cacheItem, DateTime.Now.AddYears(1)); } if (TreeView.InvokeRequired) TreeView.Invoke((MethodInvoker)delegate { LoadDetails(info); }); else LoadDetails(info); } catch (Exception exp) { Trace.WriteLine(exp.ToString()); } } cache.Dispose(); } #endregion IsLoaded = true; } catch (Exception) { try { if (TreeView.InvokeRequired) TreeView.Invoke((MethodInvoker)delegate { Text = Resources.FeedTreeNode_Text_Offline; }); else Text = Resources.FeedTreeNode_Text_Offline; } catch (ObjectDisposedException) { } } }
/// <summary> /// Loads the content of the web. /// </summary> /// <remarks>Documented by Dev05, 2009-06-26</remarks> public void LoadWebContent(object path) { IsListLoaded = false; IsLoaded = false; string configPath = path as string; nodes = new Dictionary <int, FeedCategoryTreeNode>(); if (TreeView.InvokeRequired) { TreeView.Invoke((MethodInvoker) delegate { Nodes.Clear(); }); } else { Nodes.Clear(); } try { if (TreeView.InvokeRequired) { TreeView.Invoke((MethodInvoker) delegate { Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name; }); } else { Text = String.IsNullOrWhiteSpace(Feed.Name) ? Resources.FeedTreeNode_Name : Feed.Name; } #region read categories XmlReader categoryFeedReader = XmlReader.Create(Feed.CategoriesUri); SyndicationFeed categoryFeed = SyndicationFeed.Load(categoryFeedReader); List <ModuleCategory> categoriesToAdd = new List <ModuleCategory>(); foreach (SyndicationItem item in categoryFeed.Items) { ModuleCategory category = new ModuleCategory() { Id = Convert.ToInt32(item.Id), Title = item.Title.Text, ParentCategory = Convert.ToInt32(item.Links[0].Title) }; categoriesToAdd.Add(category); } categoriesToAdd.Sort((a, b) => a.Id.CompareTo(b.Id)); categoriesToAdd.ForEach(c => AddCategoryNode(c)); #endregion #region read modules XmlReader moduleFeedReader = XmlReader.Create(Feed.ModulesUri); SyndicationFeed moduleFeed = SyndicationFeed.Load(moduleFeedReader); List <ModuleInfo> modules = new List <ModuleInfo>(); XmlSerializer infoSerializer = new XmlSerializer(typeof(ModuleInfo)); Dictionary <string, SyndicationItem> items = new Dictionary <string, SyndicationItem>(); foreach (SyndicationItem item in moduleFeed.Items) { ModuleInfo info; if (item.Summary != null) { info = (ModuleInfo)infoSerializer.Deserialize(XmlReader.Create(new StringReader(WebUtility.HtmlDecode(item.Summary.Text)))); } else { info = new ModuleInfo(); } info.Id = item.Id; info.Title = item.Title.Text; info.EditDate = item.LastUpdatedTime.ToString(); if (item.Contributors.Count > 0) { info.Author = item.Contributors[0].Name; info.AuthorMail = item.Contributors[0].Email; info.AuthorUrl = item.Contributors[0].Uri; } if (item.Content is TextSyndicationContent) { info.Description = (item.Content as TextSyndicationContent).Text; } info.Categories = new SerializableList <string>(); foreach (SyndicationCategory cat in item.Categories) { info.Categories.Add(cat.Label); } foreach (SyndicationLink link in item.Links) { if (link.RelationshipType == AtomLinkRelationshipType.Module.ToString()) { info.Size = link.Length; info.DownloadUrl = link.Uri.AbsoluteUri; } } modules.Add(info); items.Add(info.Id, item); } categories.ForEach(c => nodes[c.Id].SetModuleList(modules.FindAll(p => p.Categories.Contains(c.Id.ToString())))); #endregion if (TreeView.InvokeRequired) { TreeView.Invoke((MethodInvoker) delegate { Expand(); }); } else { Expand(); } IsListLoaded = true; OnContentLoaded(EventArgs.Empty); #region read images using (PersistentMemoryCache <ModuleInfoCacheItem> cache = new PersistentMemoryCache <ModuleInfoCacheItem>("Feed_" + moduleFeed.Id)) { WebClient webClient = new WebClient(); foreach (ModuleInfo basicInfo in modules) { try { ModuleInfo info = basicInfo; SyndicationItem item = items[basicInfo.Id]; string cacheKey = String.Format("{0}##{1}##{2}", moduleFeed.Id, item.Id, item.LastUpdatedTime); if (cache.Contains(cacheKey)) { ModuleInfoCacheItem cacheItem = (ModuleInfoCacheItem)cache[cacheKey]; info.IconSmall = Convert.FromBase64String(cacheItem.IconSmall); info.IconBig = Convert.FromBase64String(cacheItem.IconBig); info.Preview = Convert.FromBase64String(cacheItem.Preview); } else { ModuleInfoCacheItem cacheItem = new ModuleInfoCacheItem(info.Id); foreach (SyndicationLink link in item.Links) { if (link.RelationshipType == AtomLinkRelationshipType.IconSmall.ToString()) { cacheItem.IconSmall = Convert.ToBase64String(info.IconSmall = webClient.DownloadData(link.Uri)); } if (link.RelationshipType == AtomLinkRelationshipType.IconBig.ToString()) { cacheItem.IconBig = Convert.ToBase64String(info.IconBig = webClient.DownloadData(link.Uri)); } if (link.RelationshipType == AtomLinkRelationshipType.Preview.ToString()) { cacheItem.Preview = Convert.ToBase64String(info.Preview = webClient.DownloadData(link.Uri)); } } cache.Set(cacheKey, cacheItem, DateTime.Now.AddYears(1)); } if (TreeView.InvokeRequired) { TreeView.Invoke((MethodInvoker) delegate { LoadDetails(info); }); } else { LoadDetails(info); } } catch (Exception exp) { Trace.WriteLine(exp.ToString()); } } cache.Dispose(); } #endregion IsLoaded = true; } catch (Exception) { try { if (TreeView.InvokeRequired) { TreeView.Invoke((MethodInvoker) delegate { Text = Resources.FeedTreeNode_Text_Offline; }); } else { Text = Resources.FeedTreeNode_Text_Offline; } } catch (ObjectDisposedException) { } } }