using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace SCJMapper_V2
{
///
/// Provides some items that are user related - packed in one place..
///
class TheUser
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod( ).DeclaringType );
// distinguish for some stuff
public static bool UsesPTU { get; set; }
private static bool hasWriteAccessToFolder( string folderPath )
{
try {
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl( folderPath );
return true;
}
catch ( UnauthorizedAccessException ) {
return false;
}
}
///
/// Returns the name of the Personal Program folder in My Documents (depends on PTU use...)
/// Creates the folder if needed
///
/// Path to the Personal Program directory
static public string UserDir
{
get {
log.Debug( "UserDir - Entry" );
string docPath = Path.Combine( Environment.GetFolderPath( Environment.SpecialFolder.Personal ), Application.ProductName );
if ( !Directory.Exists( docPath ) ) Directory.CreateDirectory( docPath );
if ( UsesPTU ) {
docPath = Path.Combine( docPath, "PTU" );
if ( !Directory.Exists( docPath ) ) Directory.CreateDirectory( docPath );
}
return docPath;
}
}
///
/// The directory to store the assets (depends on PTU use...)
///
static public string FileStoreDir
{
get {
log.Debug( "FileStoreDir - Entry" );
string docPath = AppDir;
// fallback
if ( !hasWriteAccessToFolder( docPath ) )
docPath = UserDir;
if ( UsesPTU )
return Path.Combine( docPath, "PTU_Storage" );
else
return Path.Combine( docPath, "Storage" );
}
}
///
/// The application directory
///
static public string AppDir { get => Path.GetDirectoryName( Application.ExecutablePath); }
///
/// Returns the mapping file name + path into our user dir
///
/// The mapping name
/// A fully qualified filename
static public string MappingFileName( string mapName )
{
log.Debug( "MappingFileName - Entry" );
return Path.Combine( UserDir, mapName + ".xml" );
}
///
/// Returns the Layout Groups Json file name + path into our user dir
///
/// A fully qualified filename
static public string LayoutJsonFileName( )
{
log.Debug( "LayoutJsonFileName - Entry" );
return Path.Combine( UserDir, "LayoutGroups.json" );
}
///
/// Returns the mapping file name + path into our user dir
///
/// The mapping name
/// A fully qualified filename
static public string MappingCsvFileName( string mapName )
{
log.Debug( "MappingCsvFileName - Entry" );
return Path.Combine( UserDir, mapName + ".csv" );
}
///
/// Returns the mapping file name + path into our user dir
///
/// The mapping name
/// A fully qualified filename
static public string MappingXmlFileName( string mapName )
{
log.Debug( "MappingXmlFileName - Entry" );
return Path.Combine( UserDir, mapName + ".scjm.xml" );
}
///
/// Returns the mapping file name + path into our user dir
///
/// The mapping name
/// A fully qualified filename
static public string MappingJsonFileName( string mapName )
{
log.Debug( "MappingJsonFileName - Entry" );
return Path.Combine( UserDir, mapName + ".scjm.json" );
}
///
/// Create a backupfile from the given file
///
/// The mapping name
static public void BackupMappingFile( string mapName )
{
log.Debug( "BackupMappingFile - Entry" );
string mf = MappingFileName( mapName );
if ( File.Exists( mf ) ) File.Copy( mf, mf + ".backup", true );
}
///
/// Graphics folder name in the Application directory
///
static public string GraphicsDir => "graphics";
///
/// Graphics\Layouts folder name in the Application directory
///
static public string LayoutsDir => Path.Combine(GraphicsDir, "layouts");
}
}