#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Windows.Forms;
using MiniSqlQuery.Core;
using MiniSqlQuery.Core.Commands;
namespace MiniSqlQuery.Commands
{
/// The save file as command.
public class SaveFileAsCommand
: CommandBase
{
/// Initializes a new instance of the class.
public SaveFileAsCommand()
: base("Save File &As...")
{
}
/// Execute the command.
public override void Execute()
{
IEditor editor = HostWindow.Instance.ActiveMdiChild as IEditor;
if (editor != null)
{
string oldFilename = editor.FileName;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
saveFileDialog.Filter = editor.FileFilter;
if (saveFileDialog.ShowDialog(HostWindow.Instance) == DialogResult.OK)
{
// what if this filename covers an existing open window?
string newFilename = saveFileDialog.FileName;
editor.FileName = newFilename;
editor.SaveFile();
// register the new file and remove old if applicable
var mostRecentFilesService = Services.Resolve();
mostRecentFilesService.Register(newFilename);
if (oldFilename != null && oldFilename.Equals(newFilename, StringComparison.InvariantCultureIgnoreCase))
{
mostRecentFilesService.Remove(oldFilename);
}
}
}
}
}
}