public static WriterEditingManifest FromHomepage(LazyHomepageDownloader homepageDownloader, Uri homepageUri, IBlogClient blogClient, IBlogCredentialsAccessor credentials)
        {
            if (homepageUri == null)
            {
                return(null);
            }

            WriterEditingManifest editingManifest = null;

            try
            {
                // compute the "by-convention" url for the manifest
                string homepageUrl = UrlHelper.InsureTrailingSlash(UrlHelper.SafeToAbsoluteUri(homepageUri));
                string manifestUrl = UrlHelper.UrlCombine(homepageUrl, "wlwmanifest.xml");

                // test to see whether this url exists and has a valid manifest
                editingManifest = FromUrl(new Uri(manifestUrl), blogClient, credentials, false);

                // if we still don't have one then scan homepage contents for a link tag
                if (editingManifest == null)
                {
                    string manifestLinkTagUrl = ScanHomepageContentsForManifestLink(homepageUri, homepageDownloader);
                    if (manifestLinkTagUrl != null)
                    {
                        // test to see whether this url exists and has a valid manifest
                        try
                        {
                            editingManifest = FromUrl(new Uri(manifestLinkTagUrl), blogClient, credentials, true);
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine("Error attempting to download manifest from " + manifestLinkTagUrl + ": " + ex.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Unexpected exception attempting to discover manifest from " + UrlHelper.SafeToAbsoluteUri(homepageUri) + ": " + ex.ToString());
            }

            // return whatever editing manifest we found
            return(editingManifest);
        }
Пример #2
0
        private WriterEditingManifest SafeDownloadEditingManifest()
        {
            WriterEditingManifest editingManifest = null;

            try
            {
                // create a blog client
                IBlogClient blogClient = CreateBlogClient();

                // can we get one based on cached download info
                IBlogCredentialsAccessor credentialsToUse = (IncludeInsecureOperations || blogClient.IsSecure) ? _context.Credentials : null;
                if (_context.ManifestDownloadInfo != null)
                {
                    string manifestUrl = _context.ManifestDownloadInfo.SourceUrl;
                    if (UseManifestCache)
                    {
                        editingManifest = WriterEditingManifest.FromDownloadInfo(_context.ManifestDownloadInfo, blogClient, credentialsToUse, true);
                    }
                    else
                    {
                        editingManifest = WriterEditingManifest.FromUrl(new Uri(manifestUrl), blogClient, credentialsToUse, true);
                    }
                }

                // if we don't have one yet then probe for one
                if (editingManifest == null)
                {
                    editingManifest = WriterEditingManifest.FromHomepage(_homepageAccessor, new Uri(_context.HomepageUrl), blogClient, credentialsToUse);
                }
            }
            catch (Exception ex)
            {
                ReportException("attempting to download editing manifest", ex);
            }

            // return whatever we found
            return(editingManifest);
        }
Пример #3
0
        public object DetectSettings(IProgressHost progressHost)
        {
            using (_silentMode ? new BlogClientUIContextSilentMode() : null)
            {
                if (IncludeButtons || IncludeOptionOverrides || IncludeImages)
                {
                    using (new ProgressContext(progressHost, 40, Res.Get(StringId.ProgressDetectingWeblogSettings)))
                    {
                        // attempt to download editing manifest
                        WriterEditingManifest editingManifest = SafeDownloadEditingManifest();

                        if (editingManifest != null)
                        {
                            // always update the download info
                            if (editingManifest.DownloadInfo != null)
                            {
                                _context.ManifestDownloadInfo = editingManifest.DownloadInfo;
                            }

                            // images
                            if (IncludeImages)
                            {
                                // image if provided
                                if (editingManifest.Image != null)
                                {
                                    _context.Image = editingManifest.Image;
                                }

                                // watermark if provided
                                if (editingManifest.Watermark != null)
                                {
                                    _context.WatermarkImage = editingManifest.Watermark;
                                }
                            }

                            // buttons if provided
                            if (IncludeButtons && (editingManifest.ButtonDescriptions != null))
                            {
                                _context.ButtonDescriptions = editingManifest.ButtonDescriptions;
                            }

                            // option overrides if provided
                            if (IncludeOptionOverrides)
                            {
                                if (editingManifest.ClientType != null)
                                {
                                    _context.ClientType = editingManifest.ClientType;
                                }

                                if (editingManifest.OptionOverrides != null)
                                {
                                    _context.OptionOverrides = editingManifest.OptionOverrides;
                                }
                            }
                        }
                    }
                }

                using (new ProgressContext(progressHost, 40, Res.Get(StringId.ProgressDetectingWeblogCharSet)))
                {
                    if (IncludeOptionOverrides && IncludeHomePageSettings)
                    {
                        DetectHomePageSettings();
                    }
                }

                IBlogClient blogClient = CreateBlogClient();
                if (IncludeInsecureOperations || blogClient.IsSecure)
                {
                    if (blogClient is ISelfConfiguringClient)
                    {
                        // This must happen before categories detection but after manifest!!
                        ((ISelfConfiguringClient)blogClient).DetectSettings(_context, this);
                    }

                    // detect categories
                    if (IncludeCategories)
                    {
                        using (
                            new ProgressContext(progressHost, 20, Res.Get(StringId.ProgressDetectingWeblogCategories)))
                        {
                            BlogPostCategory[] categories = SafeDownloadCategories();
                            if (categories != null)
                            {
                                _context.Categories = categories;
                            }

                            BlogPostKeyword[] keywords = SafeDownloadKeywords();
                            if (keywords != null)
                            {
                                _context.Keywords = keywords;
                            }
                        }
                    }

                    // detect favicon (only if requested AND we don't have a PNG already
                    // for the small image size)
                    if (IncludeFavIcon)
                    {
                        using (new ProgressContext(progressHost, 10, Res.Get(StringId.ProgressDetectingWeblogIcon)))
                        {
                            byte[] favIcon = SafeDownloadFavIcon();
                            if (favIcon != null)
                            {
                                _context.FavIcon = favIcon;
                            }
                        }
                    }

                    if (IncludeImageEndpoints)
                    {
                        Debug.WriteLine("Detecting image endpoints");
                        ITemporaryBlogSettingsDetectionContext tempContext =
                            _context as ITemporaryBlogSettingsDetectionContext;
                        Debug.Assert(tempContext != null,
                                     "IncludeImageEndpoints=true but non-temporary context (type " +
                                     _context.GetType().Name + ") was used");
                        if (tempContext != null)
                        {
                            tempContext.AvailableImageEndpoints = null;
                            try
                            {
                                BlogInfo[] imageEndpoints = blogClient.GetImageEndpoints();
                                tempContext.AvailableImageEndpoints = imageEndpoints;
                                Debug.WriteLine(imageEndpoints.Length + " image endpoints detected");
                            }
                            catch (NotImplementedException)
                            {
                                Debug.WriteLine("Image endpoints not implemented");
                            }
                            catch (Exception e)
                            {
                                Trace.Fail("Exception detecting image endpoints: " + e.ToString());
                            }
                        }
                    }
                }
                // completed
                progressHost.UpdateProgress(100, 100, Res.Get(StringId.ProgressCompletedSettingsDetection));
            }

            return(this);
        }
        private BlogEditingTemplateFiles SafeGetTemplates(IProgressHost progress)
        {
            WriterEditingManifest    editingManifest = null;
            BlogEditingTemplateFiles templateFiles   = new BlogEditingTemplateFiles();

            try
            {
                // if we have a manifest url then try to get our manifest
                if (_manifestDownloadInfo != null)
                {
                    // try to get the editing manifest
                    string manifestUrl = _manifestDownloadInfo.SourceUrl;
                    editingManifest = WriterEditingManifest.FromUrl(
                        new Uri(manifestUrl),
                        _blogClient,
                        _credentials,
                        true);

                    // progress
                    CheckCancelRequested(progress);
                    progress.UpdateProgress(20, 100);
                }

                // if we have no editing manifest then probe (if allowed)
                if ((editingManifest == null) && _probeForManifest)
                {
                    editingManifest = WriterEditingManifest.FromHomepage(
                        new LazyHomepageDownloader(_blogHomepageUrl, new HttpRequestHandler(_blogClient.SendAuthenticatedHttpRequest)),
                        new Uri(_blogHomepageUrl),
                        _blogClient,
                        _credentials);
                }

                // progress
                CheckCancelRequested(progress);
                progress.UpdateProgress(40, 100);

                // if we got one then return templates from it as-appropriate
                if (editingManifest != null)
                {
                    if (editingManifest.WebLayoutUrl != null)
                    {
                        string webLayoutTemplate = DownloadManifestTemplate(new ProgressTick(progress, 10, 100), editingManifest.WebLayoutUrl);
                        if (BlogEditingTemplate.ValidateTemplate(webLayoutTemplate))
                        {
                            // download supporting files
                            string templateFile = DownloadTemplateFiles(webLayoutTemplate, _blogHomepageUrl, new ProgressTick(progress, 20, 100));

                            // return the template
                            templateFiles.FramedTemplate = new BlogEditingTemplateFile(BlogEditingTemplateType.Framed, templateFile);
                        }
                        else
                        {
                            Trace.WriteLine("Invalid webLayoutTemplate specified in manifest");
                        }
                    }

                    if (editingManifest.WebPreviewUrl != null)
                    {
                        string webPreviewTemplate = DownloadManifestTemplate(new ProgressTick(progress, 10, 100), editingManifest.WebPreviewUrl);
                        if (BlogEditingTemplate.ValidateTemplate(webPreviewTemplate))
                        {
                            // download supporting files
                            string templateFile = DownloadTemplateFiles(webPreviewTemplate, _blogHomepageUrl, new ProgressTick(progress, 20, 100));

                            // return the template
                            templateFiles.WebPageTemplate = new BlogEditingTemplateFile(BlogEditingTemplateType.Webpage, templateFile);
                        }
                        else
                        {
                            Trace.WriteLine("Invalid webPreviewTemplate specified in manifest");
                        }
                    }
                }
            }
            catch
            {
            }
            finally
            {
                progress.UpdateProgress(100, 100);
            }

            return(templateFiles);
        }
Пример #5
0
        protected override object DetectBlogService(IProgressHost progressHost)
        {
            using (BlogClientUIContextSilentMode uiContextScope = new BlogClientUIContextSilentMode()) //suppress prompting for credentials
            {
                try
                {
                    // get the weblog homepage and rsd service description if available
                    IHTMLDocument2 weblogDOM = GetWeblogHomepageDOM(progressHost);

                    // while we have the DOM available, scan for a writer manifest url
                    if (_manifestDownloadInfo == null)
                    {
                        string manifestUrl = WriterEditingManifest.DiscoverUrl(_homepageUrl, weblogDOM);
                        if (manifestUrl != String.Empty)
                        {
                            _manifestDownloadInfo = new WriterEditingManifestDownloadInfo(manifestUrl);
                        }
                    }

                    string html = weblogDOM != null?HTMLDocumentHelper.HTMLDocToString(weblogDOM) : null;

                    bool detectionSucceeded = false;

                    if (!detectionSucceeded)
                    {
                        detectionSucceeded = AttemptGenericAtomLinkDetection(_homepageUrl, html, !ApplicationDiagnostics.PreferAtom);
                    }

                    if (!detectionSucceeded && _blogSettings.IsGoogleBloggerBlog)
                    {
                        detectionSucceeded = AttemptBloggerDetection(_homepageUrl, html);
                    }

                    if (!detectionSucceeded)
                    {
                        RsdServiceDescription rsdServiceDescription = GetRsdServiceDescription(progressHost, weblogDOM);

                        // if there was no rsd service description or we fail to auto-configure from the
                        // rsd description then move on to other auto-detection techniques
                        if (!(detectionSucceeded = AttemptRsdBasedDetection(progressHost, rsdServiceDescription)))
                        {
                            // try detection by analyzing the homepage url and contents
                            UpdateProgress(progressHost, 75, Res.Get(StringId.ProgressAnalyzingHomepage));
                            if (weblogDOM != null)
                            {
                                detectionSucceeded = AttemptHomepageBasedDetection(_homepageUrl, html);
                            }
                            else
                            {
                                detectionSucceeded = AttemptUrlBasedDetection(_homepageUrl);
                            }

                            // if we successfully detected then see if we can narrow down
                            // to a specific weblog
                            if (detectionSucceeded)
                            {
                                if (!BlogProviderParameters.UrlContainsParameters(_postApiUrl))
                                {
                                    // we detected the provider, now see if we can detect the weblog id
                                    // (or at lease the list of the user's weblogs)
                                    UpdateProgress(progressHost, 80, Res.Get(StringId.ProgressAnalyzingWeblogList));
                                    AttemptUserBlogDetection();
                                }
                            }
                        }
                    }

                    if (!detectionSucceeded && html != null)
                    {
                        AttemptGenericAtomLinkDetection(_homepageUrl, html, false);
                    }

                    // finished
                    UpdateProgress(progressHost, 100, String.Empty);
                }
                catch (OperationCancelledException)
                {
                    // WasCancelled == true
                }
                catch (BlogClientOperationCancelledException)
                {
                    Cancel();
                    // WasCancelled == true
                }
                catch (BlogAccountDetectorException ex)
                {
                    if (ApplicationDiagnostics.AutomationMode)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                    else
                    {
                        Trace.Fail(ex.ToString());
                    }
                    // ErrorOccurred == true
                }
                catch (Exception ex)
                {
                    // ErrorOccurred == true
                    Trace.Fail(ex.Message, ex.ToString());
                    ReportError(MessageId.WeblogDetectionUnexpectedError, ex.Message);
                }

                return(this);
            }
        }