Exemple #1
0
        /// <summary>
        /// Saves a document to Scribd
        /// </summary>
        public void Save()
        {
            if (this.DocumentId == 0)
            {
                Service.OnErrorOccurred(666, "You must have a DocumentId indicated.");
                return;
            }
            DocumentEventArgs _arguments = new DocumentEventArgs(this);
            OnBeforeSave(_arguments);
            if (_arguments.Cancel)
            {
                // Get out!
                return;
            }

            // Build our request
            using (Request _request = new Request())
            {
                _request.MethodName = "docs.changeSettings";

                _request.Parameters.Add("doc_ids", this.DocumentId.ToString());

                if (!string.IsNullOrEmpty(this.Title))          // no sense setting title to null OR ""
                {
                _request.Parameters.Add("title", this.Title);
                }

                if (this.Description != null)                   // null means to leave description unchanged
                {
                _request.Parameters.Add("description", this.Description);
                }

                _request.Parameters.Add("access", this.AccessType == AccessTypes.Public ? "public" : "private");

                // License
                switch (this.License)
                {
                    case CCLicenseTypes.BY: _request.Parameters.Add("license", "by"); break;
                    case CCLicenseTypes.BY_NC: _request.Parameters.Add("license", "by-nc"); break;
                    case CCLicenseTypes.BY_NC_ND: _request.Parameters.Add("license", "by-nc-nd"); break;
                    case CCLicenseTypes.BY_NC_SA: _request.Parameters.Add("license", "by-nc-sa"); break;
                    case CCLicenseTypes.BY_ND: _request.Parameters.Add("license", "by-nd"); break;
                    case CCLicenseTypes.BY_SA: _request.Parameters.Add("license", "by-sa"); break;
                    case CCLicenseTypes.C: _request.Parameters.Add("license", "c"); break;
                    case CCLicenseTypes.PD: _request.Parameters.Add("license", "pd"); break;
                    default: break;
                }

                // Ads
                switch (this.ShowAds)
                {
                    case ShowAdsTypes.Default: _request.Parameters.Add("show_ads", "default"); break;
                    case ShowAdsTypes.True: _request.Parameters.Add("show_ads", "true"); break;
                    case ShowAdsTypes.False: _request.Parameters.Add("show_ads", "false"); break;
                    default: break;
                }

                if (this.LinkBackURL != null && !string.IsNullOrEmpty(this.LinkBackURL.ToString()))
                {
                    _request.Parameters.Add("link_back_url", this.LinkBackURL.ToString());
                }

                // Category
                _request.Parameters.Add("category_id", this.CategoryId.ToString());

                //if (!string.IsNullOrEmpty(this.Category)) { _request.Parameters.Add("category", this.Category); }
                //if (!string.IsNullOrEmpty(this.Subcategory)) { _request.Parameters.Add("subcategory", this.Subcategory); }
              
                // Tags
                if (this.TagList.Count > 0)
                {
                    StringBuilder _tags = new StringBuilder();
                    foreach (string _tag in this.TagList)
                    {
                        // No blank Tags, thankyouverymuch!
                        if (!string.IsNullOrEmpty(_tag))
                            _tags.AppendFormat("{0},", _tag);
                    }
                    _request.Parameters.Add("tags", _tags.ToString().TrimEnd(','));
                }

                if (!string.IsNullOrEmpty(this.Author)) { _request.Parameters.Add("author", this.Author); }
                if (!string.IsNullOrEmpty(this.Publisher)) { _request.Parameters.Add("publisher", this.Publisher); }
                if (!this.WhenPublished.Equals(DateTime.MinValue)) { _request.Parameters.Add("when_published", string.Format("YYYY-MM-DD", this.WhenPublished)); }
                if (!string.IsNullOrEmpty(this.Edition)) { _request.Parameters.Add("edition", this.Edition); }
                _request.Parameters.Add("disable_upload_link", this.DisableUploadLink ? "1" : "0");
                _request.Parameters.Add("disable_print", this.DisablePrint ? "1" : "0");
                _request.Parameters.Add("disable_select_text", this.DisableSelectText ? "1" : "0");
                _request.Parameters.Add("disable_about_dialog", this.DisableAboutDialog ? "1" : "0"); ;
                _request.Parameters.Add("disable_info_dialog", this.DisableInfoDialog ? "1" : "0");
                _request.Parameters.Add("disable_view_mode_change", this.DisableViewModeChange ? "1" : "0");
                _request.Parameters.Add("disable_related_docs", this.DisableRelatedDocuments ? "1" : "0");

                if (this.StoreSettings != null)
                {
                    this.StoreSettings.PopulateParameters(_request.Parameters);
                }

                Response _response = Service.Instance.PostRequest(_request);
                if (_response != null && _response.HasChildNodes && _response.ErrorList.Count < 1)
                {
                    // Notify those who care.
                    OnSaved();
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Notify subscribers of intent to save.
 /// </summary>
 /// <param name="arguments"></param>
 internal void OnBeforeSave(DocumentEventArgs arguments)
 {
     if (BeforeSave != null)
     {
         BeforeSave(this, arguments);
     }
 }
Exemple #3
0
 /// <summary>
 /// Notify subscribers before download
 /// </summary>
 /// <param name="arguments">Document to download</param>
 internal static void OnBeforeDownload(DocumentEventArgs arguments)
 {
     if (BeforeDownload != null)
     {
         BeforeDownload(arguments.Document, arguments);
     }
 }
Exemple #4
0
        /// <summary>
        /// Downloads a document from Scribd.
        /// </summary>
        /// <param name="documentId">Identifier of the document</param>
        /// <returns><see cref="T:Scribd.Net.Document"/></returns>
        /// <example><![CDATA[
        ///		Scribd.Net.Document myDocument = Scribd.Net.Document.Download(39402);
        /// ]]></example>
        public static Document Download(int documentId)
        {
            Document _result = new Document();
            _result.DocumentId = documentId;

            // Give subscribers a chance to bail.
            DocumentEventArgs _arguments = new DocumentEventArgs(_result);
            OnBeforeDownload(_arguments);
            if (_arguments.Cancel)
            {
                return _result;
            }

            // Set up our request
            using (Request _request = new Request())
            {
                _request.MethodName = "docs.getSettings";
                _request.Parameters.Add("doc_id", documentId.ToString());

                // Grab our response
                using (Response _response = Service.Instance.PostRequest(_request))
                {

                    // Parse the response
                    if (_response != null && _response.HasChildNodes && _response.ErrorList.Count < 1)
                    {
                        XmlNode _node = _response.SelectSingleNode("rsp");

                        // Data
                        _result.DocumentId = int.Parse(_node.SelectSingleNode("doc_id").InnerText);
                        _result.Title = _node.SelectSingleNode("title").InnerText.Trim();
                        _result.Description = _node.SelectSingleNode("description").InnerText.Trim();
                        _result.AccessKey = _node.SelectSingleNode("access_key").InnerText;
                        _result.AccessType = _node.SelectSingleNode("access").InnerText == "private" ? AccessTypes.Private : AccessTypes.Public;

                        // Depends on version of the Scribd API ...
                        string _url = _node.SelectSingleNode("thumbnail_url") == null ? string.Empty : _node.SelectSingleNode("thumbnail_url").InnerText;
                        _result.ThumbnailUrl = new Uri(_url);

                        // Link to the large image
                        _result.LargeImageURL = new Uri(_url.Replace("thumb", "large"));

                        // License
                        switch (_node.SelectSingleNode("license").InnerText.ToLower())
                        {
                            case "by": _result.License = CCLicenseTypes.BY; break;
                            case "by-nc": _result.License = CCLicenseTypes.BY_NC; break;
                            case "by-nc-nd": _result.License = CCLicenseTypes.BY_NC_ND; break;
                            case "by-nc-sa": _result.License = CCLicenseTypes.BY_NC_SA; break;
                            case "by-nd": _result.License = CCLicenseTypes.BY_ND; break;
                            case "by-sa": _result.License = CCLicenseTypes.BY_SA; break;
                            case "c": _result.License = CCLicenseTypes.C; break;
                            case "pd": _result.License = CCLicenseTypes.PD; break;
                            default: _result.License = CCLicenseTypes.None_Specified; break;
                        }

                        // 2010.04.06 - JPD - This has been removed from the API.
                        // Show Ads
                        //switch (_node.SelectSingleNode("show_ads").InnerText.ToLower())
                        //{
                        //    case "default": _result.ShowAds = ShowAdsTypes.Default; break;
                        //    case "true": _result.ShowAds = ShowAdsTypes.True; break;
                        //    case "false": _result.ShowAds = ShowAdsTypes.False; break;
                        //    default: _result.ShowAds = ShowAdsTypes.Default; break;
                        //}

                        // Tags
                        if (_node.SelectSingleNode("tags") != null) // Doing this now in case they decide to dump "tags" too...grr
                        {
                            string _tags = _node.SelectSingleNode("tags").InnerText;
                            foreach (string _tag in _tags.Split(','))
                            {
                                _result.TagList.Add(_tag.Trim());
                            }
                        }

                        // Security
                        if (_node.SelectSingleNode("secret_password") != null)
                        {
                            _result.AccessType = AccessTypes.Private;
                            _result.SecretPassword = _node.SelectSingleNode("secret_password").InnerText;
                        }

                        if (_node.SelectSingleNode("link_back_url") != null)
                        {
                            _result.LinkBackURL = new Uri( _node.SelectSingleNode("link_back_url").InnerText);
                        }

                        if (_node.SelectSingleNode("page_count") != null)
                        {
                            //_result.PageCount = int.Parse(_node.SelectSingleNode("page_count").InnerText);
                            int pc;
                            int.TryParse(_node.SelectSingleNode("page_count").InnerText, out pc);
                            _result.PageCount = pc;
                        }

                        // Category
                        if (_node.SelectSingleNode("category_id") != null)
                        {
                            int cat;
                            int.TryParse(_node.SelectSingleNode("category_id").InnerText, out cat);
                            _result.CategoryId = cat;

                            // TODO:  Set Category property
                            
                        }

                        // Download Formats
                        if (_node.SelectSingleNode("download_formats") != null)
                        {
                            _result.DownloadFormats.AddRange(_node.SelectSingleNode("download_formats").InnerText.Split(','));
                        }


                        if (_node.SelectSingleNode("author") != null)
                        {
                            _result.Author = _node.SelectSingleNode("author").InnerText;
                        }

                        if (_node.SelectSingleNode("publisher") != null)
                        {
                            _result.Publisher = _node.SelectSingleNode("publisher").InnerText;
                        }

                        if (_node.SelectSingleNode("when_published") != null)
                        {
                            if (_node.SelectSingleNode("when_published").InnerText != "")
                            {
                                _result.WhenPublished = Convert.ToDateTime(_node.SelectSingleNode("when_published").InnerText);
                            }
                        }

                        if (_node.SelectSingleNode("edition") != null)
                        {
                            _result.Edition = _node.SelectSingleNode("edition").InnerText;  
                        }

                        _result.StoreSettings = new DocumentStore();

                        if (_node.SelectSingleNode("page_restriction_type") != null)
                        {
                            switch (_node.SelectSingleNode("page_restriction_type").InnerText.ToLower())
                            {
                                case "automatic": _result.StoreSettings.RestrictionType = PageRestrictionTypes.Automatic; break;
                                case "max_pages": _result.StoreSettings.RestrictionType = PageRestrictionTypes.MaxPages; break;
                                case "max_percentage": _result.StoreSettings.RestrictionType = PageRestrictionTypes.MaxPercentage; break;
                                case "page_range": _result.StoreSettings.RestrictionType = PageRestrictionTypes.PageRange; break;
                                default: _result.StoreSettings.RestrictionType = PageRestrictionTypes.Automatic; break;
                            }
                        }

                        if (_node.SelectSingleNode("max_pages") != null)
                        {
                            _result.StoreSettings.MaxPages = Convert.ToInt32(_node.SelectSingleNode("max_pages").InnerText);
                        }

                        if (_node.SelectSingleNode("max_percentage") != null)
                        {
                            _result.StoreSettings.MaxPercentage = Convert.ToInt32(_node.SelectSingleNode("max_percentage").InnerText);
                        }

                        if (_node.SelectSingleNode("page_range") != null)
                        {
                            _result.StoreSettings.PageRange =_node.SelectSingleNode("page_range").InnerText;
                        }

                        if (_node.SelectSingleNode("allow_search_targeting") != null)
                        {
                            _result.StoreSettings.AllowSearchTargeting = Convert.ToBoolean(_node.SelectSingleNode("allow_search_targeting").InnerText);
                        }

                        if (_node.SelectSingleNode("obfuscate_numbers") != null)
                        {
                            _result.StoreSettings.ObfuscateNumbers = Convert.ToBoolean(_node.SelectSingleNode("obfuscate_numbers").InnerText);
                        }

                        if (_node.SelectSingleNode("allow_search_indexing") != null)
                        {
                            _result.StoreSettings.AllowSearchIndexing = Convert.ToBoolean(_node.SelectSingleNode("allow_search_indexing").InnerText);
                        }

                        if (_node.SelectSingleNode("price") != null)
                        {
                            _result.StoreSettings.Price = (float)Convert.ToDouble(_node.SelectSingleNode("price").InnerText);
                        }

                        if (_node.SelectSingleNode("min_price") != null)
                        {
                            _result.StoreSettings.MinPrice = (float)Convert.ToDouble(_node.SelectSingleNode("min_price").InnerText);
                        }

                        if (_node.SelectSingleNode("max_price") != null)
                        {
                            _result.StoreSettings.MaxPrice = (float)Convert.ToDouble(_node.SelectSingleNode("max_price").InnerText);
                        }

                    }
                }
                // Notify subscribers
                OnDownloaded(_result);
            }
            // Get out!
            return _result;
        }