private bool InsertSmartContentFromUrl(ContentSourceInfo contentSource, string url)
        {
            SmartContentSource smartSource = contentSource.Instance as SmartContentSource;
            string title = String.Empty;
            IExtensionData extensionData = _contentSourceSite.CreateExtensionData(Guid.NewGuid().ToString());
            ISmartContent smartContent = new SmartContent(extensionData);

            if (UrlContentRetreivalWithProgress.ExecuteSmartContentRetreival(
                EditorContext.FrameWindow, contentSource, url, ref title, smartContent))
            {
                string content = smartSource.GenerateEditorHtml(smartContent, _contentSourceSite);
                if (content != null)
                {
                    _contentSourceSite.InsertContent(contentSource.Id, content, extensionData);
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        private void BeforePublish(object sender, UpdateWeblogProgressForm.PublishingEventArgs args)
        {
            UpdateWeblogProgressForm form = (UpdateWeblogProgressForm)sender;

            bool noPluginsEnabled;
            if (!HeaderAndFooterPrerequisitesMet(form, out noPluginsEnabled))
            {
                Debug.Assert(!noPluginsEnabled, "No plugins enabled yet header/footer prerequisites met!?");
                args.RepublishOnSuccess = true;
                return;
            }

            if (noPluginsEnabled)
                return;

            BlogPost.Contents = SmartContentInsertionHelper.StripDivsWithClass(BlogPost.Contents, ContentSourceManager.HEADERS_FOOTERS);

            foreach (ContentSourceInfo csi in ContentSourceManager.GetActiveHeaderFooterPlugins(form, Blog.Id))
            {
                form.SetProgressMessage(string.Format(CultureInfo.InvariantCulture, Res.Get(StringId.PluginPublishProgress), csi.Name));

                HeaderFooterSource plugin = (HeaderFooterSource)csi.Instance;
                if (plugin.RequiresPermalink && string.IsNullOrEmpty(BlogPost.Permalink))
                {
                    Trace.WriteLine("Skipping plugin " + csi.Name + " because the post has no permalink");
                    continue;
                }
                IExtensionData extData =
                    ((IBlogPostEditingContext)this).ExtensionDataList.GetOrCreateExtensionData(csi.Id);
                SmartContent smartContent = new SmartContent(extData);
                HeaderFooterSource.Position position;
                string html;
                try
                {
                    html = plugin.GeneratePublishHtml(form, smartContent, _publishingContext, args.Publish, out position);
                }
                catch (Exception e)
                {
                    DisplayPluginException(form, csi, e);
                    continue;
                }

                if (string.IsNullOrEmpty(html))
                    continue;

                if (SmartContentInsertionHelper.ContainsUnbalancedDivs(html))
                {
                    Trace.Fail("Unbalanced divs detected in HTML generated by " + csi.Name + ": " + html);
                    DisplayMessage.Show(MessageId.MalformedHtmlIgnored, form, csi.Name);
                    continue;
                }

                // Don't use float alignment for footers--it causes them to float around
                // stuff that isn't part of the post
                bool noFloat = position == HeaderFooterSource.Position.Footer;
                string generatedHtml = SmartContentInsertionHelper.GenerateBlock(ContentSourceManager.HEADERS_FOOTERS, null, smartContent, false, html, noFloat, null);

                if (position == HeaderFooterSource.Position.Header)
                    BlogPost.Contents = generatedHtml + BlogPost.Contents;
                else if (position == HeaderFooterSource.Position.Footer)
                    BlogPost.Contents = BlogPost.Contents + generatedHtml;
                else
                    Debug.Fail("Unknown HeaderFooter position: " + position);
            }
            form.SetProgressMessage(null);

            BlogPost.Contents = HtmlLinebreakStripper.RemoveLinebreaks(BlogPost.Contents);
        }
        private bool InsertSmartContentFromEmbed(string embed)
        {
            ContentSourceInfo contentSource = ContentSourceManager.FindContentSource(typeof(VideoContentSource));

            if (contentSource != null)
            {
                SmartContentSource smartSource = contentSource.Instance as SmartContentSource;
                VideoContentSource videoSource = smartSource as VideoContentSource;
                IExtensionData extensionData = _contentSourceSite.CreateExtensionData(Guid.NewGuid().ToString());
                ISmartContent smartContent = new SmartContent(extensionData);
                videoSource.CreateContentFromEmbed(embed, smartContent);
                // generate html and insert it
                string content = videoSource.GenerateEditorHtml(smartContent, _contentSourceSite);
                if (content != null)
                {
                    _contentSourceSite.InsertContent(VideoContentSource.ID, content, extensionData);
                    return true;
                }
                else
                {
                    Trace.Fail("Video Source content generated from embed tag was empty");
                    return false;
                }
            }
            Trace.Fail("Cannot find the video plugin");
            return false;
        }
        private bool InsertSmartContentFromLiveClipboard(ContentSourceInfo contentSource, XmlDocument lcDocument)
        {
            SmartContentSource smartSource = contentSource.Instance as SmartContentSource;
            if (smartSource == null)
            {
                Trace.Fail("Unexpected failure to get live clipboard content-source!");
                return false;
            }

            // create the smart content
            IExtensionData extensionData = _contentSourceSite.CreateExtensionData(Guid.NewGuid().ToString());
            ISmartContent smartContent = new SmartContent(extensionData);
            if (smartSource.CreateContentFromLiveClipboard(EditorContext.FrameWindow, lcDocument, smartContent) == DialogResult.OK)
            {
                // generate html and insert it
                string content = smartSource.GenerateEditorHtml(smartContent, _contentSourceSite);
                if (content != null)
                {
                    _contentSourceSite.InsertContent(contentSource.Id, content, extensionData);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        IExtensionData[] IContentSourceSite.UpdateContent(IExtensionData[] extensionDataListOrginal)
        {
            IExtensionData[] extensionDataList = (IExtensionData[])extensionDataListOrginal.Clone();
            // Find all the smart content in the list, and tell them to update.
            IHTMLElement2 postBodyElement = (IHTMLElement2)_normalHtmlContentEditor.PostBodyElement;
            if (postBodyElement != null)
            {
                foreach (IHTMLElement divElement in postBodyElement.getElementsByTagName("div"))
                {
                    // Make sure that the div we have is a smart content, and that it isn't just a div that is part of smart content.
                    if (divElement.className == null || divElement.id == null || divElement.className == ContentSourceManager.SMART_CONTENT || !ContentSourceManager.IsSmartContent(divElement))
                        continue;

                    string contentSourceId = "";
                    string contentId = "";

                    // Get the contentid of the div smart content we have
                    ContentSourceManager.ParseContainingElementId(divElement.id, out contentSourceId, out contentId);

                    // Check to see if this smart content is one we want to update
                    int index = ArrayHelper.SearchForIndexOf<string, IExtensionData>(extensionDataList,
                                                             contentId,
                                                             delegate (string a, IExtensionData b) { return b != null && a == b.Id; });

                    if (index == -1)
                        continue;

                    // The smart content we found in the DOM is also one we
                    // want to update, so save a reference to it from the data list
                    IExtensionData extensionData = extensionDataList[index];

                    // We remove it from the list, which at the end of the function will only
                    // leave the IExtensionData that were not found in the editor.
                    extensionDataList[index] = null;

                    // Make a SmartContent for the element we are about to update,
                    // this will allow the content source to  get at the properties bag
                    // for the smart content so it knows how to update the content
                    SmartContent smartContent = new SmartContent(extensionData);

                    IContentSourceSidebarContext sourceContext = this;

                    // Find the content source that we will use to update the smart content
                    ContentSourceInfo contentSourceInfo = sourceContext.FindContentSource(contentSourceId);

                    // Make sure that we found one, and that is it indeed SmartContentSource
                    if (contentSourceInfo != null && contentSourceInfo.Instance is SmartContentSource)
                    {
                        // Get the html that the content source wants to update the element with
                        string newHtml = ((SmartContentSource)contentSourceInfo.Instance).GenerateEditorHtml(smartContent, this);

                        // If the source for this smart content wants to filter some updates
                        // give it a chance to right now.
                        if (contentSourceInfo.Instance is IContentUpdateFilter)
                        {
                            if (!((IContentUpdateFilter)contentSourceInfo.Instance).ShouldUpdateContent(divElement.innerHTML, newHtml))
                                continue;
                        }

                        // If we are actually sure we want to update the element then we insert it
                        // and wrap it in an invisible undo unit so it will undo the last change the user
                        // made if they ctrl+z
                        using (EditorUndoUnit undo = new EditorUndoUnit(_currentEditor, true))
                        {
                            SmartContentInsertionHelper.InsertContentIntoElement(newHtml, smartContent, sourceContext, divElement);
                            _htmlEditorSidebarHost.ForceUpdateSidebarState();
                            undo.Commit();
                        }

                    }
                }
            }

            return (IExtensionData[])ArrayHelper.Compact(extensionDataList);
        }
        public void InsertSmartContentFromFile(string[] files, string contentSourceID, HtmlInsertionOptions insertionOptions, object context)
        {
            files = RemoveInvalidImages(files);

            if (files == null || files.Length == 0)
                return;

            // Get a reference to the video plugin
            ISupportsDragDropFile contentSource =
                (ISupportsDragDropFile)ContentSourceManager.GetContentSourceInfoById(contentSourceID).Instance;

            if (contentSource == null)
                return;

            IExtensionData extensionData = (this as IContentSourceSite).CreateExtensionData(Guid.NewGuid().ToString());
            ISmartContent sContent = new SmartContent(extensionData);

            if (context == null)
                context = new InternalSmartContentContext(this, contentSourceID);

            using (EditorUndoUnit undo = new EditorUndoUnit(_currentEditor))
            {
                // Have the plugin try to make some new content from the path that we just got through the drag and drop
                if (contentSource.CreateContentFromFile(_mainFrameWindow, sContent, files, context) == DialogResult.OK)
                {
                    string content = ((SmartContentSource)contentSource).GenerateEditorHtml(sContent, this);
                    if (content != null)
                    {
                        ((IContentSourceSite)this).InsertContent(contentSourceID, content, extensionData, insertionOptions);
                        undo.Commit();
                    }
                    Focus();

                }

            }
        }
        private ISmartContent GetSmartContentForPublishHook(ContentSourceInfo csi)
        {
            // Use the plugin ID as content ID because there can only be
            // one per post anyway

            Debug.Assert(typeof(HeaderFooterSource).IsAssignableFrom(csi.Type));

            string contentId = csi.Id;
            ISmartContent smartContent =
                ((IContentSourceSidebarContext)this).FindSmartContent(contentId);
            if (smartContent == null)
                smartContent = new SmartContent(((IContentSourceSite)this).CreateExtensionData(contentId));
            return smartContent;
        }
        public static void PerformInsertion(IContentSourceSite sourceSite, ContentSourceInfo contentSource)
        {
            // record use of content-source (used to list source in MRU order on the sidebar)
            RecordContentSourceUsage(contentSource.Id);

            try
            {
                if (contentSource.Instance is SmartContentSource)
                {
                    SmartContentSource scSource = (SmartContentSource)contentSource.Instance;

                    IExtensionData extensionData = sourceSite.CreateExtensionData(Guid.NewGuid().ToString());

                    // SmartContentSource implementations *must* be stateless (see WinLive 126969), so we wrap up the
                    // internal smart content context and pass it in as a parameter to the CreateContent call.
                    ISmartContent sContent;
                    if (scSource is IInternalSmartContentSource)
                    {
                        sContent = new InternalSmartContent(extensionData, sourceSite as IInternalSmartContentContextSource, contentSource.Id);
                    }
                    else
                    {
                        sContent = new SmartContent(extensionData);
                    }

                    if (scSource.CreateContent(sourceSite.DialogOwner, sContent) == DialogResult.OK)
                    {
                        string content = scSource.GenerateEditorHtml(sContent, sourceSite);
                        if (content != null)
                        {
                            sourceSite.InsertContent(contentSource.Id, content, extensionData);
                            sourceSite.Focus();

                            if (ApplicationPerformance.ContainsEvent(MediaInsertForm.EventName))
                                ApplicationPerformance.EndEvent(MediaInsertForm.EventName);
                        }
                    }
                }
                else if (contentSource.Instance is ContentSource)
                {
                    ContentSource sSource = (ContentSource)contentSource.Instance;
                    string newContent = String.Empty; // default
                    try { if (sourceSite.SelectedHtml != null) newContent = sourceSite.SelectedHtml; }
                    catch { } // safely try to provide selected html
                    if (sSource.CreateContent(sourceSite.DialogOwner, ref newContent) == DialogResult.OK)
                    {
                        sourceSite.InsertContent(newContent, contentSource.Id == WebImageContentSource.ID);
                        sourceSite.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                DisplayContentRetreivalError(sourceSite.DialogOwner, ex, contentSource);
            }
        }
 public SmartContentItem(string contentSourceId, string contentBlockId, SmartContent smartContent)
 {
     _contentSourceId = contentSourceId;
     _contentBlockId = contentBlockId;
     _smartContent = smartContent;
 }