// The contents of this file are subject to the Mozilla Public License Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
// WARRANTY OF ANY KIND, either express or implied. See the License for the specific
// language governing rights and limitations under the License.
// 
// The Original Code is the Venice Web Communities System.
// 
// The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(Packages.com.silverwrist.util);
importClass(Packages.com.silverwrist.dynamo.Namespaces);
importPackage(Packages.com.silverwrist.dynamo.except);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.security);
importPackage(Packages.com.silverwrist.dynamo.util);
importClass(Packages.com.silverwrist.venice.VeniceNamespaces);
importPackage(Packages.com.silverwrist.venice.content);
importPackage(Packages.com.silverwrist.venice.frame);

req = bsf.lookupBean("request");
req_help = bsf.lookupBean("request_help");
user = vlib.getUser(req);
vlib.setOnError(req,"sysadmin/main.js.vs");
vlib.setOnErrorType(req,"SERVLET");

// Make sure we are permitted to be here.
srm = cast.querySecurityReferenceMonitor(req_help.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"srm"));
acl = srm.getGlobalAcl();
if (!(   acl.testPermission(user,VeniceNamespaces.FRAME_LAF_NAMESPACE,"set.property")
      && acl.testPermission(user,VeniceNamespaces.FRAME_LAF_NAMESPACE,"set.block")))
  dynamo.scriptReturn(vlib.stdErrorBox(req,"Security Error",
                                       "You are not permitted to modify frame look-and-feel settings."));

// Get the global properties store.
globals = vcast.getGlobalPropertiesStore(req);
blocks = vcast.getGlobalBlocksStore(req);

// Create the dialog.
loader = cast.queryDialogLoader(req);
dlg = loader.loadDialogResource("sysadmin/frame_laf.dlg.xml");

my_ns = VeniceNamespaces.FRAME_LAF_NAMESPACE;  // shortcut

rc = null;
if (req_help.isVerb("GET"))
{ // Load the dialog with its initial values.
  optset = cast.toOptionSet(globals.getObject(my_ns,"optionset"));

  dlg.setValue("sitetitle",globals.getObject(my_ns,"site.title"));
  dlg.setValue("siteurl",globals.getObject(my_ns,"site.url"));
  dlg.setValue("template",globals.getObject(my_ns,"frame.template"));
  dlg.setValue("fptitle",globals.getObject(my_ns,"frontpage.title"));

  dlg.setValue("lbarwidth",globals.getObject(my_ns,"left.bar.width"));
  dlg.setValue("comments",cast.booleanObject(optset.get(0)));
  dlg.setValue("smarttags",cast.booleanObject(optset.get(1)));
  
  dlg.setValue("logourl",globals.getObject(my_ns,"site.logo.url"));
  dlg.setValue("logourltype",globals.getObject(my_ns,"site.logo.url.type"));
  dlg.setValue("logowidth",globals.getObject(my_ns,"site.logo.width"));
  dlg.setValue("logoheight",globals.getObject(my_ns,"site.logo.height"));

  dlg.setValue("footer",blocks.getObject(my_ns,"footer"));
  dlg.setValue("fscale",globals.getObject(my_ns,"footer.logo.scale"));
  dlg.setValue("fbreak",cast.booleanObject(optset.get(2)));

  dlg.setValue("pgi_url",globals.getObject(my_ns,"page.icon.url"));
  dlg.setValue("pgi_urltype",globals.getObject(my_ns,"page.icon.url.type"));
  dlg.setValue("pgi_mime",globals.getObject(my_ns,"page.icon.type"));

  dlg.setValue("fav_url",globals.getObject(my_ns,"page.favicon.url"));
  dlg.setValue("fav_urltype",globals.getObject(my_ns,"page.favicon.url.type"));

} // end if
else
{ // Load information from the dialog
  op = dlg.getClickedButton(req) + "";
  if (op=="cancel")  // user cancelled - bounce back to the previous menu
    rc = new Redirect("SERVLET","sysadmin/main.js.vs");
  else
  { // load and validate the dialog
    dlg.load(req);
    try
    { // validate the dialog
      dlg.validate(req);

      // set the appropriate values into the system properties
      optset = cast.toOptionSet(globals.getObject(my_ns,"optionset"));

      globals.setObject(user,my_ns,"site.title",dlg.getValue("sitetitle"));
      globals.setObject(user,my_ns,"site.url",dlg.getValue("siteurl"));
      globals.setObject(user,my_ns,"frame.template",dlg.getValue("template"));
      globals.setObject(user,my_ns,"frontpage.title",dlg.getValue("fptitle"));

      globals.setObject(user,my_ns,"left.bar.width",dlg.getValue("lbarwidth"));
      optset.set(0,cast.toBoolean(dlg.getValue("comments")));
      optset.set(1,cast.toBoolean(dlg.getValue("smarttags")));

      globals.setObject(user,my_ns,"site.logo.url",dlg.getValue("logourl"));
      globals.setObject(user,my_ns,"site.logo.url.type",dlg.getValue("logourltype"));
      globals.setObject(user,my_ns,"site.logo.width",dlg.getValue("logowidth"));
      globals.setObject(user,my_ns,"site.logo.height",dlg.getValue("logoheight"));

      blocks.setObject(user,my_ns,"footer",dlg.getValue("footer"));
      globals.setObject(user,my_ns,"footer.logo.scale",dlg.getValue("fscale"));
      optset.set(2,cast.toBoolean(dlg.getValue("fbreak")));

      globals.setObject(user,my_ns,"page.icon.url",dlg.getValue("pgi_url"));
      globals.setObject(user,my_ns,"page.icon.url.type",dlg.getValue("pgi_urltype"));
      globals.setObject(user,my_ns,"page.icon.type",dlg.getValue("pgi_mime"));

      globals.setObject(user,my_ns,"page.favicon.url",dlg.getValue("fav_url"));
      globals.setObject(user,my_ns,"page.favicon.url.type",dlg.getValue("fav_urltype"));

      globals.setObject(user,my_ns,"optionset",optset);

      // All done - bounce back to the menu.
      rc = new Redirect("SERVLET","sysadmin/main.js.vs");

    } // end try
    catch (e)
    { // thrown an exception from the above process - what sort?
      etype = dynamo.exceptionType(e) + "";
      logger.error("Caught exception of type " + etype);
      if (etype.match(/ValidationException/))
        dlg.setErrorMessage(dynamo.exceptionMessage(e) + " Please try again.");
      else if (etype.match(/DatabaseException/))
        rc = new ErrorBox("Database Error",e,"SERVLET","sysadmin/main.js.vs");
      else if (etype.match(/DynamoSecurityException/))
        rc = new ErrorBox("Security Error",e,"SERVLET","sysadmin/main.js.vs");
      else
        rc = new ErrorBox("Unknown Exception",e,"SERVLET","sysadmin/main.js.vs");

    } // end catch

  } // end else

} // end else

if (rc==null)
  rc = new FrameDialog(dlg);  // output dialog if we don't have another value
dynamo.scriptOutput(rc);
