/* 
 * Infrastructure which supports events generated by triggers.
 * Copyright (C) 2009 Petr Kubanek <petr@kubanek.net>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifndef __RTS2_EVENTS__
#define __RTS2_EVENTS__

#include "messageevents.h"
#include "stateevents.h"
#include "valueevents.h"
#include "bbserver.h"

#include <xmlerror.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <ostream>
#include <sstream>

namespace rts2xmlrpc
{

/**
 * Directory mapping.
 *
 * @author Petr Kubanek <petr@kubanek.net>
 */
class DirectoryMapping
{
	public:
		DirectoryMapping (const char *_path, const char *_to) { path = std::string (_path); to = std::string (_to); }

		const char *getPath () { return path.c_str (); }
		const char *getTo () { return to.c_str (); }

	private:
		std::string path;
		std::string to;
};

/**
 * Holder for events which can occur on devices.
 *
 * @author Petr Kubanek <petr@kubanek.net>
 */
class Events
{
	public:
		StateCommands stateCommands;
		ValueCommands valueCommands;
		MessageCommands messageCommands;

		BBServers bbServers;

		std::vector <std::string> publicPaths;
		std::vector <std::string> allskyPaths;

		// directory which can override default files
		std::string docroot;
		int defchan;

		// directories mapping
		std::vector <DirectoryMapping> dirs;

		Events (HttpD *_master);

		/**
		 * Load a list of StateChangeCommand from file.
		 *
		 * @param file Name of file holding the list.
		 *
		 * @throw XmlError
		 */
		void load (const char *file);

		/**
		 * Return true if given path is public.
		 */
		bool isPublic (std::string path);

		const char *getDefaultImageLabel () { return defImageLabel; }

	private:
		HttpD *master;

		char *defImageLabel;

		void parseHttp (xmlNodePtr ev);
		void parseEvents (xmlNodePtr ev);
		void parseBB (xmlNodePtr ev);

		void parseState (xmlNodePtr event, std::string deviceName);
		void parseValue (xmlNodePtr event, std::string deviceName);
		void parseMessage (xmlNodePtr event, std::string deviceName);
};

}

#endif /* !__RTS2_EVENTS__ */
