using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using ScrewTurn.Wiki.PluginFramework;
using System.Globalization;
namespace ScrewTurn.Wiki {
///
/// Allows access to current user's preferences.
///
public static class Preferences {
///
/// Loads the language from a cookie.
///
/// The language, or null.
public static string LoadLanguageFromCookie() {
HttpCookie cookie = HttpContext.Current.Request.Cookies[Settings.CultureCookieName];
if(cookie != null) {
string culture = cookie["C"];
return culture;
}
else return null;
}
///
/// Loads the language from the current user's data.
///
/// The language, or null.
public static string LoadLanguageFromUserData() {
UserInfo currentUser = SessionFacade.GetCurrentUser();
if(currentUser != null) {
string culture = Users.GetUserData(currentUser, "Culture");
return culture;
}
else return null;
}
///
/// Loads the timezone from a cookie.
///
/// The timezone, or null.
public static int? LoadTimezoneFromCookie() {
HttpCookie cookie = HttpContext.Current.Request.Cookies[Settings.CultureCookieName];
if(cookie != null) {
string timezone = cookie["T"];
int res = 0;
if(int.TryParse(timezone, NumberStyles.Any, CultureInfo.InvariantCulture, out res)) return res;
}
return null;
}
///
/// Loads the timezone from the current user's data.
///
/// The timezone, or null.
public static int? LoadTimezoneFromUserData() {
UserInfo currentUser = SessionFacade.GetCurrentUser();
if(currentUser != null) {
string timezone = Users.GetUserData(currentUser, "Timezone");
if(timezone != null) {
int res = 0;
if(int.TryParse(timezone, NumberStyles.Any, CultureInfo.InvariantCulture, out res)) return res;
}
}
return null;
}
///
/// Saves language and timezone preferences into a cookie.
///
/// The culture.
/// The timezone.
public static void SavePreferencesInCookie(string culture, int timezone) {
HttpCookie cookie = new HttpCookie(Settings.CultureCookieName);
cookie.Expires = DateTime.Now.AddYears(10);
cookie.Path = Settings.CookiePath;
cookie.Values.Add("C", culture);
cookie.Values.Add("T", timezone.ToString(CultureInfo.InvariantCulture));
HttpContext.Current.Response.Cookies.Add(cookie);
}
///
/// Deletes the language and timezone preferences cookie.
///
public static void DeletePreferencesCookie() {
HttpCookie cookie = new HttpCookie(Settings.CultureCookieName);
cookie.Expires = DateTime.Now.AddYears(-1);
cookie.Path = Settings.CookiePath;
cookie.Values.Add("C", null);
cookie.Values.Add("T", null);
HttpContext.Current.Request.Cookies.Add(cookie);
}
///
/// Saves language and timezone preferences into the current user's data.
///
/// The culture.
/// The timezone.
/// true if the data is stored, false otherwise.
public static bool SavePreferencesInUserData(string culture, int timezone) {
UserInfo user = SessionFacade.GetCurrentUser();
if(user != null && !user.Provider.UsersDataReadOnly) {
Users.SetUserData(user, "Culture", culture);
Users.SetUserData(user, "Timezone", timezone.ToString(CultureInfo.InvariantCulture));
return true;
}
else {
if(user == null) {
Log.LogEntry("Attempt to save user data when no user has logged in", EntryType.Warning, Log.SystemUsername);
}
return false;
}
}
///
/// Aligns a date/time with the User's preferences (if any).
///
/// The date/time to align.
/// The aligned date/time.
public static DateTime AlignWithTimezone(DateTime dateTime) {
// First, look for hard-stored user's preferences
// If they are not available, look at the cookie
int? tempShift = LoadTimezoneFromUserData();
if(!tempShift.HasValue) tempShift = LoadTimezoneFromCookie();
int shift = tempShift.HasValue ? tempShift.Value : Settings.DefaultTimezone;
return dateTime.ToUniversalTime().AddMinutes(shift + (dateTime.IsDaylightSavingTime() ? 60 : 0));
}
///
/// Aligns a date/time with the default timezone.
///
/// The date/time to align.
/// The aligned date/time.
public static DateTime AlignWithServerTimezone(DateTime dateTime) {
return dateTime.ToUniversalTime().AddMinutes(Settings.DefaultTimezone + (dateTime.IsDaylightSavingTime() ? 60 : 0));
}
}
}