/// <summary> /// Gets the data context out of the system cache, or the database. /// </summary> /// <param name="user"> Authenticated user alias (no DOMAIN). </param> /// <param name="cache"> System cache. </param> /// <param name="context"> Data connection to the database. </param> /// <returns> User context. </returns> public static UserContext GetUserContext(string user, Cache cache, CodeReviewDataContext context) { Hashtable allUserContexts = (Hashtable)cache[UserContextTableName]; if (allUserContexts == null) cache[UserContextTableName] = allUserContexts = new Hashtable(); UserContext uc = (UserContext)allUserContexts[user]; if (uc == null) { uc = new UserContext(user); var dataContexts = from cc in context.UserContexts where cc.UserName == user select cc; foreach (DataModel.UserContext dataContext in dataContexts) { if (dataContext.KeyName.Equals(UserContext.TEXT_SIZE, StringComparison.OrdinalIgnoreCase)) { int value; if (int.TryParse(dataContext.Value, out value)) uc.TextSize = value; } else if (dataContext.KeyName.Equals(UserContext.TEXT_FONT, StringComparison.OrdinalIgnoreCase)) { uc.TextFont = dataContext.Value; } else if (dataContext.KeyName.Equals(UserContext.HINT_MASK, StringComparison.OrdinalIgnoreCase)) { int value; if (int.TryParse(dataContext.Value, out value)) uc.HintsMask = value; } else if (dataContext.KeyName.Equals(UserContext.MAX_LINE_LENGTH, StringComparison.OrdinalIgnoreCase)) { int value; if (int.TryParse(dataContext.Value, out value)) uc.MaxLineLength = value; } else if (dataContext.KeyName.Equals(UserContext.SPACES_PER_TAB, StringComparison.OrdinalIgnoreCase)) { int value; if (int.TryParse(dataContext.Value, out value)) uc.SpacesPerTab = value; } else if (dataContext.KeyName.Equals(UserContext.UNIFIED_DIFF_VIEW, StringComparison.OrdinalIgnoreCase)) { bool value; if (bool.TryParse(dataContext.Value, out value)) uc.UnifiedDiffView = value; } else if (dataContext.KeyName.Equals(UserContext.COMMENT_CLICK_MODE, StringComparison.OrdinalIgnoreCase)) { try { uc.CommentClickMode = (CommentClickMode)Enum.Parse( typeof(CommentClickMode), dataContext.Value); } catch (ArgumentNullException) { } catch (ArgumentException) { } } // Compat with previous "double/single" click database entries else if (dataContext.KeyName.Equals("commentstyle", StringComparison.OrdinalIgnoreCase)) { int value; if (int.TryParse(dataContext.Value, out value)) { uc.CommentClickMode = (value == 0 ? CommentClickMode.SingleClickAnywhere : CommentClickMode.DoubleClickAnywhere); // Save translated setting context.SetUserContext(UserContext.COMMENT_CLICK_MODE, uc.CommentClickMode.ToString()); } // Remove old setting from database context.UserContexts.DeleteOnSubmit(dataContext); context.SubmitChanges(); } else if (dataContext.KeyName.Equals( UserContext.AUTO_COLLAPSE_COMMENTS, StringComparison.OrdinalIgnoreCase)) { bool value; if (bool.TryParse(dataContext.Value, out value)) uc.AutoCollapseComments = value; } } allUserContexts[user] = uc; } return uc; }
public void RecordHintShowing(int hintNumber) { string alias = GetUserAlias(); if (alias == null) return; CodeReviewDataContext context = new CodeReviewDataContext( System.Configuration.ConfigurationManager.ConnectionStrings[Config.ConnectionString].ConnectionString); UserContext uc = UserContext.GetUserContext(alias, Context.Cache, context); long mask = 1 << (hintNumber - 1); uc.HintsMask = (uc.HintsMask == null ? 0 : uc.HintsMask.Value) | mask; context.SetUserContext(UserContext.HINT_MASK, uc.HintsMask.Value.ToString()); context.Connection.Close(); context.Dispose(); }