|
@@ -26,7 +26,10 @@ import com.dmdirc.ClientModule.GlobalConfig;
|
26
|
26
|
import com.dmdirc.DMDircMBassador;
|
27
|
27
|
import com.dmdirc.addons.ui_swing.EDTInvocation;
|
28
|
28
|
import com.dmdirc.addons.ui_swing.UIUtilities;
|
|
29
|
+import com.dmdirc.addons.ui_swing.components.IconManager;
|
29
|
30
|
import com.dmdirc.addons.ui_swing.injection.MainWindow;
|
|
31
|
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
|
|
32
|
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
|
30
|
33
|
import com.dmdirc.config.ConfigBinder;
|
31
|
34
|
import com.dmdirc.config.ConfigBinding;
|
32
|
35
|
import com.dmdirc.config.prefs.PluginPreferencesCategory;
|
|
@@ -40,18 +43,17 @@ import com.dmdirc.events.DisplayProperty;
|
40
|
43
|
import com.dmdirc.interfaces.GroupChatUser;
|
41
|
44
|
import com.dmdirc.interfaces.User;
|
42
|
45
|
import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
43
|
|
-import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
|
44
|
46
|
import com.dmdirc.parser.interfaces.StringConverter;
|
45
|
47
|
import com.dmdirc.plugins.PluginDomain;
|
46
|
48
|
import com.dmdirc.plugins.PluginInfo;
|
47
|
|
-import com.dmdirc.addons.ui_swing.components.IconManager;
|
48
|
49
|
import com.dmdirc.ui.messages.ColourManager;
|
49
|
50
|
import com.dmdirc.util.colours.Colour;
|
50
|
51
|
|
|
52
|
+import java.awt.Color;
|
51
|
53
|
import java.awt.Window;
|
52
|
|
-import java.util.ArrayList;
|
53
|
|
-import java.util.Collection;
|
|
54
|
+import java.nio.file.Path;
|
54
|
55
|
import java.util.List;
|
|
56
|
+import java.util.Map;
|
55
|
57
|
|
56
|
58
|
import javax.inject.Inject;
|
57
|
59
|
import javax.inject.Provider;
|
|
@@ -67,24 +69,19 @@ public class NickColourManager {
|
67
|
69
|
|
68
|
70
|
/** Manager to parse colours with. */
|
69
|
71
|
private final ColourManager colourManager;
|
70
|
|
- /** Config to read settings from. */
|
71
|
|
- private final AggregateConfigProvider globalConfig;
|
72
|
|
- /** Config binder. */
|
73
|
72
|
private final ConfigBinder configBinder;
|
74
|
|
- /** Plugin's setting domain. */
|
75
|
|
- private final String domain;
|
76
|
73
|
private final IconManager iconManager;
|
77
|
74
|
private final Provider<Window> mainWindowProvider;
|
78
|
|
- /** Event bus to subscribe to events on . */
|
79
|
75
|
private final DMDircMBassador eventBus;
|
80
|
|
- /** The plugin's info. */
|
|
76
|
+ private final NickColourYamlStore nickColourYamlStore;
|
|
77
|
+ private final Path path;
|
81
|
78
|
private final PluginInfo pluginInfo;
|
82
|
|
- /** "Random" colours to use to colour nicknames. */
|
83
|
79
|
private String[] randColours = {
|
84
|
80
|
"E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3",};
|
85
|
81
|
private boolean useowncolour;
|
86
|
82
|
private String owncolour;
|
87
|
83
|
private boolean userandomcolour;
|
|
84
|
+ private Map<String, Color> nickColours;
|
88
|
85
|
|
89
|
86
|
@Inject
|
90
|
87
|
public NickColourManager(
|
|
@@ -94,14 +91,16 @@ public class NickColourManager {
|
94
|
91
|
final IconManager iconManager,
|
95
|
92
|
@MainWindow final Provider<Window> mainWindowProvider,
|
96
|
93
|
@PluginDomain(NickColourPlugin.class) final PluginInfo pluginInfo,
|
97
|
|
- final DMDircMBassador eventBus) {
|
98
|
|
- this.domain = domain;
|
99
|
|
- this.globalConfig = globalConfig;
|
|
94
|
+ final DMDircMBassador eventBus,
|
|
95
|
+ final NickColourYamlStore nickColourYamlStore,
|
|
96
|
+ @Directory(DirectoryType.BASE) final Path path) {
|
100
|
97
|
this.colourManager = colourManager;
|
101
|
98
|
this.iconManager = iconManager;
|
102
|
99
|
this.mainWindowProvider = mainWindowProvider;
|
103
|
100
|
this.pluginInfo = pluginInfo;
|
104
|
101
|
this.eventBus = eventBus;
|
|
102
|
+ this.nickColourYamlStore = nickColourYamlStore;
|
|
103
|
+ this.path = path;
|
105
|
104
|
configBinder = globalConfig.getBinder().withDefaultDomain(domain);
|
106
|
105
|
}
|
107
|
106
|
|
|
@@ -127,8 +126,8 @@ public class NickColourManager {
|
127
|
126
|
final StringConverter sc = client.getUser().getConnection().getParser().get()
|
128
|
127
|
.getStringConverter();
|
129
|
128
|
final User myself = client.getUser();
|
130
|
|
- final String nickOption1 = "color:" + sc.toLowerCase(network + ':' + client.getNickname());
|
131
|
|
- final String nickOption2 = "color:" + sc.toLowerCase("*:" + client.getNickname());
|
|
129
|
+ final String nickOption1 = sc.toLowerCase(network + ':' + client.getNickname());
|
|
130
|
+ final String nickOption2 = sc.toLowerCase("*:" + client.getNickname());
|
132
|
131
|
|
133
|
132
|
if (useowncolour && client.getUser().equals(myself)) {
|
134
|
133
|
final Colour color = colourManager.getColourFromString(owncolour, null);
|
|
@@ -137,22 +136,14 @@ public class NickColourManager {
|
137
|
136
|
putColour(client, getColour(client.getNickname()));
|
138
|
137
|
}
|
139
|
138
|
|
140
|
|
- String[] parts = null;
|
141
|
|
-
|
142
|
|
- if (globalConfig.hasOptionString(domain, nickOption1)) {
|
143
|
|
- parts = getParts(globalConfig, domain, nickOption1);
|
144
|
|
- } else if (globalConfig.hasOptionString(domain, nickOption2)) {
|
145
|
|
- parts = getParts(globalConfig, domain, nickOption2);
|
|
139
|
+ Color color = null;
|
|
140
|
+ if (nickColours.containsKey(nickOption1)) {
|
|
141
|
+ color = nickColours.get(nickOption1);
|
|
142
|
+ } else if (nickColours.containsKey(nickOption2)) {
|
|
143
|
+ color = nickColours.get(nickOption2);
|
146
|
144
|
}
|
147
|
|
-
|
148
|
|
- if (parts != null) {
|
149
|
|
- Colour textColor = null;
|
150
|
|
-
|
151
|
|
- if (parts[0] != null) {
|
152
|
|
- textColor = colourManager.getColourFromString(parts[0], null);
|
153
|
|
- }
|
154
|
|
-
|
155
|
|
- putColour(client, textColor);
|
|
145
|
+ if (color != null) {
|
|
146
|
+ putColour(client, new Colour(color.getRed(), color.getGreen(), color.getBlue()));
|
156
|
147
|
}
|
157
|
148
|
}
|
158
|
149
|
|
|
@@ -185,69 +176,13 @@ public class NickColourManager {
|
185
|
176
|
return colourManager.getColourFromString(randColours[count], null);
|
186
|
177
|
}
|
187
|
178
|
|
188
|
|
- /**
|
189
|
|
- * Reads the nick colour data from the config.
|
190
|
|
- *
|
191
|
|
- * @param config Config to read settings from
|
192
|
|
- * @param domain Config domain
|
193
|
|
- *
|
194
|
|
- * @return A multi-dimensional array of nick colour info.
|
195
|
|
- */
|
196
|
|
- public static Object[][] getData(final ReadOnlyConfigProvider config, final String domain) {
|
197
|
|
- final Collection<Object[]> data = new ArrayList<>();
|
198
|
|
-
|
199
|
|
- config.getOptions(domain).keySet().stream().filter(key -> key.startsWith("color:"))
|
200
|
|
- .forEach(key -> {
|
201
|
|
- final String network = key.substring(6, key.indexOf(':', 6));
|
202
|
|
- final String user = key.substring(1 + key.indexOf(':', 6));
|
203
|
|
- final String[] parts = getParts(config, domain, key);
|
204
|
|
-
|
205
|
|
- data.add(new Object[]{network, user, parts[0], parts[1]});
|
206
|
|
- });
|
207
|
|
-
|
208
|
|
- final Object[][] res = new Object[data.size()][4];
|
209
|
|
-
|
210
|
|
- int i = 0;
|
211
|
|
- for (Object[] row : data) {
|
212
|
|
- res[i] = row;
|
213
|
|
-
|
214
|
|
- i++;
|
215
|
|
- }
|
216
|
|
-
|
217
|
|
- return res;
|
218
|
|
- }
|
219
|
|
-
|
220
|
|
- /**
|
221
|
|
- * Retrieves the config option with the specified key, and returns an array of the colours that
|
222
|
|
- * should be used for it.
|
223
|
|
- *
|
224
|
|
- * @param config Config to read settings from
|
225
|
|
- * @param domain Config domain
|
226
|
|
- * @param key The config key to look up
|
227
|
|
- *
|
228
|
|
- * @return The colours specified by the given key
|
229
|
|
- */
|
230
|
|
- private static String[] getParts(final ReadOnlyConfigProvider config, final String domain,
|
231
|
|
- final String key) {
|
232
|
|
- String[] parts = config.getOption(domain, key).split(":");
|
233
|
|
-
|
234
|
|
- if (parts.length == 0) {
|
235
|
|
- parts = new String[]{null, null};
|
236
|
|
- } else if (parts.length == 1) {
|
237
|
|
- parts = new String[]{parts[0], null};
|
238
|
|
- } else if (parts.length == 2) {
|
239
|
|
- parts = new String[]{parts[0], parts[1]};
|
240
|
|
- }
|
241
|
|
-
|
242
|
|
- return parts;
|
243
|
|
- }
|
244
|
|
-
|
245
|
179
|
/**
|
246
|
180
|
* Loads this plugin.
|
247
|
181
|
*/
|
248
|
182
|
public void onLoad() {
|
249
|
183
|
eventBus.subscribe(this);
|
250
|
184
|
configBinder.bind(this, NickColourManager.class);
|
|
185
|
+ nickColours = nickColourYamlStore.readNickColourEntries(path.resolve("nickcolours.yml"));
|
251
|
186
|
}
|
252
|
187
|
|
253
|
188
|
/**
|
|
@@ -256,6 +191,11 @@ public class NickColourManager {
|
256
|
191
|
public void onUnload() {
|
257
|
192
|
eventBus.unsubscribe(this);
|
258
|
193
|
configBinder.unbind(this);
|
|
194
|
+ saveNickColourStore(nickColours);
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ public void saveNickColourStore(final Map<String, Color> savingNickColours) {
|
|
198
|
+ nickColourYamlStore.writeNickColourEntries(path.resolve("nickcolours.yml"), savingNickColours);
|
259
|
199
|
}
|
260
|
200
|
|
261
|
201
|
@ConfigBinding(key = "useowncolour", invocation = EDTInvocation.class)
|
|
@@ -280,29 +220,26 @@ public class NickColourManager {
|
280
|
220
|
|
281
|
221
|
@Handler
|
282
|
222
|
public void handlePrefsOpened(final ClientPrefsOpenedEvent event) {
|
283
|
|
- final PreferencesCategory general = new PluginPreferencesCategory(
|
284
|
|
- pluginInfo, "Nick Colours",
|
285
|
|
- "General configuration for NickColour plugin.");
|
286
|
|
- final PreferencesCategory colours = new PluginPreferencesCategory(
|
287
|
|
- pluginInfo, "Colours",
|
|
223
|
+ final PreferencesCategory general =
|
|
224
|
+ new PluginPreferencesCategory(pluginInfo, "Nick Colours",
|
|
225
|
+ "General configuration for NickColour plugin.");
|
|
226
|
+ final PreferencesCategory colours = new PluginPreferencesCategory(pluginInfo, "Colours",
|
288
|
227
|
"Set colours for specific nicknames.", UIUtilities.invokeAndWait(
|
289
|
|
- () -> new NickColourPanel(mainWindowProvider.get(), iconManager, colourManager,
|
290
|
|
- event.getModel().getIdentity(), event.getModel().getConfigManager(),
|
291
|
|
- pluginInfo.getDomain()
|
292
|
|
- )));
|
293
|
|
-
|
294
|
|
- general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
295
|
|
- pluginInfo.getDomain(), "userandomcolour", "Use random colour",
|
296
|
|
- "Use a pseudo-random colour for each person?",
|
297
|
|
- event.getModel().getConfigManager(), event.getModel().getIdentity()));
|
298
|
|
- general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
299
|
|
- pluginInfo.getDomain(), "useowncolour", "Use colour for own nick",
|
300
|
|
- "Always use the same colour for our own nickname?",
|
301
|
|
- event.getModel().getConfigManager(), event.getModel().getIdentity()));
|
|
228
|
+ () -> new NickColourPanel(mainWindowProvider.get(), iconManager,
|
|
229
|
+ colourManager, this, nickColours)));
|
|
230
|
+
|
|
231
|
+ general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
|
|
232
|
+ "userandomcolour", "Use random colour",
|
|
233
|
+ "Use a pseudo-random colour for each person?",
|
|
234
|
+ event.getModel().getConfigManager(), event.getModel().getIdentity()));
|
|
235
|
+ general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
|
|
236
|
+ "useowncolour", "Use colour for own nick",
|
|
237
|
+ "Always use the same colour for our own nickname?",
|
|
238
|
+ event.getModel().getConfigManager(), event.getModel().getIdentity()));
|
302
|
239
|
general.addSetting(new PreferencesSetting(PreferencesType.COLOUR, pluginInfo.getDomain(),
|
303
|
|
- "owncolour", "Colour to use for own nick",
|
304
|
|
- "Colour used for our own nickname, if above setting is enabled.",
|
305
|
|
- event.getModel().getConfigManager(), event.getModel().getIdentity()));
|
|
240
|
+ "owncolour", "Colour to use for own nick",
|
|
241
|
+ "Colour used for our own nickname, if above setting is enabled.",
|
|
242
|
+ event.getModel().getConfigManager(), event.getModel().getIdentity()));
|
306
|
243
|
|
307
|
244
|
general.addSubCategory(colours);
|
308
|
245
|
event.getModel().getCategory("Plugins").addSubCategory(general);
|