|
@@ -0,0 +1,252 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+package com.dmdirc.addons.windowstatus;
|
|
23
|
+
|
|
24
|
+import com.dmdirc.Channel;
|
|
25
|
+import com.dmdirc.FrameContainer;
|
|
26
|
+import com.dmdirc.Query;
|
|
27
|
+import com.dmdirc.addons.ui_swing.MainFrame;
|
|
28
|
+import com.dmdirc.addons.ui_swing.SelectionListener;
|
|
29
|
+import com.dmdirc.addons.ui_swing.UIUtilities;
|
|
30
|
+import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
|
|
31
|
+import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
|
|
32
|
+import com.dmdirc.addons.windowstatus.WindowStatusModule.WindowStatusDomain;
|
|
33
|
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
|
|
34
|
+import com.dmdirc.config.prefs.PreferencesCategory;
|
|
35
|
+import com.dmdirc.config.prefs.PreferencesDialogModel;
|
|
36
|
+import com.dmdirc.config.prefs.PreferencesSetting;
|
|
37
|
+import com.dmdirc.config.prefs.PreferencesType;
|
|
38
|
+import com.dmdirc.interfaces.Connection;
|
|
39
|
+import com.dmdirc.interfaces.config.ConfigChangeListener;
|
|
40
|
+import com.dmdirc.interfaces.config.IdentityController;
|
|
41
|
+import com.dmdirc.parser.interfaces.ChannelClientInfo;
|
|
42
|
+import com.dmdirc.parser.interfaces.ChannelInfo;
|
|
43
|
+import com.dmdirc.parser.interfaces.ClientInfo;
|
|
44
|
+import com.dmdirc.plugins.PluginInfo;
|
|
45
|
+
|
|
46
|
+import java.util.HashMap;
|
|
47
|
+import java.util.Map;
|
|
48
|
+import java.util.concurrent.Callable;
|
|
49
|
+
|
|
50
|
+import javax.inject.Inject;
|
|
51
|
+
|
|
52
|
+/**
|
|
53
|
+ * Displays information related to the current window in the status bar.
|
|
54
|
+ */
|
|
55
|
+public class WindowStatusManager implements ConfigChangeListener, SelectionListener {
|
|
56
|
+
|
|
57
|
+ /** Main frame. */
|
|
58
|
+ private final MainFrame mainFrame;
|
|
59
|
+ /** Status bar we're adding to. */
|
|
60
|
+ private final SwingStatusBar statusBar;
|
|
61
|
+ /** The panel we use in the status bar. */
|
|
62
|
+ private final WindowStatusPanel panel;
|
|
63
|
+ /** Identity controller to read settings from. */
|
|
64
|
+ private final IdentityController identityController;
|
|
65
|
+ /** Plugin settings domain. */
|
|
66
|
+ private final String domain;
|
|
67
|
+ /** Should we show the real name in queries? */
|
|
68
|
+ private boolean showname;
|
|
69
|
+ /** Should we show users without modes? */
|
|
70
|
+ private boolean shownone;
|
|
71
|
+ /** Prefix for users without modes. */
|
|
72
|
+ private String nonePrefix;
|
|
73
|
+
|
|
74
|
+ @Inject
|
|
75
|
+ public WindowStatusManager(final MainFrame mainFrame, final SwingStatusBar statusBar,
|
|
76
|
+ final IdentityController identityController,
|
|
77
|
+ @WindowStatusDomain final String domain) {
|
|
78
|
+ this.domain = domain;
|
|
79
|
+ this.mainFrame = mainFrame;
|
|
80
|
+ this.statusBar = statusBar;
|
|
81
|
+ this.identityController = identityController;
|
|
82
|
+
|
|
83
|
+ panel = UIUtilities.invokeAndWait(new Callable<WindowStatusPanel>() {
|
|
84
|
+
|
|
85
|
+ /** {@inheritDoc} */
|
|
86
|
+ @Override
|
|
87
|
+ public WindowStatusPanel call() {
|
|
88
|
+ return new WindowStatusPanel();
|
|
89
|
+ }
|
|
90
|
+ });
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ /**
|
|
94
|
+ * Loads the plugin.
|
|
95
|
+ */
|
|
96
|
+ public void onLoad() {
|
|
97
|
+ statusBar.addComponent(panel);
|
|
98
|
+ mainFrame.addSelectionListener(this);
|
|
99
|
+ identityController.getGlobalConfiguration().addChangeListener(domain, this);
|
|
100
|
+ updateCache();
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ /**
|
|
104
|
+ * Unloads the plugin.
|
|
105
|
+ */
|
|
106
|
+ public void onUnload() {
|
|
107
|
+ mainFrame.removeSelectionListener(this);
|
|
108
|
+ statusBar.removeComponent(panel);
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ /** {@inheritDoc} */
|
|
112
|
+ @Override
|
|
113
|
+ public void selectionChanged(final TextFrame window) {
|
|
114
|
+ if (window != null) {
|
|
115
|
+ updateStatus(window.getContainer());
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ /** Updates the cached config settings. */
|
|
120
|
+ private void updateCache() {
|
|
121
|
+ showname = identityController.getGlobalConfiguration()
|
|
122
|
+ .getOptionBool(domain, "client.showname");
|
|
123
|
+ shownone = identityController.getGlobalConfiguration()
|
|
124
|
+ .getOptionBool(domain, "channel.shownone");
|
|
125
|
+ nonePrefix = identityController.getGlobalConfiguration()
|
|
126
|
+ .getOption(domain, "channel.noneprefix");
|
|
127
|
+ updateStatus();
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ /** Update the window status using the current active window. */
|
|
131
|
+ public void updateStatus() {
|
|
132
|
+ final TextFrame active = mainFrame.getActiveFrame();
|
|
133
|
+
|
|
134
|
+ if (active != null) {
|
|
135
|
+ updateStatus(active.getContainer());
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ /**
|
|
140
|
+ * Update the window status using a given FrameContainer as the active frame.
|
|
141
|
+ *
|
|
142
|
+ * @param current Window to use when adding status.
|
|
143
|
+ */
|
|
144
|
+ public void updateStatus(final FrameContainer current) {
|
|
145
|
+ if (current == null) {
|
|
146
|
+ return;
|
|
147
|
+ }
|
|
148
|
+ final StringBuffer textString = new StringBuffer();
|
|
149
|
+
|
|
150
|
+ if (current instanceof Connection) {
|
|
151
|
+ textString.append(((Connection) current).getAddress());
|
|
152
|
+ } else if (current instanceof Channel) {
|
|
153
|
+ final ChannelInfo chan = ((Channel) current).getChannelInfo();
|
|
154
|
+ final Map<Integer, String> names = new HashMap<>();
|
|
155
|
+ final Map<Integer, Integer> types = new HashMap<>();
|
|
156
|
+
|
|
157
|
+ textString.append(chan.getName());
|
|
158
|
+ textString.append(" - Nicks: ");
|
|
159
|
+ textString.append(chan.getChannelClientCount());
|
|
160
|
+ textString.append(" (");
|
|
161
|
+
|
|
162
|
+ for (ChannelClientInfo client : chan.getChannelClients()) {
|
|
163
|
+ String mode = client.getImportantModePrefix();
|
|
164
|
+ final Integer im = client.getClient().getParser()
|
|
165
|
+ .getChannelUserModes().indexOf(mode);
|
|
166
|
+
|
|
167
|
+ if (!names.containsKey(im)) {
|
|
168
|
+ if (mode.isEmpty()) {
|
|
169
|
+ if (shownone) {
|
|
170
|
+ mode = nonePrefix;
|
|
171
|
+ } else {
|
|
172
|
+ continue;
|
|
173
|
+ }
|
|
174
|
+ }
|
|
175
|
+ names.put(im, mode);
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ Integer count = types.get(im);
|
|
179
|
+
|
|
180
|
+ if (count == null) {
|
|
181
|
+ count = Integer.valueOf(1);
|
|
182
|
+ } else {
|
|
183
|
+ count++;
|
|
184
|
+ }
|
|
185
|
+ types.put(im, count);
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ boolean isFirst = true;
|
|
189
|
+
|
|
190
|
+ for (Map.Entry<Integer, Integer> entry : types.entrySet()) {
|
|
191
|
+ if (isFirst) {
|
|
192
|
+ isFirst = false;
|
|
193
|
+ } else {
|
|
194
|
+ textString.append(' ');
|
|
195
|
+ }
|
|
196
|
+ textString.append(names.get(entry.getKey()));
|
|
197
|
+ textString.append(entry.getValue());
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ textString.append(')');
|
|
201
|
+ } else if (current instanceof Query) {
|
|
202
|
+ final Query frame = (Query) current;
|
|
203
|
+
|
|
204
|
+ textString.append(frame.getHost());
|
|
205
|
+ if (showname && frame.getConnection().getParser() != null) {
|
|
206
|
+ final ClientInfo client = frame.getConnection().getParser()
|
|
207
|
+ .getClient(frame.getHost());
|
|
208
|
+ final String realname = client.getRealname();
|
|
209
|
+ if (realname != null && !realname.isEmpty()) {
|
|
210
|
+ textString.append(" - ");
|
|
211
|
+ textString.append(client.getRealname());
|
|
212
|
+ }
|
|
213
|
+ }
|
|
214
|
+ } else {
|
|
215
|
+ textString.append("???");
|
|
216
|
+ }
|
|
217
|
+ panel.setText(textString.toString());
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ /**
|
|
221
|
+ * Shows the configuration page.
|
|
222
|
+ *
|
|
223
|
+ * @param manager Prefs dialog manager to add settings to
|
|
224
|
+ * @param pluginInfo Plugin info
|
|
225
|
+ */
|
|
226
|
+ public void showConfig(final PreferencesDialogModel manager,
|
|
227
|
+ final PluginInfo pluginInfo) {
|
|
228
|
+ final PreferencesCategory category = new PluginPreferencesCategory(
|
|
229
|
+ pluginInfo, "Window status", "");
|
|
230
|
+
|
|
231
|
+ category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
|
232
|
+ domain, "channel.shownone", "Show 'none' count",
|
|
233
|
+ "Should the count for users with no state be shown?",
|
|
234
|
+ manager.getConfigManager(), manager.getIdentity()));
|
|
235
|
+ category.addSetting(new PreferencesSetting(PreferencesType.TEXT,
|
|
236
|
+ domain, "channel.noneprefix", "'None' count prefix",
|
|
237
|
+ "The Prefix to use when showing the 'none' count",
|
|
238
|
+ manager.getConfigManager(), manager.getIdentity()));
|
|
239
|
+ category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
|
240
|
+ domain, "client.showname", "Show real name",
|
|
241
|
+ "Should the realname for clients be shown if known?",
|
|
242
|
+ manager.getConfigManager(), manager.getIdentity()));
|
|
243
|
+
|
|
244
|
+ manager.getCategory("Plugins").addSubCategory(category);
|
|
245
|
+ }
|
|
246
|
+
|
|
247
|
+ /** {@inheritDoc} */
|
|
248
|
+ @Override
|
|
249
|
+ public void configChanged(final String domain, final String key) {
|
|
250
|
+ updateCache();
|
|
251
|
+ }
|
|
252
|
+}
|