// ==========================================================================
// METHOD ItemIconRequestHandler::run
// ==========================================================================
int ItemIconRequestHandler::run (string &uri, string &postbody, value &inhdr,
								 string &out, value &outhdr, value &env,
								 tcpsocket &s)
{
	string uuid = uri.copyafterlast ("/");
	uuid.cropat ('.');
	
	app->log (log::debug, "httpicon", "Request for <%s>" %format (uuid));
	
	if (! app->mdb->classExistsUUID (uuid))
	{
		string orgpath;
		
		orgpath = "/var/openpanel/http/images/icons/%s_item.png" %format (uuid);

		if (fs.exists (orgpath))
		{
			out = fs.load (orgpath);
			outhdr["Content-type"] = "image/png";
			return 200;
		}
		return 404;
	}
	
	CoreClass &c = app->mdb->getClassUUID (uuid);
	string path = "%s/item_%s" %format (c.module.path, c.icon);
	app->log (log::debug, "itemicon", "Loading %s" %format (path));
	
	if (! fs.exists (path)) return 404;
	
	outhdr["Content-type"] = "image/png";
	out = fs.load (path);
	return 200;
}
// ==========================================================================
// METHOD IconRequestHandler::run
// ==========================================================================
int IconRequestHandler::run (string &uri, string &postbody, value &inhdr,
							 string &out, value &outhdr, value &env,
							 tcpsocket &s)
{
	bool isdown = (uri.strstr ("/down/") >= 0);
	string uuid = uri.copyafterlast ("/");
	uuid.cropat ('.');
	
	app->log (log::debug, "httpicon", "Request for <%s>" %format (uuid));
	
	if (inhdr.exists ("If-Modified-Since"))
	{
		s.puts ("HTTP/1.1 304 NOT CHANGED\r\n"
				"Connection: %s\r\n"
				"Content-length: 0\r\n\r\n"
				%format (env["keepalive"].bval() ? "keep-alive" : "close"));
		
		env["sentbytes"] = 0;
		return -304;
	}
	
	if (! app->mdb->classExistsUUID (uuid))
	{
		string orgpath;
		
		if (isdown)
		{
			orgpath = "/var/openpanel/http/images/icons/down_%s.png" %format (uuid);
		}
		else
		{
			orgpath = "/var/openpanel/http/images/icons/%s.png" %format (uuid);
		}
		if (fs.exists (orgpath))
		{
			out = fs.load (orgpath);
			outhdr["Content-type"] = "image/png";
			return 200;
		}
		return 404;
	}
	CoreClass &c = app->mdb->getClassUUID (uuid);
	string path;
	if (isdown)
	{
		path = "%s/down_%s" %format (c.module.path, c.icon);
	}
	else
	{
		path = "%s/%s" %format (c.module.path, c.icon);
	}
	
	app->log (log::debug, "httpicon", "Loading %s" %format (path));
	
	if (! fs.exists (path)) return 404;
	
	outhdr["Content-type"] = "image/png";
	out = fs.load (path);
	return 200;
}
// ==========================================================================
// METHOD ImagePreloader::run
// ==========================================================================
int ImagePreloader::run (string &uri, string &postbody, value &inhdr,
						 string &out, value &outhdr, value &env,
						 tcpsocket &s)
{
	string fname = uri.copyafterlast ("/");
	if (fname == "imagelist.js")
	{
		out = "function preloadImages () {\n"
			  "preloadedGUIImages = new Array();\n";
		value dir = fs.dir ("/var/openpanel/http/images/gui");
		foreach (img, dir)
		{
			string ext = img.sval().copyafterlast('.');
			if ((ext != "png")&&(ext!="jpg")&&(ext!="gif")) continue;
			
			out += "preloadedGUIImages[\"%{0}s\"] = new Image(32,32);\n"
				   "preloadedGUIImages[\"%{0}s\"].src = \"/images/gui/%{0}s\";\n"
				   %format (img.id());
		}
// ==========================================================================
// METHOD EmblemRequestHandler::run
// ==========================================================================
int EmblemRequestHandler::run (string &uri, string &postbody, value &inhdr,
							   string &out, value &outhdr, value &env,
							   tcpsocket &s)
{
	string uuid = uri.copyafterlast ("/");
	uuid.cropat ('.');
	
	if (! app->mdb->classExistsUUID (uuid))
	{
		out = "%s not found" %format (uuid);
		return 404;
	}
	
	CoreClass &c = app->mdb->getClassUUID (uuid);
	string path = "%s/large_%s" %format (c.module.path, c.icon);
	if (! fs.exists (path)) return 404;
	
	outhdr["Content-type"] = "image/png";
	out = fs.load (path);
	return 200;
}
// ==========================================================================
// METHOD WallpaperHandler::run
// ==========================================================================
int WallpaperHandler::run (string &uri, string &postbody, value &inhdr,
						   string &out, value &outhdr, value &env,
						   tcpsocket &s)
{
	outhdr["Content-type"] = "image/jpeg";
	
	if (uri == "/dynamic/wallpaper.jpg")
	{
		string p = WallpaperClass::getCurrentWallpaper();
		log::write (log::info, "WallP", "Serving %s" %format (p));
		if (fs.exists (p)) out = fs.load (p);
		return 200;
	}
	else
	{
		string fn = uri.copyafterlast ("/");
		string path = "/var/openpanel/wallpaper/%s.preview" %format (fn);
		if (fs.exists (path)) out = fs.load (path);
		return 200;
	}
}