/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#include <sxpagerss.h>
#include <dditemset.h>
#include <dditemlink.h>
#include <ddwebconnection.h>


sxpagerss::sxpagerss( const ddstring& name, ddwebsite* site )
: sxpage( name, site )
{
/*
"rss_forum"
"rss_groups"
"rss_groupfaqs"
"rss_news"
"rss_faqs"
*/
	add_request_handler( "rss_forum",			"xx_rss_forum", 	(ddmemptr) &sxpage::on_overview );
	add_request_handler( "rss_groups",		"xx_rss_groups", 	(ddmemptr) &sxpage::on_overview );
	add_request_handler( "rss_groupfaqs",	"xx_rss_faqs", 		(ddmemptr) &sxpage::on_overview );
	add_request_handler( "rss_news",			"xx_rss_news", 		(ddmemptr) &sxpage::on_overview );
	add_request_handler( "rss_faqs",			"xx_rss_faqs",		(ddmemptr) &sxpage::on_overview );

}


/**
	Übersichtsdarstellung basteln.
*/

dd_result sxpagerss::on_overview( dditem& item )
{
	
	ddwebview& table = get_view();
	const ddstringrow& sp = get_split_path();
	
	const ddstring& request = sp[0];
 	ddstring group;
 	int xcount = 5;
	
 	// fallunterscheidung: gibts eine GruppenID bzw. einen count ?
 	if( sp.size() > 1 )
 	{
 		if( request.contains( "groups" ) )
 		{
 			group = sp[1];
 			if( sp.size() > 2 )
				xcount = sp[2].as_int();
 		}
 		else
 		{
 			xcount = sp[1].as_int();
 		}
 	}
	
	// ...daten holen, der zweite parameter steuert die gruppe, so vorhanden
	context.numrows = table.fill( request, group );
	if( context.numrows <= 0 )
		return rs_error;
	
	if( xcount > context.numrows )
		xcount = context.numrows;
	
	prepare_table();
	
	item["sn_date"].from_string( table[0]["sn_date"] );
	
	// ...anzeigen...
	table.on_render( *this, item["channel"], 0, xcount );	
	
	context.contenttype = "text/xml; charset=iso-8859-1";//application/atom+xml";
	
	
	// ...und gut
	return rs_ok;
	(void) item;

}


void sxpagerss::prepare_table()
{  
	ddwebview& table = get_view();
	// PFUSCH!
  for( int i = 0; i<context.numrows; ++i )
  {
  	table[i]["sn_msgbody"].trim_text( 180 );
  	prepare_entry( table[i]["sn_msgbody"] );
   	prepare_entry( table[i]["sn_msgsubject"] );
   	table[i]["sn_block_article"] = table[i]["sn_msgbody"];
  }
}


void sxpagerss::prepare_entry( ddstring& entry )
{
  entry.replace_all( "&amp;", "&" );
  entry.replace_all( "&quot;", "\"" );
  entry.replace_all( "&lt;", "<" );
  entry.replace_all( "&gt;", ">" );
	
	ddstring result;
	for( size_t i = 0; i<entry.size(); ++i )
  {
  	switch( (unsigned char) entry[i] )
		{
			// euro
			case 128:
				
				result += " Euro";
				continue;
			
			// single '
			case 130: case 145: case 146:
				
				result += '\'';
				continue;
			
			// double "
			case 132: case 147: case 148:
				
				result += '"';
				continue;
			
			// dot
			case 149:
				
				result += '*';
				continue;
			
			// tilde
			case 152:
				
				result += '~';
				continue;
			
			// dash
			case 150: case 151:
				
				result += '-';
				continue;
			
			default:
					
				result += entry[i];
			
		}
 	}
 	
 	entry = result;
  
}







