Pārlūkot izejas kodu

Fixes issue 3443: logging plugin should cache variables

Fixes issue 3444: window status plugin needs to cache values
issue 3441: twitter plugin needs to cache variables, especially debugEnabled

Change-Id: I116863f2a4e220f330aefeff9f49ed45f698c4d3
Reviewed-on: http://gerrit.dmdirc.com/363
Tested-by: Gregory Holmes <greboid@dmdirc.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Gregory Holmes 14 gadus atpakaļ
vecāks
revīzija
a0ae765d22

+ 67
- 29
src/com/dmdirc/addons/logging/LoggingPlugin.java Parādīt failu

@@ -36,6 +36,7 @@ import com.dmdirc.config.prefs.PreferencesManager;
36 36
 import com.dmdirc.config.prefs.PreferencesSetting;
37 37
 import com.dmdirc.config.prefs.PreferencesType;
38 38
 import com.dmdirc.interfaces.ActionListener;
39
+import com.dmdirc.interfaces.ConfigChangeListener;
39 40
 import com.dmdirc.logger.ErrorLevel;
40 41
 import com.dmdirc.logger.Logger;
41 42
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
@@ -71,18 +72,33 @@ import java.util.TimerTask;
71 72
  *
72 73
  * @author Shane 'Dataforce' McCormack
73 74
  */
74
-public class LoggingPlugin extends Plugin implements ActionListener {
75
+public class LoggingPlugin extends Plugin implements ActionListener,
76
+        ConfigChangeListener {
75 77
 
76 78
     /** The command we registered. */
77 79
     private LoggingCommand command;
80
+    /** Cached boolean settings. */
81
+    private boolean networkfolders, filenamehash, addtime, stripcodes,
82
+            channelmodeprefix, autobackbuffer, backbufferTimestamp, usedate;
83
+    /** Cached string settings. */
84
+    private String timestamp, usedateformat, logDirectory, colour;
85
+    /** Cached int settings. */
86
+    private int historyLines, backbufferLines;
78 87
 
79 88
     /** Open File */
80 89
     protected class OpenFile {
81 90
 
91
+        /** Last used time. */
82 92
         public long lastUsedTime = System.currentTimeMillis();
83 93
 
94
+        /** Open file's writer. */
84 95
         public BufferedWriter writer = null;
85 96
 
97
+        /**
98
+         * Creates a new open file.
99
+         *
100
+         * @param writer Writer that has file open
101
+         */
86 102
         public OpenFile(final BufferedWriter writer) {
87 103
             this.writer = writer;
88 104
         }
@@ -117,7 +133,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
117 133
      */
118 134
     @Override
119 135
     public void onLoad() {
120
-        final File dir = new File(IdentityManager.getGlobalConfig().getOption(getDomain(), "general.directory"));
136
+        final File dir = new File(logDirectory);
121 137
         if (dir.exists()) {
122 138
             if (!dir.isDirectory()) {
123 139
                 Logger.userError(ErrorLevel.LOW, "Unable to create logging dir (file exists instead)");
@@ -128,6 +144,9 @@ public class LoggingPlugin extends Plugin implements ActionListener {
128 144
             }
129 145
         }
130 146
 
147
+        setCachedSettings();
148
+        IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
149
+
131 150
         command = new LoggingCommand();
132 151
         ActionManager.addListener(this,
133 152
                 CoreActionType.CHANNEL_OPENED,
@@ -191,8 +210,10 @@ public class LoggingPlugin extends Plugin implements ActionListener {
191 210
      */
192 211
     @Override
193 212
     public void onUnload() {
194
-        idleFileTimer.cancel();
195
-        idleFileTimer.purge();
213
+        if (idleFileTimer != null) {
214
+            idleFileTimer.cancel();
215
+            idleFileTimer.purge();
216
+        }
196 217
 
197 218
         CommandManager.unregisterCommand(command);
198 219
         ActionManager.removeListener(this);
@@ -258,7 +279,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
258 279
 
259 280
         if (parser == null) {
260 281
             // Without a parser object, we might not be able to find the file to log this to.
261
-            if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.networkfolders")) {
282
+            if (networkfolders) {
262 283
                 // We *wont* be able to, so rather than logging to an incorrect file we just won't log.
263 284
                 return;
264 285
             }
@@ -271,7 +292,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
271 292
 
272 293
         switch (type) {
273 294
             case QUERY_OPENED:
274
-                if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.autobackbuffer")) {
295
+                if (autobackbuffer) {
275 296
                     showBackBuffer(query.getFrame(), filename);
276 297
                 }
277 298
 
@@ -326,7 +347,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
326 347
 
327 348
         switch (type) {
328 349
             case CHANNEL_OPENED:
329
-                if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.autobackbuffer")) {
350
+                if (autobackbuffer) {
330 351
                     showBackBuffer(chan.getFrame(), filename);
331 352
                 }
332 353
 
@@ -449,6 +470,12 @@ public class LoggingPlugin extends Plugin implements ActionListener {
449 470
         }
450 471
     }
451 472
 
473
+    /** {@inheritDoc} */
474
+    @Override
475
+    public void configChanged(String domain, String key) {
476
+        setCachedSettings();
477
+    }
478
+
452 479
     /**
453 480
      * Add a backbuffer to a frame.
454 481
      *
@@ -456,9 +483,6 @@ public class LoggingPlugin extends Plugin implements ActionListener {
456 483
      * @param filename File to get backbuffer from
457 484
      */
458 485
     protected void showBackBuffer(final Window frame, final String filename) {
459
-        final int numLines = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "backbuffer.lines");
460
-        final String colour = IdentityManager.getGlobalConfig().getOption(getDomain(), "backbuffer.colour");
461
-        final boolean showTimestamp = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.timestamp");
462 486
         if (frame == null) {
463 487
             Logger.userError(ErrorLevel.LOW, "Given a null frame");
464 488
             return;
@@ -472,12 +496,12 @@ public class LoggingPlugin extends Plugin implements ActionListener {
472 496
                 // is returned by getLines. To counter this, we call getLines(1) and do
473 497
                 // nothing with the output.
474 498
                 file.getLines(1);
475
-                final Stack<String> lines = file.getLines(numLines);
499
+                final Stack<String> lines = file.getLines(backbufferLines);
476 500
                 while (!lines.empty()) {
477
-                    frame.addLine(getColouredString(colour, lines.pop()), showTimestamp);
501
+                    frame.addLine(getColouredString(colour, lines.pop()), backbufferTimestamp);
478 502
                 }
479 503
                 file.close();
480
-                frame.addLine(getColouredString(colour, "--- End of backbuffer\n"), showTimestamp);
504
+                frame.addLine(getColouredString(colour, "--- End of backbuffer\n"), backbufferTimestamp);
481 505
             } catch (FileNotFoundException e) {
482 506
                 Logger.userError(ErrorLevel.LOW, "Unable to show backbuffer (Filename: " + filename + "): " + e.getMessage());
483 507
             } catch (IOException e) {
@@ -545,24 +569,23 @@ public class LoggingPlugin extends Plugin implements ActionListener {
545 569
     protected boolean appendLine(final String filename, final String line) {
546 570
         final StringBuffer finalLine = new StringBuffer();
547 571
 
548
-        if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.addtime")) {
572
+        if (addtime) {
549 573
             String dateString;
550
-            final String dateFormatString = IdentityManager.getGlobalConfig().getOption(getDomain(), "general.timestamp");
551 574
             try {
552
-                final DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
575
+                final DateFormat dateFormat = new SimpleDateFormat(timestamp);
553 576
                 dateString = dateFormat.format(new Date()).trim();
554 577
             } catch (IllegalArgumentException iae) {
555 578
                 // Default to known good format
556 579
                 final DateFormat dateFormat = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]");
557 580
                 dateString = dateFormat.format(new Date()).trim();
558 581
 
559
-                Logger.userError(ErrorLevel.LOW, "Dateformat String '" + dateFormatString + "' is invalid. For more information: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html");
582
+                Logger.userError(ErrorLevel.LOW, "Dateformat String '" + timestamp + "' is invalid. For more information: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html");
560 583
             }
561 584
             finalLine.append(dateString);
562 585
             finalLine.append(" ");
563 586
         }
564 587
 
565
-        if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.stripcodes")) {
588
+        if (stripcodes) {
566 589
             finalLine.append(Styliser.stipControlCodes(line));
567 590
         } else {
568 591
             finalLine.append(line);
@@ -605,7 +628,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
605 628
         final StringBuffer file = new StringBuffer();
606 629
         String md5String = "";
607 630
 
608
-        directory.append(IdentityManager.getGlobalConfig().getOption(getDomain(), "general.directory"));
631
+        directory.append(logDirectory);
609 632
         if (directory.charAt(directory.length() - 1) != File.separatorChar) {
610 633
             directory.append(File.separatorChar);
611 634
         }
@@ -631,8 +654,8 @@ public class LoggingPlugin extends Plugin implements ActionListener {
631 654
             md5String = obj.toString();
632 655
         }
633 656
 
634
-        if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.usedate")) {
635
-            final String dateFormat = IdentityManager.getGlobalConfig().getOption(getDomain(), "advanced.usedateformat");
657
+        if (usedate) {
658
+            final String dateFormat = usedateformat;
636 659
             final String dateDir = (new SimpleDateFormat(dateFormat)).format(new Date());
637 660
             directory.append(dateDir);
638 661
             if (directory.charAt(directory.length() - 1) != File.separatorChar) {
@@ -644,7 +667,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
644 667
             }
645 668
         }
646 669
 
647
-        if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.filenamehash")) {
670
+        if (filenamehash) {
648 671
             file.append('.');
649 672
             file.append(md5(md5String));
650 673
         }
@@ -663,7 +686,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
663 686
      * @param networkName Name of network
664 687
      */
665 688
     protected void addNetworkDir(final StringBuffer directory, final StringBuffer file, final String networkName) {
666
-        if (!IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.networkfolders")) {
689
+        if (!networkfolders) {
667 690
             return;
668 691
         }
669 692
 
@@ -761,14 +784,12 @@ public class LoggingPlugin extends Plugin implements ActionListener {
761 784
      * @return name to display
762 785
      */
763 786
     protected String getDisplayName(final ChannelClientInfo channelClient, final String overrideNick) {
764
-        final boolean addModePrefix = (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.channelmodeprefix"));
765
-
766 787
         if (channelClient == null) {
767 788
             return (overrideNick.isEmpty()) ? "Unknown Client" : overrideNick;
768 789
         } else if (overrideNick.isEmpty()) {
769
-            return (addModePrefix) ? channelClient.toString() : channelClient.getClient().getNickname();
790
+            return channelmodeprefix ? channelClient.toString() : channelClient.getClient().getNickname();
770 791
         } else {
771
-            return (addModePrefix) ? channelClient.getImportantModePrefix() + overrideNick : overrideNick;
792
+            return channelmodeprefix ? channelClient.getImportantModePrefix() + overrideNick : overrideNick;
772 793
         }
773 794
     }
774 795
 
@@ -812,10 +833,27 @@ public class LoggingPlugin extends Plugin implements ActionListener {
812 833
             return false;
813 834
         }
814 835
 
815
-        new HistoryWindow("History", reader, target,
816
-                IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "history.lines"));
836
+        new HistoryWindow("History", reader, target, historyLines);
817 837
 
818 838
         return true;
819 839
     }
820 840
 
841
+    /** Updates cached settings. */
842
+    public void setCachedSettings() {
843
+        networkfolders = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.networkfolders");
844
+        filenamehash = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.filenamehash");
845
+        addtime = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.addtime");
846
+        stripcodes = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.stripcodes");
847
+        channelmodeprefix = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.channelmodeprefix");
848
+        autobackbuffer = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.autobackbuffer");
849
+        backbufferTimestamp = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.timestamp");
850
+        usedate = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.usedate");
851
+        timestamp = IdentityManager.getGlobalConfig().getOption(getDomain(), "general.timestamp");
852
+        usedateformat = IdentityManager.getGlobalConfig().getOption(getDomain(), "advanced.usedateformat");
853
+        historyLines = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "history.lines");
854
+        colour = IdentityManager.getGlobalConfig().getOption(getDomain(), "backbuffer.colour");
855
+        backbufferLines = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "backbuffer.lines");
856
+        logDirectory = IdentityManager.getGlobalConfig().getOption(getDomain(), "general.directory");
857
+    }
858
+
821 859
 }

+ 11
- 8
src/com/dmdirc/addons/parser_twitter/Twitter.java Parādīt failu

@@ -153,6 +153,9 @@ public class Twitter implements Parser, TwitterErrorHandler, TwitterRawHandler,
153 153
     /** Map to store misc stuff in. */
154 154
     private Map<Object, Object> myMap = new HashMap<Object, Object>();
155 155
 
156
+    /** Debug enabled. */
157
+    private boolean debugEnabled;
158
+
156 159
     /**
157 160
      * Create a new Twitter Parser!
158 161
      *
@@ -172,6 +175,7 @@ public class Twitter implements Parser, TwitterErrorHandler, TwitterRawHandler,
172 175
         
173 176
         resetState(true);
174 177
 
178
+        debugEnabled = getConfigManager().getOptionBool(myPlugin.getDomain(), "debugEnabled");
175 179
         if (getConfigManager().hasOptionString(myPlugin.getDomain(), "api.address."+myServerName)) {
176 180
             this.apiAddress = getConfigManager().getOption(myPlugin.getDomain(), "api.address."+myServerName);
177 181
         } else {
@@ -834,8 +838,7 @@ public class Twitter implements Parser, TwitterErrorHandler, TwitterRawHandler,
834 838
      * @param message Content of the message.
835 839
      */
836 840
     private void doDebug(final Debug code, final String message) {
837
-        final boolean debug = getConfigManager().getOptionBool(myPlugin.getDomain(), "debugEnabled");
838
-        if (debug) {
841
+        if (debugEnabled) {
839 842
             getCallbackManager().getCallbackType(DebugInfoListener.class).call(code.ordinal(), message);
840 843
         }
841 844
     }
@@ -873,7 +876,7 @@ public class Twitter implements Parser, TwitterErrorHandler, TwitterRawHandler,
873 876
         currentParsers.add(this);
874 877
         api.addErrorHandler(this);
875 878
         api.addRawHandler(this);
876
-        api.setDebug(getConfigManager().getOptionBool(myPlugin.getDomain(), "debugEnabled"));
879
+        api.setDebug(debugEnabled);
877 880
 
878 881
         getConfigManager().addChangeListener(myPlugin.getDomain(), this);
879 882
 
@@ -1281,13 +1284,12 @@ public class Twitter implements Parser, TwitterErrorHandler, TwitterRawHandler,
1281 1284
     /** {@inheritDoc} */
1282 1285
     @Override
1283 1286
     public void handleTwitterError(final TwitterAPI api, final Throwable t, final String source, final String twitterInput, final String twitterOutput, final String message) {
1284
-        final boolean debug = getConfigManager().getOptionBool(myPlugin.getDomain(), "debugEnabled");
1285
-        final boolean hide500Errors = !debug && getConfigManager().getOptionBool(myPlugin.getDomain(), "hide500Errors");
1287
+        final boolean hide500Errors = !debugEnabled && getConfigManager().getOptionBool(myPlugin.getDomain(), "hide500Errors");
1286 1288
         if (hide500Errors && message.matches("^\\(50[0-9]\\).*")) { return; }
1287 1289
         try {
1288 1290
             if (!message.isEmpty()) {
1289
-                twitterFail("Recieved an error from twitter: " + message + (debug ? " [" + source + "]" : ""));
1290
-            } else if (debug) {
1291
+                twitterFail("Recieved an error from twitter: " + message + (debugEnabled ? " [" + source + "]" : ""));
1292
+            } else if (debugEnabled) {
1291 1293
                 twitterFail("Recieved an error: " + source);
1292 1294
             }
1293 1295
             if (t != null) {
@@ -1395,7 +1397,8 @@ public class Twitter implements Parser, TwitterErrorHandler, TwitterRawHandler,
1395 1397
     public void configChanged(final String domain, final String key) {
1396 1398
         if (domain.equalsIgnoreCase(myPlugin.getDomain())) {
1397 1399
             if (key.equalsIgnoreCase("debugEnabled")) {
1398
-                api.setDebug(getConfigManager().getOptionBool(myPlugin.getDomain(), "debugEnabled"));
1400
+                debugEnabled = getConfigManager().getOptionBool(myPlugin.getDomain(), "debugEnabled");
1401
+                api.setDebug(debugEnabled);
1399 1402
             } else if (key.equalsIgnoreCase("autoAt")) {
1400 1403
                 sendPrivateNotice("'autoAt' setting was changed, reconnect needed.");
1401 1404
                 disconnect("'autoAt' setting was changed, reconnect needed.");

+ 0
- 2
src/com/dmdirc/addons/parser_twitter/api/TwitterMessage.java Parādīt failu

@@ -22,8 +22,6 @@
22 22
 
23 23
 package com.dmdirc.addons.parser_twitter.api;
24 24
 
25
-import java.text.ParseException;
26
-import java.text.SimpleDateFormat;
27 25
 import org.w3c.dom.Element;
28 26
 import org.w3c.dom.Node;
29 27
 import org.w3c.dom.NodeList;

+ 0
- 1
src/com/dmdirc/addons/parser_twitter/api/commons/StringEscapeUtils.java Parādīt failu

@@ -19,7 +19,6 @@ package com.dmdirc.addons.parser_twitter.api.commons;
19 19
 import java.io.IOException;
20 20
 import java.io.StringWriter;
21 21
 import java.io.Writer;
22
-import java.util.Locale;
23 22
 
24 23
 /**
25 24
  * <p>Escapes and unescapes <code>String</code>s for

+ 14
- 9
src/com/dmdirc/addons/windowstatus/WindowStatusPlugin.java Parādīt failu

@@ -57,6 +57,8 @@ public final class WindowStatusPlugin extends Plugin implements ActionListener,
57 57
 
58 58
     /** The panel we use in the status bar. */
59 59
     private final WindowStatusPanel panel = new WindowStatusPanel();
60
+    private boolean showname, shownone;
61
+    private String nonePrefix;
60 62
 
61 63
     /** Creates a new instance of WindowStatusPlugin. */
62 64
     public WindowStatusPlugin() {
@@ -70,7 +72,7 @@ public final class WindowStatusPlugin extends Plugin implements ActionListener,
70 72
     public void onLoad() {
71 73
         Main.getUI().getStatusBar().addComponent(panel);
72 74
         IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
73
-        updateStatus();
75
+        updateCache();
74 76
 
75 77
         ActionManager.addListener(this, CoreActionType.CLIENT_FRAME_CHANGED);
76 78
     }
@@ -98,6 +100,13 @@ public final class WindowStatusPlugin extends Plugin implements ActionListener,
98 100
         }
99 101
     }
100 102
 
103
+    private void updateCache() {
104
+        showname = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "client.showname");
105
+        shownone = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "channel.shownone");
106
+        nonePrefix = IdentityManager.getGlobalConfig().getOption(getDomain(), "channel.noneprefix");
107
+        updateStatus();
108
+    }
109
+
101 110
     /**
102 111
      * Update the window status using the current active window.
103 112
      */
@@ -139,12 +148,8 @@ public final class WindowStatusPlugin extends Plugin implements ActionListener,
139 148
 
140 149
                 if (!names.containsKey(im)) {
141 150
                     if (mode.isEmpty()) {
142
-                        if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "channel.shownone")) {
143
-                            if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), "channel.noneprefix")) {
144
-                                mode = IdentityManager.getGlobalConfig().getOption(getDomain(), "channel.noneprefix");
145
-                            } else {
146
-                                mode = "None:";
147
-                            }
151
+                        if (shownone) {
152
+                            mode = nonePrefix;
148 153
                         } else {
149 154
                             continue;
150 155
                         }
@@ -179,7 +184,7 @@ public final class WindowStatusPlugin extends Plugin implements ActionListener,
179 184
             final Query frame = (Query) current;
180 185
 
181 186
             textString.append(frame.getHost());
182
-            if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "client.showname") && frame.getServer().getParser() != null) {
187
+            if (showname && frame.getServer().getParser() != null) {
183 188
                 final ClientInfo client = frame.getServer().getParser().getClient(frame.getHost());
184 189
                 final String realname = client.getRealname();
185 190
                 if (!realname.isEmpty()) {
@@ -214,7 +219,7 @@ public final class WindowStatusPlugin extends Plugin implements ActionListener,
214 219
     /** {@inheritDoc} */
215 220
     @Override
216 221
     public void configChanged(final String domain, final String key) {
217
-        updateStatus();
222
+        updateCache();
218 223
     }
219 224
 
220 225
 }

Notiek ielāde…
Atcelt
Saglabāt