[smuxi/experiments/configurable_message_patterns: 2/6] Engine(-IRC), Frontend-GNOME: user message patterns can't be static
- From: Mirco M. M. Bauer <mmmbauer src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [smuxi/experiments/configurable_message_patterns: 2/6] Engine(-IRC), Frontend-GNOME: user message patterns can't be static
- Date: Sun, 16 Feb 2014 18:23:29 +0000 (UTC)
commit cdd9ece50df0adc0c612a70a65b893296faa6120
Author: Mirco Bauer <meebey meebey net>
Date: Sun Feb 16 18:27:36 2014 +0100
Engine(-IRC), Frontend-GNOME: user message patterns can't be static
As the smuxi-server can still host multiple Session instances the
MessageBuilderSettings type cannot store user message patterns statically. This
would overwrite the user message patterns of other users.
This commit also factors the setting members from MessageBuilder into
MessageBuilderSettings.
src/Engine-IRC/Protocols/Irc/IrcMessageBuilder.cs | 4 +-
src/Engine-IRC/Protocols/Irc/IrcProtocolManager.cs | 4 +-
src/Engine/Config/MessageBuilderSettings.cs | 65 ++++++++++++++-----
...Settings.cs => MessagePatternListController.cs} | 4 +-
src/Engine/Makefile.am | 2 +-
src/Engine/Messages/MessageBuilder.cs | 26 ++------
src/Engine/Protocols/ProtocolManagerBase.cs | 4 +-
src/Engine/Session.cs | 13 +++-
src/Frontend-GNOME/Views/Chats/GroupChatView.cs | 2 +-
9 files changed, 74 insertions(+), 50 deletions(-)
---
diff --git a/src/Engine-IRC/Protocols/Irc/IrcMessageBuilder.cs
b/src/Engine-IRC/Protocols/Irc/IrcMessageBuilder.cs
index f4f7caf..64a39fe 100644
--- a/src/Engine-IRC/Protocols/Irc/IrcMessageBuilder.cs
+++ b/src/Engine-IRC/Protocols/Irc/IrcMessageBuilder.cs
@@ -55,11 +55,11 @@ namespace Smuxi.Engine
}
// strip color and formatting if configured
- if (StripColors) {
+ if (Settings.StripColors) {
msg = Regex.Replace(msg, (char)IrcControlCode.Color +
"[0-9]{1,2}(,[0-9]{1,2})?", String.Empty);
}
- if (StripFormattings) {
+ if (Settings.StripFormattings) {
msg = Regex.Replace(msg, String.Format("({0}|{1}|{2}|{3})",
(char)IrcControlCode.Bold,
(char)IrcControlCode.Clear,
diff --git a/src/Engine-IRC/Protocols/Irc/IrcProtocolManager.cs
b/src/Engine-IRC/Protocols/Irc/IrcProtocolManager.cs
index c4f2a92..0c258fd 100644
--- a/src/Engine-IRC/Protocols/Irc/IrcProtocolManager.cs
+++ b/src/Engine-IRC/Protocols/Irc/IrcProtocolManager.cs
@@ -3332,7 +3332,7 @@ namespace Smuxi.Engine
if (!String.IsNullOrEmpty(e.PartMessage)) {
builder.AppendText(" [");
// colors in part messages are annoying
- builder.StripColors = true;
+ builder.Settings.StripColors = true;
builder.AppendMessage(e.PartMessage);
builder.AppendText("]");
}
@@ -3655,7 +3655,7 @@ namespace Smuxi.Engine
e.Data.Ident, e.Data.Host));
builder.AppendText(" [");
// colors are annoying in quit messages
- builder.StripColors = true;
+ builder.Settings.StripColors = true;
builder.AppendMessage(e.QuitMessage);
builder.AppendText("]");
var quitMsg = builder.ToMessage();
diff --git a/src/Engine/Config/MessageBuilderSettings.cs b/src/Engine/Config/MessageBuilderSettings.cs
index 0a41d13..7ec0533 100644
--- a/src/Engine/Config/MessageBuilderSettings.cs
+++ b/src/Engine/Config/MessageBuilderSettings.cs
@@ -27,32 +27,44 @@ namespace Smuxi.Engine
public class MessageBuilderSettings
{
static List<MessagePatternModel> BuiltinPatterns { get; set; }
- static List<MessagePatternModel> GlobalPatterns { get; set; }
- public List<MessagePatternModel> Patterns { get; private set; }
+ public List<MessagePatternModel> UserPatterns { get; set; }
+ public List<MessagePatternModel> Patterns { get; set; }
+ public bool NickColors { get; set; }
+ public bool StripFormattings { get; set; }
+ public bool StripColors { get; set; }
+ public TextColor HighlightColor { get; set; }
+ public List<string> HighlightWords { get; set; }
static MessageBuilderSettings()
{
BuiltinPatterns = new List<MessagePatternModel>();
InitBuiltinSmartLinks();
- GlobalPatterns = new List<MessagePatternModel>();
}
public MessageBuilderSettings()
{
- var builtinPatterns = BuiltinPatterns;
- var globalPatterns = GlobalPatterns;
+ NickColors = true;
// No need to lock BuiltinPatterns as List<T> is thread-safe for
// multiple readers as long as there is no writer at the same time.
- // BuiltinSmartLinks is only written once before the first instance
+ // BuiltinPatterns is only written once before the first instance
// of MessageBuilderSettings is created via the static initializer.
- Patterns = new List<MessagePatternModel>(builtinPatterns.Count +
- globalPatterns.Count);
- Patterns.AddRange(builtinPatterns);
+ Patterns = new List<MessagePatternModel>(BuiltinPatterns);
+ }
- // GlobalPatterns is only set atomically in ApplyStaticConfig()
- // and the collection is never modified after that
- Patterns.AddRange(globalPatterns);
+ public MessageBuilderSettings(MessageBuilderSettings settings)
+ {
+ if (settings == null) {
+ throw new ArgumentNullException("settings");
+ }
+
+ UserPatterns = new List<MessagePatternModel>(settings.UserPatterns);
+ Patterns = new List<MessagePatternModel>(settings.Patterns);
+ NickColors = settings.NickColors;
+ StripFormattings = settings.StripFormattings;
+ StripColors = settings.StripColors;
+ HighlightColor = settings.HighlightColor;
+ HighlightWords = settings.HighlightWords;
}
static void InitBuiltinSmartLinks()
@@ -297,16 +309,35 @@ namespace Smuxi.Engine
// TODO: JID
}
- public static void ApplyStaticConfig(UserConfig userConfig)
+ public void ApplyConfig(UserConfig userConfig)
{
- Trace.Call(userConfig);
-
if (userConfig == null) {
throw new ArgumentNullException("userConfig");
}
- var settings = new MessagePatternSettings(userConfig);
- GlobalPatterns = settings.GetList();
+ NickColors = (bool) userConfig["Interface/Notebook/Channel/NickColors"];
+ StripColors = (bool) userConfig["Interface/Notebook/StripColors"];
+ StripFormattings = (bool) userConfig["Interface/Notebook/StripFormattings"];
+ HighlightColor = TextColor.Parse(
+ (string) userConfig["Interface/Notebook/Tab/HighlightColor"]
+ );
+ HighlightWords = new List<string>(
+ (string[]) userConfig["Interface/Chat/HighlightWords"]
+ );
+
+ var patternController = new MessagePatternListController(userConfig);
+ var userPatterns = patternController.GetList();
+ var builtinPatterns = BuiltinPatterns;
+ var patterns = new List<MessagePatternModel>(builtinPatterns.Count +
+ userPatterns.Count);
+ // No need to lock BuiltinPatterns as List<T> is thread-safe for
+ // multiple readers as long as there is no writer at the same time.
+ // BuiltinPatterns is only written once before the first instance
+ // of MessageBuilderSettings is created via the static initializer.
+ patterns.AddRange(builtinPatterns);
+ patterns.AddRange(userPatterns);
+ Patterns = patterns;
+ UserPatterns = userPatterns;
}
}
}
diff --git a/src/Engine/Config/MessagePatternSettings.cs b/src/Engine/Config/MessagePatternListController.cs
similarity index 97%
rename from src/Engine/Config/MessagePatternSettings.cs
rename to src/Engine/Config/MessagePatternListController.cs
index fc55e03..9898d76 100644
--- a/src/Engine/Config/MessagePatternSettings.cs
+++ b/src/Engine/Config/MessagePatternListController.cs
@@ -27,7 +27,7 @@ using Smuxi.Engine;
namespace Smuxi.Engine
{
- public class MessagePatternSettings
+ public class MessagePatternListController
{
UserConfig UserConfig { get; set; }
@@ -40,7 +40,7 @@ namespace Smuxi.Engine
}
}
- public MessagePatternSettings(UserConfig userConfig)
+ public MessagePatternListController(UserConfig userConfig)
{
if (userConfig == null) {
throw new ArgumentNullException("userConfig");
diff --git a/src/Engine/Makefile.am b/src/Engine/Makefile.am
index 99b0152..4c6f2df 100644
--- a/src/Engine/Makefile.am
+++ b/src/Engine/Makefile.am
@@ -94,8 +94,8 @@ FILES = \
Config/ProxyType.cs \
Config/EntrySettings.cs \
Config/MessageBuilderSettings.cs \
+ Config/MessagePatternListController.cs \
Config/MessagePatternModel.cs \
- Config/MessagePatternSettings.cs \
Protocols/ProtocolManagerBase.cs \
Protocols/ProtocolManagerFactory.cs \
Protocols/ProtocolManagerInfoModel.cs \
diff --git a/src/Engine/Messages/MessageBuilder.cs b/src/Engine/Messages/MessageBuilder.cs
index a944ecc..83598c2 100644
--- a/src/Engine/Messages/MessageBuilder.cs
+++ b/src/Engine/Messages/MessageBuilder.cs
@@ -38,13 +38,8 @@ namespace Smuxi.Engine
#endif
static readonly string LibraryTextDomain = "smuxi-engine";
MessageModel Message { get; set; }
- public bool NickColors { get; set; }
- public bool StripFormattings { get; set; }
- public bool StripColors { get; set; }
- public TextColor HighlightColor { get; set; }
- public List<string> HighlightWords { get; set; }
public PersonModel Me { get; set; }
- public MessageBuilderSettings Settings { get; private set; }
+ public MessageBuilderSettings Settings { get; set; }
public MessageType MessageType {
get {
@@ -74,7 +69,6 @@ namespace Smuxi.Engine
{
Message = new MessageModel();
Settings = new MessageBuilderSettings();
- NickColors = true;
}
public MessageModel ToMessage()
@@ -89,15 +83,7 @@ namespace Smuxi.Engine
throw new ArgumentNullException("userConfig");
}
- NickColors = (bool) userConfig["Interface/Notebook/Channel/NickColors"];
- StripColors = (bool) userConfig["Interface/Notebook/StripColors"];
- StripFormattings = (bool) userConfig["Interface/Notebook/StripFormattings"];
- HighlightColor = TextColor.Parse(
- (string) userConfig["Interface/Notebook/Tab/HighlightColor"]
- );
- HighlightWords = new List<string>(
- (string[]) userConfig["Interface/Chat/HighlightWords"]
- );
+ Settings.ApplyConfig(userConfig);
}
public virtual MessageBuilder Append(MessagePartModel msgPart)
@@ -310,7 +296,7 @@ namespace Smuxi.Engine
throw new ArgumentNullException("identity");
}
- if (!NickColors) {
+ if (!Settings.NickColors) {
return CreateText(identity.IdentityName);
}
@@ -346,7 +332,7 @@ namespace Smuxi.Engine
var prefix = CreateText("<");
var suffix = CreateText(">");
var nick = CreateIdendityName(contact);
- if (NickColors) {
+ if (Settings.NickColors) {
// using bg colors for the nick texts are too intrusive, thus
// map the bg color to the fg color of the surrounding tags
var senderBgColor = contact.IdentityNameColored.BackgroundColor;
@@ -438,7 +424,7 @@ namespace Smuxi.Engine
}
// go through the user's custom highlight words and check for them.
- foreach (string highLightWord in HighlightWords) {
+ foreach (string highLightWord in Settings.HighlightWords) {
if (String.IsNullOrEmpty(highLightWord)) {
continue;
}
@@ -510,7 +496,7 @@ namespace Smuxi.Engine
// ClearHighlights() has no chance to properly undo all
// highlights
textMsg.IsHighlight = true;
- textMsg.ForegroundColor = HighlightColor;
+ textMsg.ForegroundColor = Settings.HighlightColor;
}
}
diff --git a/src/Engine/Protocols/ProtocolManagerBase.cs b/src/Engine/Protocols/ProtocolManagerBase.cs
index b66527b..a638a8f 100644
--- a/src/Engine/Protocols/ProtocolManagerBase.cs
+++ b/src/Engine/Protocols/ProtocolManagerBase.cs
@@ -349,7 +349,9 @@ namespace Smuxi.Engine
{
var builder = new T();
builder.Me = Me;
- builder.ApplyConfig(Session.UserConfig);
+ // copy settings so the caller can override settings without
+ // changing the settings of the complete session
+ builder.Settings = new MessageBuilderSettings(Session.MessageBuilderSettings);
return builder;
}
diff --git a/src/Engine/Session.cs b/src/Engine/Session.cs
index 80bcf13..325bea8 100644
--- a/src/Engine/Session.cs
+++ b/src/Engine/Session.cs
@@ -58,6 +58,7 @@ namespace Smuxi.Engine
DateTime NewsFeedLastModified { get; set; }
TimeSpan NewsFeedUpdateInterval { get; set; }
TimeSpan NewsFeedRetryInterval { get; set; }
+ internal MessageBuilderSettings MessageBuilderSettings { get; private set; }
public event EventHandler<GroupChatPersonAddedEventArgs> GroupChatPersonAdded;
public event EventHandler<GroupChatPersonRemovedEventArgs> GroupChatPersonRemoved;
@@ -154,7 +155,8 @@ namespace Smuxi.Engine
_UserConfig.Changed += OnUserConfigChanged;
_FilterListController = new FilterListController(_UserConfig);
_Filters = _FilterListController.GetFilterList().Values;
- MessageBuilderSettings.ApplyStaticConfig(_UserConfig);
+ MessageBuilderSettings = new MessageBuilderSettings();
+ MessageBuilderSettings.ApplyConfig(_UserConfig);
_Chats = new List<ChatModel>();
InitSessionChat();
@@ -188,7 +190,9 @@ namespace Smuxi.Engine
protected MessageBuilder CreateMessageBuilder()
{
var builder = new MessageBuilder();
- builder.ApplyConfig(UserConfig);
+ // copy settings so the caller can override settings without
+ // changing the settings of the complete session
+ builder.Settings = new MessageBuilderSettings(MessageBuilderSettings);
return builder;
}
@@ -486,6 +490,7 @@ namespace Smuxi.Engine
"config (save|load|list)",
"config get key",
"config set key=value",
+ "config remove key",
"shutdown"
};
@@ -738,7 +743,7 @@ namespace Smuxi.Engine
if (oldValue == null && setKey.StartsWith("MessagePatterns/")) {
var id = setKey.Split('/')[1];
var parsedId = Int32.Parse(id);
- var msgPatternSettings = new MessagePatternSettings(_UserConfig);
+ var msgPatternSettings = new MessagePatternListController(_UserConfig);
var pattern = msgPatternSettings.Get(parsedId);
if (pattern == null) {
// pattern does not exist, create it with default values
@@ -761,7 +766,7 @@ namespace Smuxi.Engine
_UserConfig[setKey] = newValue;
builder.AppendText("{0} = {1}", setKey, newValue.ToString());
if (setKey.StartsWith("MessagePatterns/")) {
- MessageBuilderSettings.ApplyStaticConfig(UserConfig);
+ MessageBuilderSettings.ApplyConfig(UserConfig);
}
} catch (InvalidCastException) {
builder.AppendErrorText(
diff --git a/src/Frontend-GNOME/Views/Chats/GroupChatView.cs b/src/Frontend-GNOME/Views/Chats/GroupChatView.cs
index 8b2c4a3..a011c1c 100644
--- a/src/Frontend-GNOME/Views/Chats/GroupChatView.cs
+++ b/src/Frontend-GNOME/Views/Chats/GroupChatView.cs
@@ -570,7 +570,7 @@ namespace Smuxi.Frontend.Gnome
// TODO: do we need to optimize this? it's called very often...
Gdk.Color bgColor = _PersonTreeView.Style.Base(Gtk.StateType.Normal);
var builder = new MessageBuilder();
- builder.NickColors = true;
+ builder.Settings.NickColors = true;
builder.AppendNick(person);
renderer.Markup = PangoTools.ToMarkup(builder.ToMessage(),
bgColor);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]