context: #region Source information
  
  //*****************************************************************************
  //
  //    ReportCollection.cs
  //    Created by Adam (2015-10-23, 9:21)
  //
  // ---------------------------------------------------------------------------
  //
  //    Report Smart View
  //    Copyright (C) 2009-2015, Adam Halassy
  //
  // ---------------------------------------------------------------------------
  //
  //    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 3 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, see <http://www.gnu.org/licenses/>.
  //
  //*****************************************************************************
  
  #endregion
  
  using System;
  using System.Windows.Forms;
  using System.Xml;
  
  namespace ReportSmart.Documents.Collections
  {
  
      public class RsReportCollection
      {
          public const string XMLNODE_ITEM = "item";
          public const string XMLNODE_FOLDER = "folder";
          public const string XMLNODE_REPORT = "file";
          // public const string XMLNODE_LINK = "link"; // Later version"s will support link collections
  
          public static CReportItem CreateItemFromXMLNode(XmlNode aXmlNode)
          {
              CReportItem lResult = null;
  
              if (aXmlNode.Name == XMLNODE_FOLDER)
              {
                  lResult = new CReportFolder(aXmlNode.Attributes["name"].Value);
              }
              else
              if (aXmlNode.Name == XMLNODE_REPORT)
              {
                  lResult = new CReportFile(aXmlNode.Attributes["name"].Value, aXmlNode.Attributes["path"].Value);
              }
              else
                  lResult = new CReportItem(aXmlNode.Attributes["name"].Value);
  
              return lResult;
          }
  
          private TreeView _ctlGUITreeView;
          private CReportRootFolder _RootFolder;
          private string _LastFile;
          private bool _Killed;
          public bool _Modified;
  
          public bool Modified
          {
              get { return _Modified; }
          }
  
          public string CollectionName
          {
              get { return RootFolder.ItemName; }
              set
              {
                  RootFolder.ItemName = value;
                  _Modified = true;
              }
          }
  
          public bool Killed { get { return _Killed; } }
  
          public string FileName
          {
              get { return _LastFile; }
              set { _LastFile = value; }
          }
  
          public CReportRootFolder RootFolder
          {
              get { return getRootFolder(); }
              //set { 
              //		_Root = value;
              //		ApplyToGUI();
              //	}
          }
  
          public TreeView GUITreeView
          {
              get { return _ctlGUITreeView; }
              set
              {
                  _ctlGUITreeView = value;
                  if (_ctlGUITreeView != null)
                      if (_ctlGUITreeView.Nodes.IndexOf(RootFolder.GUINode) < 0)
                          _ctlGUITreeView.Nodes.Add(RootFolder.GUINode);
                  RootFolder.GUIRefresh(null);
              }
          }
  
          protected internal void setModified(bool aSet)
          {
              _Modified = aSet;
          }
  
          protected virtual CReportRootFolder getRootFolder()
          {
              return _RootFolder;
          }
  
          protected CReportRootFolder setRootFolder(CReportRootFolder aFolder)
          {
              CReportRootFolder lResult = _RootFolder;
              _RootFolder = aFolder;
              _RootFolder.Collection = this;
              ApplyToGUI();
              return lResult;
          }
  
          protected virtual CReportRootFolder createReportRootFolder(string aCollectionName)
          {
              return new CReportRootFolder(aCollectionName, this);
          }
  
          public void ClearCollection()
          {
              string lCollectionName = _RootFolder.ItemName;
              _RootFolder.Release();
              _RootFolder = createReportRootFolder(lCollectionName);
              _ctlGUITreeView.Nodes.Add(_RootFolder.GUINode);
          }
  
          public void ApplyToGUI()
          {
              if (_ctlGUITreeView != null)
              {
                  RootFolder.GUIRefresh(null);
              }
          }
  
          public bool QuickSave()
          {
              if (_LastFile == "")
                  return false;
              else
                  SaveToXML(_LastFile);
              return true;
          }
  
          public void SaveToXML(string aFile)
          {
              ExportToXML(aFile);
              _LastFile = aFile;
              setModified(false);
          }
  
          public void ExportToXML(string aFile)
          {
              XmlDocument lXMLDoc = new XmlDocument();
  
              lXMLDoc.AppendChild(lXMLDoc.CreateXmlDeclaration("1.0", "utf-8", ""));
              lXMLDoc.AppendChild(lXMLDoc.CreateDocumentType("ReportCollection", null, null, null));
              XmlNode lRootNode = lXMLDoc.CreateNode("element", "report-collection", "");
              XmlAttribute lDocVer = lXMLDoc.CreateAttribute("version");
              lDocVer.Value = "rcf0";
              lRootNode.Attributes.Append(lDocVer);
  
              lXMLDoc.AppendChild(lRootNode);
              RootFolder.BuildXML(lRootNode, lXMLDoc);
  
              lXMLDoc.Save(aFile);
  
          }
  
          public void LoadFromXML(string aFile)
          {
  #if DEBUG
              System.Diagnostics.Debugger.Log(0, "FileOperation", "Loading collection: " + aFile + "\n");
  #endif
  
              int i = 0;
              XmlNode lRoot = null;
              XmlDocument lXMLDoc = new XmlDocument();
              lXMLDoc.Load(aFile);
  
              XmlNodeList lRootSet = lXMLDoc.ChildNodes;
              for (int j = 0; j < lRootSet.Count; j++)
                  if (lRootSet[j].Name == "report-collection")
                  {
                      lRoot = lRootSet[j];
                      break;
                  }
  
              if (lRoot == null)
              {
                  throw new Exception("Invalid report collection file!");
              }
              else
              {
                  if (i < lRoot.ChildNodes.Count)
                  {
                      RootFolder.BuildFromXML(lRoot.ChildNodes[i]);
                  }
                  ApplyToGUI();
              }
              _LastFile = aFile;
              setModified(false);
          }
  
          public void Release()
          {
              ClearCollection();
              _RootFolder.Release();
              _Killed = true;
          }
  
          public RsReportCollection()
          {
  
gt:             _RootFolder = createReportRootFolder("root");