/*
* UserProfileUtility.cs
* Authors: Mike DeMauro
* Copyright (c) 2007-2008 Cornell University
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
namespace Util
{
///
/// Loads and tracks users and the current user.
///
public static class UserProfileUtility
{
#region Constants
///
/// The default directory for writing log files.
///
public const string DefaultLogDirectory = @"LogFiles\";
///
/// The default log file.
///
public const string DefaultLogFile = DefaultLogDirectory + @"ExperimentLog.txt";
///
/// Directory for save files.
///
public const string SaveDirectory = @"Saves\";
///
/// Path of the users directory.
///
public const string UsersDir = "Users";
// Path seperator character
private const char PathSeparator = '\\';
#endregion
#region Fields
// The current user for any game
private static string mCurrentUser = string.Empty;
// The event logger
private static Logger mCurrentLogger;
// List of user names that profiles exist for
private static readonly List mUserNames = new List();
#endregion
#region Properties
///
/// Gets/Sets the current user.
///
/// Cannot be set to null or empty string
public static string CurrentUser
{
get { return mCurrentUser; }
set
{
if (string.IsNullOrEmpty(value))
throw new System.ArgumentException("Cannot set user to null or empty string");
mCurrentUser = value;
createLogger();
}
}
///
/// Logs the current user out and disposes of the user's logger.
///
public static void CurrentUserLogOut()
{
mCurrentUser = null;
DisposeLogger();
}
///
/// Gets the current event logger
///
public static Logger CurrentLogger
{
get
{
if (mCurrentLogger == null)
{
createLogger();
}
return mCurrentLogger;
}
}
private static void createLogger()
{
if (mCurrentLogger != null)
{
mCurrentLogger.Dispose();
}
if (!Directory.Exists(GameScreen.GlobalRootStorageDirectory + OutputDirectory + DefaultLogDirectory))
{
Directory.CreateDirectory(GameScreen.GlobalRootStorageDirectory + OutputDirectory + DefaultLogDirectory);
}
mCurrentLogger = new Logger(GameScreen.GlobalRootStorageDirectory + OutputDirectory + DefaultLogFile);
mCurrentLogger.Initialize();
}
public static void DisposeLogger()
{
if (mCurrentLogger != null)
mCurrentLogger.Dispose();
}
///
/// Gets the current output directory
///
public static string OutputDirectory
{
get
{
if (string.IsNullOrEmpty(mCurrentUser))
{
return string.Empty;
}
return UsersDir + PathSeparator + mCurrentUser + PathSeparator;
}
}
///
/// Gets the list of users names
///
public static ReadOnlyCollection UserNames { get { return new ReadOnlyCollection(mUserNames); } }
#endregion
#region Methods
///
/// Load all user names
///
public static void LoadUserNames()
{
if (!Directory.Exists(GameScreen.GlobalRootStorageDirectory + UsersDir))
{
Directory.CreateDirectory(GameScreen.GlobalRootStorageDirectory + UsersDir);
}
string[] names = Directory.GetDirectories(GameScreen.GlobalRootStorageDirectory + UsersDir);
foreach (string name in names)
{
mUserNames.Add(name.Substring(name.LastIndexOf(PathSeparator) + 1));
}
}
///
/// Creates a new user if one does not exist with the same name.
///
/// The name of the user profile to create
/// True if the user was created successfully, False if a user with the given name already exists.
public static bool CreateUser(string userName)
{
if (mUserNames.Contains(userName))
{
return false;
}
Directory.CreateDirectory(GameScreen.GlobalRootStorageDirectory + UsersDir + PathSeparator + userName);
mUserNames.Add(userName);
return true;
}
///
/// Deletes a user.
///
/// Then name of the user profile to delete
/// True if the user was deleted
public static bool DeleteUser(string userName)
{
// Need to cleanup current user if it is to be deleted
if (CurrentUser == userName)
{
CurrentUserLogOut();
}
// Assume something went wrong or nothing happened until finished with delete operation
bool isSuccessful = false;
// Location of the user's directory
string directory = GameScreen.GlobalRootStorageDirectory + UsersDir + PathSeparator + userName;
if (Directory.Exists(directory))
{
try
{
// Delete the directory and the username, remembering the result
Directory.Delete(directory, true);
isSuccessful = mUserNames.Remove(userName);
}
catch (IOException e)
{
// Delete was unsuccessful
mCurrentLogger.Write((int)EventCode.Error, "Could not delete directory '" + directory + "'.\n" + e.ToString());
}
}
return isSuccessful;
}
#endregion
}
}