//RShare
//Copyright (C) 2010 Lars Torsten Regensburger

//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
//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, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.IO;

namespace Regensburger.RShare
{
    internal static class FileMover
    {
        /// <summary>
        /// Verschiebt ein "Preference File".
        /// </summary>
        /// <param name="saveFileName">Der Dateiname der Kopie.</param>
        /// <param name="finalFileName">Der Zeildateiname.</param>
        /// <returns>Gibt an, ob das Verschieben erfolgreich war.</returns>
        public static bool MoveFile(string saveFileName, string finalFileName)
        {
            try
            {
                // Datei exisitert nicht!
                if (!File.Exists(saveFileName))
                    return false;
                FileInfo info = new FileInfo(saveFileName);
                // Datei ist leer!
                if (!(info.Length > 0))
                    return false;
                File.Delete(finalFileName);
                File.Copy(saveFileName, finalFileName);
                // Datei exisitert nicht!
                if (!File.Exists(finalFileName))
                    return false;
                info = new FileInfo(finalFileName);
                // Datei ist leer!
                if (!(info.Length > 0))
                    return false;
                File.Delete(saveFileName);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Instance.Log(ex, "The preference file \"{0}\" could not be moved because an exception has occured!", Path.GetFileName(finalFileName));
                return false;
            }
        }
    }
}