/// <summary>Deletes completely specified Information Items.</summary> /// <param name="itemIds">Array of IDs of the Information Items.</param> public void BurnUp(IEnumerable itemIds) { if (itemIds == null) { return; } this.Load(); foreach (int itemId in itemIds) { foreach (InfoItem refItem in _infoItems) { if (itemId == refItem.Id) { _infoItems.Remove(refItem); break; } } } XmlSerializer writer = new XmlSerializer(typeof(InfoItem[])); using (StreamWriter file = new StreamWriter(_path)) { InfoItem[] array = new InfoItem[_infoItems.Count]; _infoItems.CopyTo(array); writer.Serialize(file, array); } }
public void Merge(InfoItem precedingItem) { if (_attachments != null && precedingItem.Attachments != null) { foreach (InfoAttach attach in _attachments) { foreach (InfoAttach precAttach in precedingItem.Attachments) { if (attach.Id == precAttach.Id && attach.Timestamp == precAttach.Timestamp) { attach.Status = precAttach.Status; break; } } } } #if ZEPTAIR if (_command != null && precedingItem.Command != null) { InfoCommand precCmd = precedingItem.Command; if (_command.Id == precCmd.Id && _command.Timestamp == precCmd.Timestamp) { _command = precCmd; } } #endif }
/// <summary>Throws the specified Information Items into Trash.</summary> /// <param name="items">Array of the Information Items.</param> public void Push(IEnumerable items) { if (items == null) { return; } this.Load(); foreach (InfoItem item in items) { _infoItems.Add(item); } if (_infoItems.Count > FULL_COUNT) { _infoItems.RemoveRange(0, _infoItems.Count - FULL_COUNT); } XmlSerializer writer = new XmlSerializer(typeof(InfoItem[])); using (StreamWriter file = new StreamWriter(_path)) { InfoItem[] array = new InfoItem[_infoItems.Count]; _infoItems.CopyTo(array); writer.Serialize(file, array); } }
/// <summary>Restore specified Information Items.</summary> /// <param name="itemIds">Array of IDs of the Information Items.</param> public void Restore(IEnumerable itemIds) { if (itemIds == null) { return; } this.Load(); ArrayList items = new ArrayList(); foreach (int itemId in itemIds) { foreach (InfoItem refItem in _infoItems) { if (itemId == refItem.Id) { items.Add(refItem); _infoItems.Remove(refItem); break; } } } if (items.Count <= 0) { return; } XmlSerializer writer = new XmlSerializer(typeof(InfoItem[])); using (StreamWriter file = new StreamWriter(_path)) { InfoItem[] array = new InfoItem[_infoItems.Count]; _infoItems.CopyTo(array); writer.Serialize(file, array); } RssTargetInfo[] targetInfos = TaskMain.Instance.RssManager.TargetInfos; RssTargetInfo targetInfo = RssTargetInfo.FindGenerator(targetInfos, ((InfoItem)items[0]).GeneratorId); #if ZEPTAIR if (targetInfo.IsZeptDist) { TaskMain.Instance.ZeptDistManager.PostItems(items, true); } else #endif TaskMain.Instance.InfoManager.PostItems(items, true); }
/// <summary>Removes the specified Information Items.</summary> /// <param name="itemIds">Array of IDs of the Information Items.</param> /// <param name="bLoadSave">Flag to Load and Save the file.</param> public void RemoveItems(int[] itemIds, bool bLoadSave) { if (bLoadSave) { _Load(); } if (itemIds == null || itemIds.Length <= 0) { return; } RssTargetInfo[] targetInfos = TaskMain.Instance.RssManager.TargetInfos; foreach (int id in itemIds) { InfoItem item = null; foreach (InfoItem elem in _infoItems) { if (elem.Id == id) { item = elem; break; } } if (item == null) { continue; } _infoItems.Remove(item); RssTargetInfo targetInfo = RssTargetInfo.FindGenerator(targetInfos, item.GeneratorId); if (targetInfo != null) { targetInfo.TrashBox.Push(new InfoItem[1] { item }); } } if (bLoadSave) { _Save(); } }
/// <summary>Saves all Information Items.</summary> protected void _Save() { if (_infoItems == null) { return; } if (!Directory.Exists(ThetisCore.Lib.Def.INFO_ROOT_DIR)) { Directory.CreateDirectory(ThetisCore.Lib.Def.INFO_ROOT_DIR); } InfoItem[] array = new InfoItem[_infoItems.Count]; _infoItems.CopyTo(array); try { XmlSerializer writer = new XmlSerializer(typeof(InfoItem[])); string fpath = ThetisCore.Lib.Def.INFO_ITEMS_FILE_PATH; CommonUtil.OpenStreamSafe(fpath, "w", (f) => { StreamWriter file = (StreamWriter)f; try { writer.Serialize(file, array); } catch (IOException ioe) { Log.AddError(ioe.Message + "\n" + ioe.StackTrace); } } ); } catch (Exception e) { Log.AddError(e.Message + "\n" + e.StackTrace); } }
/// <summary>Creates an instance from a RSS Item.</summary> /// <param name="rssItem">RSS Item as source.</param> /// <param name="channel">The title of the channel.</param> /// <param name="targetEntry">RSS Target Entry.</param> public static InfoItem Create(RssItem rssItem, string channel, RssTargetEntry targetEntry) { InfoItem item = new InfoItem(); item._generatorId = targetEntry.Id; item._author = rssItem.Author; if (rssItem.Categories != null) { string[] categories = new string[rssItem.Categories.Count]; int idx = 0; foreach (RssCategory cat in rssItem.Categories) { categories[idx++] = cat.Name; } item._categories = categories; } item._comments = rssItem.Comments; item._description = rssItem.Description; item._link = rssItem.Link.ToString(); item._guid = rssItem.Guid.Name; item._pubDate = rssItem.PubDate; item._title = rssItem.Title; item._srcTitle = targetEntry.Title; item._channel = channel; if (rssItem.Enclosures != null) { ArrayList imageList = new ArrayList(); ArrayList attachList = new ArrayList(); foreach (RssEnclosure enclosure in rssItem.Enclosures) { if (enclosure.Type != null && enclosure.Type.IndexOf("image/") >= 0 && !targetEntry.IsZeptDist) { imageList.Add(enclosure.Url.ToString()); } else { attachList.Add(InfoAttach.Create(enclosure)); } } if (imageList.Count > 0) { item._images = (string[])imageList.ToArray(typeof(string)); } if (attachList.Count > 0) { item._attachments = (InfoAttach[])attachList.ToArray(typeof(InfoAttach)); } } #if ZEPTAIR if (rssItem.Description != null && rssItem.Description.Length > 0) { item._command = InfoCommand.Create(rssItem.Description); } #endif return(item); }
/// <summary>Does read the target URL.</summary> /// <param name="state"></param> public void DoRead(Object state) { _mutex.WaitOne(); if (!_activated) { _mutex.ReleaseMutex(); return; } Log.AddInfo(@"RssWatcher.DoRead() : " + this._targetInfo.Title); string url = _targetInfo.Url; #if ZEPTAIR if (_targetInfo.IsZeptDist) { Zeptair.Lib.Common.ConfParam zeptConf = TaskMain.Instance.ZeptConf(false); if (zeptConf.SpecifyAdminNames && zeptConf.AdminNames != null && zeptConf.AdminNames.Length > 0) { if (url.IndexOf('?') >= 0) { url += @"&"; } else { url += @"?"; } url += @"admins=" + zeptConf.AdminNames; } } #endif RssFeed feed = null; try { ServicePointManager.ServerCertificateValidationCallback = ThetisCore.Lib.EasyTrustPolicy.CheckValidationResult; HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url); if (_targetInfo.UserName != null && _targetInfo.UserName.Length > 0) { webReq.Credentials = new NetworkCredential(_targetInfo.UserName, _targetInfo.Password); } if (_lastFeed == null) { feed = RssFeed.Read(webReq); } else { feed = RssFeed.Read(webReq, _lastFeed); } _lastFeed = feed; } catch (WebException we) { Log.AddError(" " + we.Message + "\n" + we.StackTrace); } if (feed != null && !feed.Cached) { bool modified = false; ArrayList postArray = new ArrayList(); if (_feedHistory == null) { _feedHistory = feed; foreach (RssChannel channel in feed.Channels) { foreach (RssItem item in channel.Items) { postArray.Add(InfoItem.Create(item, channel.Title, _targetInfo)); } } modified = true; } else { foreach (RssChannel channel in feed.Channels) { int historyIdx = RssHelper.IndexOf(_feedHistory.Channels, channel); if (historyIdx < 0) { _feedHistory.Channels.Add(channel); foreach (RssItem item in channel.Items) { postArray.Add(InfoItem.Create(item, channel.Title, _targetInfo)); } modified = true; } else { RssItemCollection historyItems = _feedHistory.Channels[historyIdx].Items; foreach (RssItem item in channel.Items) { if (!RssHelper.Contains(historyItems, item)) { historyItems.Add(item); postArray.Add(InfoItem.Create(item, channel.Title, _targetInfo)); modified = true; } } } } } if (modified) { _feedHistory.LastModified = feed.LastModified; _targetInfo.SaveFeedHistory(_feedHistory); _manager.PostItems(postArray, _targetInfo); postArray.Clear(); } postArray = null; } _mutex.ReleaseMutex(); }