/*
// -----------------------------------------------------------------------
// 
// Copyright (c) 2001-2009, MeVis Medical Solutions AG, Bremen, Germany
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of MeVis Medical Solutions AG nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY MEVIS MEDICAL SOLUTIONS AG ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL MEVIS MEDICAL SOLUTIONS AG BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//-----------------------------------------------------------------------------
//! Module definition for DicomSettings.js
/*!
// \file    DicomSettings.js
// \author  Bernd Kuemmerlen
// \date    06/2004
*/
//-----------------------------------------------------------------------------

/* Current file version */
gFileVersion = "1.0";

gConfigFilename = String(ExpandPath("$(SettingsPath)/DICOMSettings.xml"));

var gDicomName = [];
var gDicomType = [];
var gDicomCalledAET = [];
var gDicomCallingAET = [];
var gDicomHostname = [];
var gDicomPort = [];

/* Expanddirectory name macros */
function ExpandPath (path)
{
  path = ctx.expandFilename(path);
  return path;
}

function saveDICOMConfigAction() {
  saveDICOMConfig();
}

function saveDICOMConfig(filename) {
  var dicomName = ctx.field("dicomName").stringValue();;
  var dicomType = ctx.field("dicomType").stringValue();
  var dicomCalledAET = ctx.field("dicomCalledAET").stringValue();
  var dicomCallingAET = ctx.field("dicomCallingAET").stringValue();
  var dicomHostname = ctx.field("dicomHostname").stringValue();
  var dicomPort = ctx.field("dicomPort").intValue();

  var dom = new DOMObject();
  var xmlRoot, xmlDICOMEntry, xmlCalledAET, xmlCallingAET, xmlHostname, xmlPort;

  var alreadyThere = false;

  if (!filename) {
    filename = gConfigFilename;
  }

  //ctx.log(gDicomName);

  for (var i=0; i<gDicomName.length; i++) {
    //ctx.log("Checking "+gDicomName[i]);
    if (dicomName == gDicomName[i]) {
      //ctx.log("configuration named "+dicomName+" already present, overwriting");
      alreadyThere = true;
    }

  }
  if (!alreadyThere) {
    gDicomName.push(dicomName);
  }
  gDicomType[dicomName] = dicomType;
  gDicomCalledAET[dicomName] = dicomCalledAET;
  gDicomCallingAET[dicomName] = dicomCallingAET;
  gDicomHostname[dicomName] = dicomHostname;
  gDicomPort[dicomName] = dicomPort;

  //ctx.log(gDicomName);

  //ctx.log("saveDICOMConfig()");
  //logVar("dicomName", dicomName);
  //logVar("dicomType", dicomType);
  //logVar("dicomCalledAET", dicomCalledAET);
  //logVar("dicomCallingAET", dicomCallingAET);
  //logVar("dicomHostname", dicomHostname);
  //logVar("dicomPort", dicomPort);

  // Create XML root and description elements 
  xmlRoot = new XMLElement("dicomsettings", "TEXT", "");
  xmlRoot.addAttribute("fileVersion", gFileVersion);
  dom.insertChild(xmlRoot);

  for (var i=0; i<gDicomName.length; i++) {
    dicomName = gDicomName[i];
    dicomType = gDicomType[dicomName];
    dicomCalledAET = gDicomCalledAET[dicomName];
    dicomCallingAET = gDicomCallingAET[dicomName];
    dicomHostname = gDicomHostname[dicomName];
    dicomPort = gDicomPort[dicomName];

    xmlDICOMEntry = new XMLElement("dicomentry", "TEXT", "");
    xmlDICOMEntry.addAttribute("name", dicomName);
    xmlDICOMEntry.addAttribute("type", dicomType);
    
    xmlCalledAET = new XMLElement("calledAET","TEXT",dicomCalledAET);
    xmlCallingAET = new XMLElement("callingAET","TEXT",dicomCallingAET);
    xmlHostname = new XMLElement("hostname","TEXT",dicomHostname);
    xmlPort = new XMLElement("port","TEXT",dicomPort);
    
    xmlDICOMEntry.addChild(xmlCalledAET);
    xmlDICOMEntry.addChild(xmlCallingAET);
    xmlDICOMEntry.addChild(xmlHostname);
    xmlDICOMEntry.addChild(xmlPort);
    
    xmlRoot.addChild(xmlDICOMEntry);
  }

  // Write XML tree to file 
  if (dom.writeToFile(filename))
  {
    MLAB.log("Config file written to "+filename);
  }
  else {
    MLAB.logError("Could not write config file (filename = "+filename+")");
  }

  // update combobox list
  updateConfigList();
}

function loadDICOMConfigAction() {
  loadDICOMConfig();
}

function loadDICOMConfig(filename) {
  var status = "";

  var dicomName = "";
  var dicomType = "";
  var dicomCalledAET = ""; 
  var dicomCallingAET = "";
  var dicomHostname = "";
  var dicomPort = 0;

  var dom = new DOMObject();
  var xmlRoot, xmlDICOMEntry, xmlCalledAET, xmlCallingAET, xmlHostname, xmlPort;
  if (!filename) {
    filename = gConfigFilename;
  }

   // Read XML file 
  if (!dom.readFromFile(filename))
    status = "Error reading file "+filename;
  else
  {
    // Get root element 
    xmlRoot = dom.getRoot();
    if (!xmlRoot)
      status = "Root element not found";
    else if (xmlRoot.getName() != "dicomsettings")
      status = "Unknown root element: "+xmlRoot.getName();
  }
  
  if (!status) {
    var numberOfEntries = xmlRoot.getNumberOfChildrenWithName("dicomentry");
    var entryList = [];
    
    //ctx.log("Found "+numberOfEntries+" DICOM entries");
    
    // clear current entries
    gDicomName = [];
    gDicomType = [];
    gDicomCalledAET = [];
    gDicomCallingAET = [];
    gDicomHostname = [];
    gDicomPort = [];
    
    for (var i=1; i<=numberOfEntries; i++) {
      xmlDICOMEntry = xmlRoot.getN_thChildWithName("dicomentry",i);

      dicomName = xmlDICOMEntry.getAttribute("name");
      dicomType = xmlDICOMEntry.getAttribute("type");
      
      dicomCalledAET = xmlDICOMEntry.getChildWithName("calledAET").getValue();
      dicomCallingAET = xmlDICOMEntry.getChildWithName("callingAET").getValue();
      dicomHostname = xmlDICOMEntry.getChildWithName("hostname").getValue();
      dicomPort = xmlDICOMEntry.getChildWithName("port").getValue();

      //ctx.log("got entry with name "+dicomName);
      gDicomName.push(dicomName);
      gDicomType[dicomName] = dicomType;
      gDicomCalledAET[dicomName] = dicomCalledAET;
      gDicomCallingAET[dicomName] = dicomCallingAET;
      gDicomHostname[dicomName] = dicomHostname;
      gDicomPort[dicomName] = dicomPort;

//    logVar("dicomName", dicomName);
//    logVar("dicomType", dicomType);
//     logVar("dicomCalledAET", dicomCalledAET);
//     logVar("dicomCallingAET", dicomCallingAET);
//     logVar("dicomHost", dicomHostname);
//     logVar("dicomPort", dicomPort);
    }

    updateConfigList()

/*
    ctx.field("dicomName").setStringValue(dicomName);
    ctx.field("dicomType").setStringValue(dicomType);
    ctx.field("dicomCalledAET").setStringValue(dicomCalledAET);
    ctx.field("dicomCallingAET").setStringValue(dicomCallingAET);
    ctx.field("dicomHostname").setStringValue(dicomHostname);
    ctx.field("dicomPort").setIntValue(dicomPort);
*/
      
    selectConfigSet(gDicomName[0]);
  }
  else {
    MLAB.logError("Error: "+status);
  }
}

function autoLoadDICOMConfig() {
  //ctx.log("trying autoloading");

  // Check for existance of file to prevent errors when 
  // autoloading.
  if (ctx.field("autoLoadConfig").boolValue() == true 
      && MLABFileManager.exists(gConfigFilename))
  {
    loadDICOMConfig();
  }
}

function selectConfigSet(name)
{
  ctx.field("dicomName").value = name;
  ctx.field("dicomType").value = gDicomType[name];
  ctx.field("dicomCalledAET").value = gDicomCalledAET[name];
  ctx.field("dicomCallingAET").value = gDicomCallingAET[name];
  ctx.field("dicomHostname").value = gDicomHostname[name];
  ctx.field("dicomPort").value = gDicomPort[name];
  
  ctx.field("currentConfigSet").value = name;
}

function deleteConfigSet(name)
{
  var returnIndex = 0;

  for (var i=0; i<gDicomName.length; i++) {
    if (gDicomName[i] == name) {
      // remove dicomName
      gDicomName.splice(i,1);
      gDicomType[name] = "";
      gDicomCalledAET[name] = "";
      gDicomCallingAET[name] = "";
      gDicomHostname[name] = "";
      gDicomPort[name] = "";

      returnIndex = i;
    }
  }
  return returnIndex;
}

function deleteCurrentDICOMConfig()
{
  var name =  ctx.field("currentConfigSet").value;
  var deletedIndex = 0;

  // do not remove if this is the last config set
  if (gDicomName.length > 1) {
    deletedIndex = deleteConfigSet(name);
    
    updateConfigList();
    
    if ((deletedIndex > 0) && (gDicomName.length-1 < deletedIndex)) {
      deletedIndex = gDicomName.length-1;
    }
    
    selectConfigSet(gDicomName[deletedIndex]);
  }
}

function updateConfigList(type) {
  var filteredConfigList = new Array();
  
  for (var i=0; i<gDicomName.length; i++) {
    if ((type == undefined) || (type == "") || (type == gDicomType[gDicomName[i]])) {
      filteredConfigList.push(gDicomName[i]);
    }
  }

  ctx.field("dicomConfigList").value = filteredConfigList.join(",");
}

//# MeVis signature v1
//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
//# owner: MeVis
//# date: 2009-05-09T05:37:18
//# hash: vOudj/8UVxpM68/DBNMJ2JQEDyJ8X40O7JZ08uI37s4f+7ODJ0J+vDAZ7z65ZkyD157GjdWI7VsliqllaOm3LQ==
//# MeVis end
