|| applet sadman_flickr_rss.curl
|| maintained by: sadman

|| Simple RSS viewer using sadXMLHttpRequest


|| Distributed under the New BSD License:
|#
  Copyright (c) 2006, Steve Adams
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, are permitted 
  provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions 
      and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
      and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of curlr nor the names of its contributors may be used to endorse or promote products 
      derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY 
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#|
{curl 5.0 applet}
{curl-file-attributes character-encoding = "utf8"}
{applet
    manifest = "manifest.mcurl",
    author = "Steve Adams",
    {compiler-directives careful? = true}
}

{import * from COM.CURLR.XML}

|| to keep the xml handling code code uncluttered, this class handles the Flickr RSS display
{include "sadSimpleFlickrRSSViewer.scurl"}

{value
    || Step 1. Create a sadXMLHttpRequest object
    let xmlhttp:sadXMLHttpRequest = {new sadXMLHttpRequest}

    || Step 2. Create an event handler that will issue a request 
    || Use the simple viewer class
    let rss-viewer:sadSimpleFlickrRSSViewer =
        {sadSimpleFlickrRSSViewer
            || event handler for a change in the group selection
            {on Commit do
                || start the request for the selected group Url
                {xmlhttp.open HttpRequestMethod.get, rss-viewer.current-feed-url, async? = true}
            }
        }
    
    || Step 3. Create a procedure to be called when the state of the sadXMLHttpRequest object changes.
    set xmlhttp.onreadystatechange =
        {proc {}:void
            {switch xmlhttp.readyState
             case readyStateEnum.Open do
                || we could make the send call immediately after the open call,
                || but this shows the handling of the state change
                {xmlhttp.send}
             case readyStateEnum.Loaded do
                || get the response headers for reporting
                let hdrs:String = 
                    {non-null {xmlhttp.getAllResponseHeaders}}
                
                || consider anything other than 200 is an error
                {if xmlhttp.status == 200 then
                    || get the XML response and update the display
                    {if-non-null feed-xml = xmlhttp.responseXML then
                        {rss-viewer.display-feed feed-xml}
                        {return}
                    }
                }
                || show headers if there's a problem
                {rss-viewer.display-text hdrs}
            }            
        }
    
    || show the viewer
    rss-viewer
}
