/*
* Copyright (c) 2009 Craig Sutherland
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using FastForward.WinCore.Configuration;
namespace FastForward.Monitor
{
///
/// The UI for adding a new template.
///
public partial class AddTemplateForm
: Form
{
#region Private fields
private List existingTemplates;
private bool locationEntered = false;
#endregion
#region Constructors
///
/// Initialise a new .
///
///
public AddTemplateForm(ApplicationConfig configuration)
{
InitializeComponent();
existingTemplates = new List(from record in configuration.BuildTemplates
select record.Name);
}
#endregion
#region Public properties
#region Configuration
///
/// The configuration for the new template.
///
public BuildTemplateConfig Configuration
{
get
{
return new BuildTemplateConfig
{
Name = templateName.Text,
FileName = templateLocation.Text
};
}
}
#endregion
#endregion
#region Public events
#region OkClicked
///
/// The OK button has been clicked.
///
public event EventHandler OkClicked;
#endregion
#endregion
#region Private methods
#region ValidateData()
///
/// Validate the settings.
///
///
private bool ValidateData()
{
// Validate the name
var isValid = true;
errorProvider.SetError(templateName, null);
if (string.IsNullOrEmpty(templateName.Text))
{
isValid = false;
errorProvider.SetError(templateName, "Name is required");
}
else if (existingTemplates.Contains(templateName.Text))
{
isValid = false;
errorProvider.SetError(templateName, "Name must be unique");
}
if (locationEntered)
{
// Validate the location
errorProvider.SetError(findFileButton, null);
if (string.IsNullOrEmpty(templateLocation.Text))
{
isValid = false;
errorProvider.SetError(findFileButton, "Location is required");
}
else if (!File.Exists(templateLocation.Text))
{
isValid = false;
errorProvider.SetError(findFileButton, "Location does not point to a valid file");
}
}
return isValid;
}
#endregion
#region MoveFileToData()
///
/// Check if the template is in the data folder, if not see if the user wants to move it there.
///
private void MoveFileToData()
{
var localData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"FastForward.NET");
if (!templateLocation.Text.StartsWith(localData))
{
// Prompt the user
if (MessageBox.Show(this,
"Would you like to move this template to the data folder?",
"Move Template",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
// Find a safe new file name
var newPath = Path.Combine(localData, Path.GetFileName(templateLocation.Text));
if (File.Exists(newPath))
{
var basePath = Path.Combine(localData, Path.GetFileNameWithoutExtension(templateLocation.Text));
var extension = Path.GetExtension(templateLocation.Text);
var copy = 1;
while (true)
{
newPath = string.Format("{0} (Copy {1}){2}", basePath, copy++, extension);
if (!File.Exists(newPath)) break;
}
}
// Perform the actual move
File.Copy(templateLocation.Text, newPath);
templateLocation.Text = newPath;
}
}
}
#endregion
#endregion
#region Event handlers
#region okButton_Click()
///
/// The OK button was clicked.
///
///
///
private void okButton_Click(object sender, EventArgs e)
{
locationEntered = true;
if (ValidateData())
{
// See if the template is in application data
MoveFileToData();
if (OkClicked != null) OkClicked(this, EventArgs.Empty);
DialogResult = DialogResult.OK;
Close();
}
}
#endregion
#region OnValidationData()
///
/// Validate all the data.
///
///
///
private void OnValidationData(object sender, EventArgs e)
{
ValidateData();
}
#endregion
#region findFileButton_Click()
///
/// Allow the user to search for a file.
///
///
///
private void findFileButton_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog
{
AutoUpgradeEnabled = true,
CheckFileExists = true,
DefaultExt = "xslt",
FileName = templateLocation.Text,
Filter = "XSL-T files (*.xslt, *.xsl)|*.xsl;*.xslt|All files (*.*)|*.*",
FilterIndex = 1,
InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FastForward.NET"),
Multiselect = false,
RestoreDirectory = true,
ShowHelp = false,
ShowReadOnly = false,
Title = "Select Template"
};
if (dialog.ShowDialog(this) == DialogResult.OK)
{
templateLocation.Text = dialog.FileName;
locationEntered = true;
ValidateData();
}
}
#endregion
#region templateLocation_Enter()
///
/// Record that the user has entered this text box.
///
///
///
private void templateLocation_Enter(object sender, EventArgs e)
{
locationEntered = true;
}
#endregion
#endregion
}
}