WebLog.cs :  » RSS-RDF » RSS-Feeder » RSSBlogAPI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » RSS RDF » RSS Feeder 
RSS Feeder » RSSBlogAPI » WebLog.cs
// Copyright  2005 by Omar Al Zabir. All rights are reserved.
// 
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
// 
// website http://www.oazabir.com, email OmarAlZabir@gmail.com, msn oazabir@hotmail.com
using System;
using System.Collections;
using System.Drawing;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Drawing.Design;

namespace RSSBlogAPI{
  using RSSCommon.PropertyEditor;

  public enum EngineEnum
  {
    DotText,
    Wordpress,
    B2Evolution,
    CommunityServer,
    Drupal,
    CommunityServerMetaBlogAPI
  }
    
  [Serializable][XmlRoot("weblog")]
  public class WebLog
  {
    #region Events

    ///<summary>A PropertyChanged event is raised when a property is changed on a component. A PropertyChangedEventArgs object specifies the name of the property that changed.</summary>
    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Private Fields

    private string _BlogId = string.Empty;
    private string _UserName = string.Empty;
    private string _Password = string.Empty;
    private string _ServiceUrl = string.Empty;
    private string _Server = string.Empty;
    private bool _IsValidated = false;
    private EngineEnum _Engine = EngineEnum.DotText;
    private Category [] _Categories;
    private string _OutlookFolder;
    private bool _SendPasswordHashed = false;
    private ArrayList _Posts = new ArrayList();
    private string _Title = "New Weblog";

    #endregion

    #region Constructors

    public WebLog()
      : this( string.Empty, string.Empty, string.Empty, string.Empty, false, EngineEnum.DotText)
    {
    }

    public WebLog( string blogId, string userName, string password, string serviceUrl, bool validated, EngineEnum engine )
    {
      _BlogId = blogId;
      _UserName = userName;
      _Password = password;
      _ServiceUrl = serviceUrl;
      _IsValidated = validated;
      _Engine = engine;
    }

    #endregion

    #region Public Properties
    
    [Category("Post"), Browsable(false), 
    Description(@"Pending posts in queue")]
    [XmlArray("posts"), XmlArrayItem("post", typeof(Post)) ]
    public ArrayList Posts
    {
      get { return _Posts;  }  
      set 
      {
        bool changed = !object.Equals(this._Posts, value);
        this._Posts = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Posts));
      }
    }

    [Category("Outlook"), Browsable(true), 
    Description(@"Outlook pickup folder. Items posted in this folder will be posted to weblog")]
    [XmlElement("outlookFolder")]
    [Editor(typeof(UIOutlookFolderPickerEditor), typeof(UITypeEditor))]
    public string OutlookFolder
    {
      get { return _OutlookFolder;  }  
      set 
      {
        bool changed = !object.Equals(this._OutlookFolder, value);
        this._OutlookFolder = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.OutlookFolder));
      }
    }

    [Category("Post"), Browsable(true), ReadOnly(true),
    Description(@"Categories retrieved from the server")]
    [XmlElement("categories")]
    public Category[] Categories
    {
      get {
 
        if( null == _Categories ) _Categories= new Category[0];
        return _Categories; 
      }  
      set 
      {
        //bool changed = !object.Equals(this._Categories, value);
        this._Categories = value;
        //if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Categories));
      }
    }

    [Category("Blog"), DefaultValue(""), Browsable(true),
    Description(@"Your blog ID. For example, if your blog URL is www.blogger.com/myblog then the id will be 'myblog'. If your blog server is B2Evolution, then the blog ID will be a number.")]
    [XmlElement("blogId")]
    public string BlogId
    {
      get { return _BlogId;  }  
      set 
      {
        bool changed = !object.Equals(this._BlogId, value);
        this._BlogId = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.BlogId));
      }
    }

    [Category("Credentials"), DefaultValue(false), Browsable(true),
    Description(@"If you are having login problem, set it to true or false. Some blog servers require this to be set true and some may not require")]
    [XmlElement("sendPasswordHashed")]
    public bool SendPasswordHashed
    {
      get { return _SendPasswordHashed;  }  
      set
      {
        bool changed = !object.Equals(this._SendPasswordHashed, value);
        this._SendPasswordHashed = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.SendPasswordHashed));
      }
    }

    [Category("Account"), DefaultValue("New Weblog"), Browsable(true),
    Description(@"Title of this weblog which identifies it in the list")]
    [XmlElement("title")]
    public string Title
    {
      get { return _Title;  }  
      set
      {
        bool changed = !object.Equals(this._Title, value);
        this._Title = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Title));
      }
    }

    [Category("Credentials"), DefaultValue(""), Browsable(true),
    Description(@"User account name. eg. myusername")]
    [XmlElement("userName")]
    public string UserName
    {
      get { return _UserName;  }  
      set
      {
        bool changed = !object.Equals(this._UserName, value);
        this._UserName = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.UserName));
      }
    }

    [Category("Credentials"), DefaultValue(""), Browsable(true), 
    Description(@"DO NOT TYPE, CLICK THE '...' BUTTON")]
    [XmlElement("password")]
    [Editor(typeof(PasswordEditor), typeof(UITypeEditor))]
    public string Password
    {
      get { return _Password;  }  
      set
      {
        bool changed = !object.Equals(this._Password, value);
        if( this.ValidPassword( value ) )
        {
          this._Password = value;          
        }
        else
        {
          this._Password = "Don't Type. Click '...'";
        }
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Password));
      }
    }

    [Category("Server"), DefaultValue(""), Browsable(true), ReadOnly(true),
    Description(@"URL to the blog service webservice/page which takes input. eg. http://www.YOURSERVER.com/YOURBLOGNAME/Services/SimpleBlogService.asmx")]
    [XmlElement("serviceUrl")]
    public string ServiceUrl
    {
      get { return _ServiceUrl;  }  
      set
      {
        bool changed = !object.Equals(this._ServiceUrl, value);
        this._ServiceUrl = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.ServiceUrl));
      }
    }

    [Category("Server"), DefaultValue(""), Browsable(true),
    Description(@"Server name. eg. http://www.blogger.com")]
    [XmlElement("server")]
    public string Server
    {
      get { return _Server;  }  
      set
      {
        bool changed = !object.Equals(this._Server, value);
        this._Server = value;

        if( !this._Server.StartsWith("http://") )
          this._Server = "http://" + this._Server;
        if( !this._Server.EndsWith("/") )
          this._Server += "/";

        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Server));
      }
    }

    [Category("Server"), Browsable(true),
    Description(@"The Type of engine your blog provider is using")]
    [XmlElement("engine")]
    public EngineEnum Engine
    {
      get { return _Engine;  }  
      set
      {
        bool changed = !object.Equals(this._Engine, value);
        this._Engine = value;
        if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Engine));
      }
    }

    #endregion

    #region Private Methods

    ///<summary>A PropertyChanged event is raised when a property is 
    ///changed on a component. A PropertyChangedEventArgs object 
    ///specifies the name of the property that changed.</summary>
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {  
      if( e.PropertyName != Properties.ServiceUrl )
        this.MakeUrl();
      this.CheckProperties();
      if( null != PropertyChanged )
      {
        PropertyChanged(this, e);      
      }
    }

    private bool ValidPassword( string password )
    {
      // No password
      if( null == password || 0 == password.Length ) return true;

      // Password decryption provider is still not available
      if( null == RSSCommon.PropertyEditor.PasswordEditor.PasswordProvider ) return true;
      try
      {
        // If password decryption fails, surely user has manually typed the password
        // inside the property dialog.
        string decryptedPassword = 
          RSSCommon.PropertyEditor.PasswordEditor.PasswordProvider.Decrypt( password );
        return ( null != decryptedPassword && decryptedPassword.Length > 0);
      }
      catch
      {
        return false;
      }
    }

    private void CheckProperties()
    {
      if( EngineEnum.B2Evolution == this._Engine )
      {
        if( this._BlogId[0] < '0' || this._BlogId[0] > '9' )
        {
          this.BlogId = "1";
        }
      }
    }

    private void MakeUrl()
    {
      if( EngineEnum.DotText == this._Engine )
      {
        this.ServiceUrl = this.Server + this.BlogId + 
          "/services/SimpleBlogService.asmx";
      }
      else if( EngineEnum.Wordpress == this._Engine
        || EngineEnum.Drupal == this._Engine )
      {
        this.ServiceUrl = this.Server + "xmlrpc.php";
      }
      else if( EngineEnum.B2Evolution == this._Engine )
      {
        this.ServiceUrl = this.Server + "xmlsrv/xmlrpc.php";
      }
      else if( EngineEnum.CommunityServer == this._Engine )        
      {
        this.ServiceUrl = this.Server + "blogservice.asmx";
      }
      else if( EngineEnum.CommunityServerMetaBlogAPI == this._Engine )
      {
        this.ServiceUrl = this.Server + "metablog.ashx";
      }
    }
    #endregion
    
    #region Property Definition

    public class Properties
    {
      public const string Title = "Title";
      public const string Posts = "Posts";
      public const string SendPasswordHashed = "SendPasswordHashed";
      public const string OutlookFolder = "OutlookFolder";
      public const string Categories = "Categories";
      public const string BlogId = "BlogId";
      public const string UserName = "UserName";
      public const string Password = "Password";
      public const string ServiceUrl = "ServiceUrl";
      public const string IsValidated = "IsValidated";
      public const string Engine = "Engine";
      public const string Server = "Server";
    }

    #endregion

    #region Public Methods

    public void EnqueuePost( Post post ) 
    {
      this.Posts.Insert( 0, post );
      OnPropertyChanged(new PropertyChangedEventArgs(Properties.Posts));
    }

    public Category [] GetCategories( string categories )
    {
      // Build categories
      string [] categoryNames = categories.Trim().Split(',');
      ArrayList postCategories = new ArrayList();
      if( null != categoryNames )
      {
        foreach( string categoryName in categoryNames )
        {
          // Check if this category matches with the weblog's category list
          foreach( Category cat in this._Categories )
            if( cat.Title.ToLower() == categoryName.Trim().ToLower() )
              postCategories.Add( cat );
        }
      }
      // If we have found any valid match for category then use one
      if( postCategories.Count > 0 )
      {
        Category [] categoryArray = new Category[ postCategories.Count ];
        postCategories.CopyTo( categoryArray, 0 );

        return categoryArray;
      }
      else
      {
        // none found, use the first one as default
        if( this._Categories.Length > 0 )
        {
          Category [] categoryArray = new Category[ 1 ];
          categoryArray[ 0 ] = this._Categories[ 0 ];

          return categoryArray;
        }
        else
        {
          Category [] dummy = new Category[ 1 ];
          dummy[0] = new Category( 0, "1", "Announcement" );

          return dummy;
        }
      }
            
    }

    public override string ToString()
    {
      return this.UserName + " at " + this.Server + " ID: " + this.BlogId;
    }


    #endregion
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.