//
// $HeadURL: https://danbala.squirrel.local/repository/SystemProperties/trunk/Data/EnvironmentVariableManager.cs $
//
// $Author: joerg $
// $Date: 2006-02-19 17:52:58 +0100 (So, 19 Feb 2006) $
// $Revision: 3 $
//
//
// 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, 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.
//
// http://www.gnu.org/copyleft/gpl.html
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Finalize.SystemProperties.Data
{
    public class EnvironmentVariableManager
    {
        private static EnvironmentVariableManager _instance;

        private EnvironmentVariables _systemVariables;
        private EnvironmentVariables _userVariables;

        private EnvironmentVariableManager()
        {
            _systemVariables = LoadVariables(EnvironmentVariableTarget.Machine);
            _userVariables = LoadVariables(EnvironmentVariableTarget.User);
        }

        public static EnvironmentVariableManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new EnvironmentVariableManager();
                }
                return _instance;
            }
        }

        public EnvironmentVariables SystemVariables
        {
            get
            {
                return _systemVariables;
            }
        }

        public EnvironmentVariables UserVariables
        {
            get
            {
                return _userVariables;
            }
        }


        private EnvironmentVariables LoadVariables(EnvironmentVariableTarget target)
        {
            EnvironmentVariables variables = new EnvironmentVariables();

            foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables(target))
            {
                EnvironmentVariables.EnvorinmentVariablesRow var = variables.EnvorinmentVariables.NewEnvorinmentVariablesRow();
                variables.EnvorinmentVariables.AddEnvorinmentVariablesRow((string) entry.Key, (string) entry.Value);
            }

            variables.AcceptChanges();
            return variables;
        }

        public void SaveChanges()
        {
            if (_userVariables.GetChanges() != null)
            {
                SaveChanges(_userVariables, EnvironmentVariableTarget.User);
            }
            
            if (_systemVariables.GetChanges() != null)
            {
                SaveChanges(_systemVariables, EnvironmentVariableTarget.Machine);
            }
        }

        private void SaveChanges(EnvironmentVariables variables, EnvironmentVariableTarget target)
        {
            DataSet ds = variables.GetChanges(DataRowState.Added);
            if (ds != null)
            {
                foreach (EnvironmentVariables.EnvorinmentVariablesRow row in ds.Tables[0].Rows)
                {
                    Environment.SetEnvironmentVariable(row.Key, row.Value == null ? "" : row.Value, target);
                }
            }

            ds = variables.GetChanges(DataRowState.Modified);
            if (ds != null)
            {
                foreach (EnvironmentVariables.EnvorinmentVariablesRow row in ds.Tables[0].Rows)
                {
                    Environment.SetEnvironmentVariable(row.Key, row.Value == null ? "" : row.Value, target);
                }
            }

            ds = variables.GetChanges(DataRowState.Deleted);
            if (ds != null)
            {
                foreach (EnvironmentVariables.EnvorinmentVariablesRow row in ds.Tables[0].Rows)
                {
                    Environment.SetEnvironmentVariable((string) row[0, DataRowVersion.Original], null, target);
                }
            }

            variables.AcceptChanges();
        }
    }
}
