using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; using System.Net; using System.IO; namespace RSSViewer { public partial class frmAddFeed : Form { enum templateId { tWordpress = 0, tBlogspot = 1, tLivejournal = 2, tFeedburner = 3, tOther = 4 }; string[] arrTemplateName = { "Wordpress", "Blogspot", "Livejournal", "Feedburner", "Other" }; enum xmlTypeId { xtAtom = 0, xtRSS = 1 }; string[] arrXmlTypeName = { "Atom", "RSS" }; private templateId iCurrentTemplate; public frmAddFeed() { InitializeComponent(); for ( int i = 0; i < arrTemplateName.Length; i++ ) { cmbTemplate.Items.Add( arrTemplateName[i] ); } for ( int i = 0; i < arrXmlTypeName.Length; i++ ) { cmbFeedType.Items.Add( arrXmlTypeName[i] ); } iCurrentTemplate = templateId.tWordpress; cmbTemplate.SelectedIndex = (int)iCurrentTemplate; updateVisuals(); } public string getShortName() { return Guid.NewGuid().ToString(); } public string getLongName() { return edtTitle.Text; } public string getUrl() { return edtUrl.Text; } public int getXmlType() { return cmbFeedType.SelectedIndex; } private void updateUrl() { xmlTypeId feedtype = (xmlTypeId)cmbFeedType.SelectedIndex; switch ( iCurrentTemplate ) { case templateId.tWordpress: edtUrl.Text = "http://" + edtUsername.Text + ".wordpress.com/feed"; break; case templateId.tBlogspot: edtUrl.Text = "http://" + edtUsername.Text + ".blogspot.com/feeds/posts/default"; break; case templateId.tLivejournal: if ( feedtype == xmlTypeId.xtAtom ) { edtUrl.Text = "http://" + edtUsername.Text + ".livejournal.com/data/atom"; } else if ( feedtype == xmlTypeId.xtRSS ) { edtUrl.Text = "http://" + edtUsername.Text + ".livejournal.com/data/rss"; } break; case templateId.tFeedburner: edtUrl.Text = "http://feeds.feedburner.com/" + edtUsername.Text; break; case templateId.tOther: break; } } private void updateVisuals() { switch ( iCurrentTemplate ) { case templateId.tWordpress: cmbFeedType.SelectedIndex = (int)xmlTypeId.xtRSS; cmbFeedType.Enabled = false; edtUrl.Enabled = false; edtUsername.Visible = true; lblUsername.Visible = true; break; case templateId.tBlogspot: cmbFeedType.SelectedIndex = (int)xmlTypeId.xtAtom; cmbFeedType.Enabled = false; edtUrl.Enabled = false; edtUsername.Visible = true; lblUsername.Visible = true; break; case templateId.tLivejournal: //cmbFeedType.SelectedIndex = (int)xmlTypeId.xtRSS; cmbFeedType.Enabled = true; edtUrl.Enabled = false; edtUsername.Visible = true; lblUsername.Visible = true; break; case templateId.tFeedburner: cmbFeedType.SelectedIndex = (int)xmlTypeId.xtRSS; cmbFeedType.Enabled = false; edtUrl.Enabled = false; edtUsername.Visible = true; lblUsername.Visible = true; break; case templateId.tOther: cmbFeedType.Enabled = true; edtUrl.Enabled = true; edtUsername.Visible = false; lblUsername.Visible = false; break; } updateUrl(); } private void cmbTemplate_SelectedIndexChanged( object sender, EventArgs e ) { iCurrentTemplate = (templateId)cmbTemplate.SelectedIndex; updateVisuals(); } private void menuItem2_Click( object sender, EventArgs e ) { this.DialogResult = DialogResult.Cancel; } private XmlNode digAndFindNodeByName( XmlNode parent, string name ) { int c = parent.ChildNodes.Count; for ( int i = 0; i < c; i++ ) { XmlNode node = parent.ChildNodes[i]; if ( node.Name.Equals( name ) ) { return node; } else { node = digAndFindNodeByName( node, name ); if ( node != null ) { return node; } } } return null; } private bool saveAndTest() { bool bIsOk = false; Cursor.Current = Cursors.WaitCursor; try { string content = downloadFile( edtUrl.Text ); if ( edtTitle.Text.Length == 0 ) { XmlDocument doc = new XmlDocument(); doc.LoadXml( content ); XmlNode node = digAndFindNodeByName( doc, "title" ); if ( node != null ) { edtTitle.Text = node.InnerText; } } bIsOk = true; } catch ( Exception ) { } Cursor.Current = Cursors.Default; return bIsOk; } private void menuItem1_Click( object sender, EventArgs e ) { DialogResult = DialogResult.OK; } private void edtUsername_TextChanged( object sender, EventArgs e ) { updateUrl(); } private void cmbFeedType_SelectedIndexChanged( object sender, EventArgs e ) { updateUrl(); } private string downloadFile( string url ) { HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create( url ); loHttp.Timeout = 10000; // 10 secs loHttp.UserAgent = "RSSViewer"; HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse(); Encoding enc = Encoding.GetEncoding( 65001 ); // UTF8? StreamReader loResponseStream = new StreamReader( loWebResponse.GetResponseStream(), enc ); string lcHtml = loResponseStream.ReadToEnd(); loWebResponse.Close(); loResponseStream.Close(); return lcHtml; } private void frmAddFeed_Closing( object sender, CancelEventArgs e ) { if ( DialogResult != DialogResult.Cancel ) { if ( saveAndTest() ) { e.Cancel = false; } else { MessageBox.Show( "Connection failed" ); e.Cancel = true; } } } } }