private void RaiseNewFeedsDiscovered(DiscoveredFeedsInfo feedInfo)
 {
     if (NewFeedsDiscovered != null)
     {
         try {
             NewFeedsDiscovered(this, new DiscoveredFeedsInfoEventArgs(feedInfo));
         } catch (Exception ex) {
             Log.Error("NewFeedsDiscovered() event causes an exception", ex);
         }
     }
 }
        private bool RaiseDiscoveredFeedsSubscribe(DiscoveredFeedsInfo feedInfo)
        {
            bool cancel = false;

            if (DiscoveredFeedsSubscribe != null)
            {
                try {
                    DiscoveredFeedsInfoCancelEventArgs ea = new DiscoveredFeedsInfoCancelEventArgs(feedInfo, cancel);
                    DiscoveredFeedsSubscribe(this, ea);
                    cancel = ea.Cancel;
                } catch (Exception ex) {
                    Log.Error("DiscoveredFeedsSubscribe() event causes an exception", ex);
                }
            }
            return(cancel);
        }
        private AppButtonToolCommand FindYetDiscoveredFeedMenuItem(DiscoveredFeedsInfo info)
        {
            if (info == null)
            {
                return(null);
            }

            AppButtonToolCommand foundItem = null;

            lock (SyncRoot)
            {
                foreach (AppButtonToolCommand item in discoveredFeeds.Keys)
                {
                    DiscoveredFeedsInfo itemInfo;
                    if (!discoveredFeeds.TryGetValue(item, out itemInfo))
                    {
                        continue;
                    }

                    if (0 == String.Compare(itemInfo.SiteBaseUrl, info.SiteBaseUrl, true))
                    {
                        foundItem = item;
                    }
                    else
                    {
                        List <string> knownFeeds = itemInfo.FeedLinks;
                        foreach (string feedLink in knownFeeds)
                        {
                            if (info.FeedLinks.Contains(feedLink))
                            {
                                foundItem = item;
                                break;
                            }
                        }
                    }

                    if (null != foundItem)
                    {
                        break;
                    }
                }
            }
            return(foundItem);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DiscoveredFeedsInfoCancelEventArgs"/> class.
 /// </summary>
 /// <param name="feedsInfo">The feeds info.</param>
 /// <param name="cancel">if set to <c>true</c> [cancel].</param>
 internal DiscoveredFeedsInfoCancelEventArgs(DiscoveredFeedsInfo feedsInfo, bool cancel)
 {
     Cancel         = cancel;
     this.feedsInfo = feedsInfo;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DiscoveredFeedsInfoCancelEventArgs"/> class.
 /// </summary>
 internal DiscoveredFeedsInfoCancelEventArgs() : base(false)
 {
     feedsInfo = new DiscoveredFeedsInfo();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DiscoveredFeedsInfoEventArgs"/> class.
 /// </summary>
 /// <param name="feedsInfo">The feeds info.</param>
 internal DiscoveredFeedsInfoEventArgs(DiscoveredFeedsInfo feedsInfo)
 {
     this.feedsInfo = feedsInfo;
 }
        /// <summary>
        /// Thread worker procedure.
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <param name="pageUrl"></param>
        /// <param name="pageTitle"></param>
        private void AsyncDiscoverFeedsInContent(string htmlContent, string pageUrl, string pageTitle)
        {
            if (string.IsNullOrEmpty(pageUrl))
            {
                return;
            }

            string baseUrl = GetBaseUrlOf(pageUrl);
            AppButtonToolCommand foundItem = null;

            lock (SyncRoot)
            {
                // simple search for baseUrl, so we may prevent lookup of the content
                foreach (AppButtonToolCommand item in discoveredFeeds.Keys)
                {
                    DiscoveredFeedsInfo info;
                    if (discoveredFeeds.TryGetValue(item, out info))
                    {
                        string url = info.SiteBaseUrl;
                        if (String.Equals(baseUrl, url, StringComparison.OrdinalIgnoreCase))
                        {
                            foundItem = item;
                        }
                    }
                }
            }

            if (foundItem != null)
            {
                foundItem.SharedProps.Caption = StripAndShorten(pageTitle);
            }
            else
            {
                NewsComponents.Feed.RssLocater locator = new NewsComponents.Feed.RssLocater(Proxy, RssBanditApplication.UserAgent);                     //we did not really need the proxy. Content is allready loaded
                List <string> feeds = null;
                try
                {
                    // You can use this to simplify debugging IEControl HTML output.
                    // That is slightly different than the HTML we would get from a direct request!
                    //					using(System.IO.StreamWriter writer = System.IO.File.CreateText(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "IEContent.htm"))) {
                    //						writer.Write(htmlContent);
                    //						writer.Flush();
                    //					}
                    feeds = locator.GetRssFeedsForUrlContent(pageUrl, htmlContent, false);
                }
                catch (Exception)
                {
                    //catch up all, if it fails, it returns an empty list
                }
                if (feeds != null && feeds.Count > 0)
                {
                    feeds = this.CheckAndRemoveSubscribedFeeds(feeds, baseUrl);
                }

                if (feeds != null && feeds.Count > 0)
                {
                    DiscoveredFeedsInfo info = new DiscoveredFeedsInfo(feeds, pageTitle, baseUrl);
                    this.Add(info);
                    return;
                }
            }

            RefreshDiscoveredItemContainer();
        }
        /// <summary>
        /// Will add a DiscoveredFeedsInfo entry to the managed tool dropdown.
        /// </summary>
        /// <param name="info">DiscoveredFeedsInfo instance</param>
        public void Add(DiscoveredFeedsInfo info)
        {
            if (info == null)
            {
                return;
            }

            // detect duplicates:
            AppButtonToolCommand duplicateItem = FindYetDiscoveredFeedMenuItem(info);

            if (duplicateItem != null)
            {
                // update title/desc:
                duplicateItem.SharedProps.Caption    = StripAndShorten(info.Title);
                duplicateItem.SharedProps.StatusText = info.FeedLinks[0];

                lock (SyncRoot) {
                    // refresh the existing info item to the new one
                    discoveredFeeds.Remove(duplicateItem);
                    discoveredFeeds.Add(duplicateItem, info);
                }
            }
            else
            {
                // new entry:
                WinGuiMain guiMain = (WinGuiMain)app.MainForm;

                GuiInvoker.InvokeAsync(guiMain, delegate
                {
                    //guiMain.AddAutoDiscoveredUrl(info);

                    AppButtonToolCommand newItem = new AppButtonToolCommand(
                        String.Concat("cmdDiscoveredFeed_", ++(cmdKeyPostfix)),
                        mediator,
                        OnDiscoveredItemClick,
                        StripAndShorten(info.Title), info.FeedLinks[0]);

                    if (itemDropdown.ToolbarsManager.Tools.Exists(newItem.Key))
                    {
                        itemDropdown.ToolbarsManager.Tools.Remove(newItem);
                    }

                    itemDropdown.ToolbarsManager.Tools.Add(newItem);
                    newItem.SharedProps.StatusText       = info.SiteBaseUrl;
                    newItem.SharedProps.ShowInCustomizer = false;

                    lock (SyncRoot)
                    {
                        // add a fresh version of info
                        discoveredFeeds.Add(newItem, info);
                    }

                    lock (newDiscoveredFeeds.SyncRoot)
                    {
                        // re-order to top of list, in RefreshItemContainer()
                        newDiscoveredFeeds.Enqueue(newItem);
                    }

                    RaiseNewFeedsDiscovered(info);
                });
            }

            RefreshDiscoveredItemContainer();
        }