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

    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.

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


#ifndef DDCOOKIE_H
#define DDCOOKIE_H

#include <ddstringrow.h>
#include <ddiobuffer.h>


/// ddwebcookie : a simple class to handle cookies. 
/**
	@see www.netscape.com/newsref/std/cookie_spec.html
*/

class ddwebcookie : public ddstringrow
{

public: 
	
	enum dd_cookie_fields
	{
		cookie_name=0,
		cookie_value=1,
    cookie_comment=3,
		cookie_expires=5,
		cookie_path=7,
 		cookie_site=9,
    cookie_secure=11,
    cookie_version=12,
    cookie_fieldsize=14
	};
	
	ddwebcookie();
	ddwebcookie( const ddstring& name, const ddstring& value );
	virtual ~ddwebcookie();
	
	void      set_cookie_data( const ddstring& name, const ddstring& value, int days=0, const ddstring& domain="", const ddstring& path="/" );	
	void		  set_expires( size_t days );
	
	void 			flush_data( ddiobuffer& buffer );
	
	void      dump() const;		

  /**
  	@return the "name" field.
  */

  inline ddstring& get_name()
  {
  	return (*this)[cookie_name];
  }


  /**
  	@return the "value" field.
  */

  inline ddstring& get_value()
  {
  	return (*this)[cookie_value];
  }
  

  /**
  	@return true when name and value are not empty
  	@see get_name
  	@see get_value
  */

  inline bool is_valid()
  {
  	return ( !get_name().empty() && !get_value().empty() );
  }

  /**
  	Clear this cookie.
  */
	
  inline void invalidate_data()
  {
  	get_name() = "";
		get_value() = "";
  }

  
  /**
  	@return the "comment" field.
  */

  inline ddstring& get_comment()
  {
  	return (*this)[cookie_comment];
  }

  
  /**
  	@return the "site" field.
  */

  inline ddstring& get_site()
  {
  	return (*this)[cookie_site];
  }

  
  /**
  	@return the "path" field.
  */

  inline ddstring& get_path()
  {
  	return (*this)[cookie_path];
  }


  /**
  	@return the "expires" field.
  */

  inline ddstring& get_expires()
  {
  	return (*this)[cookie_expires];
  }

  
  /**
  	@return the "secure" field.
  */

  inline ddstring& get_secure()
  {
  	return (*this)[cookie_secure];
  }


  /**
  	@return the "version" field.
  */


  inline ddstring& get_version()
  {
  	return (*this)[cookie_version];
  }


  /**
  	@return join()
    @see join()
  */

  inline ddstring to_string() const
  {
  	return join();
  }

	
protected:

	static const char*  dd_cookie_data[];		
				
};

#endif
