Browse Source

DI the freedesktop notifications plugin.

Change-Id: I11d67a421097ec06a01efbb467f37716742b9406
Reviewed-on: http://gerrit.dmdirc.com/3196
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8
Greg Holmes 10 years ago
parent
commit
8a11dfa6c5

+ 185
- 0
src/com/dmdirc/addons/freedesktop_notifications/FDManager.java View File

@@ -0,0 +1,185 @@
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
+
23
+package com.dmdirc.addons.freedesktop_notifications;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.ClientModule.UserConfig;
27
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
28
+import com.dmdirc.interfaces.config.ConfigChangeListener;
29
+import com.dmdirc.interfaces.config.ConfigProvider;
30
+import com.dmdirc.logger.ErrorLevel;
31
+import com.dmdirc.logger.Logger;
32
+import com.dmdirc.plugins.PluginDomain;
33
+import com.dmdirc.plugins.implementations.PluginFilesHelper;
34
+import com.dmdirc.ui.messages.Styliser;
35
+import com.dmdirc.util.io.StreamReader;
36
+
37
+import java.io.IOException;
38
+import java.util.ArrayList;
39
+
40
+import javax.inject.Inject;
41
+
42
+import org.apache.commons.lang.StringEscapeUtils;
43
+
44
+public class FDManager implements ConfigChangeListener {
45
+
46
+    /** Global configuration. */
47
+    private final AggregateConfigProvider config;
48
+    /** User configuration. */
49
+    private final ConfigProvider userConfig;
50
+    /** This plugin's settings domain. */
51
+    private final String domain;
52
+    /** Plugin files helper. */
53
+    private final PluginFilesHelper filesHelper;
54
+    /** notification timeout. */
55
+    private int timeout;
56
+    /** notification icon. */
57
+    private String icon;
58
+    /** Escape HTML. */
59
+    private boolean escapehtml;
60
+    /** Strict escape. */
61
+    private boolean strictescape;
62
+    /** Strip codes. */
63
+    private boolean stripcodes;
64
+
65
+    @Inject
66
+    public FDManager(@GlobalConfig final AggregateConfigProvider config,
67
+            @UserConfig ConfigProvider userConfig,
68
+            @PluginDomain(FreeDesktopNotificationsPlugin.class) final String domain,
69
+            final PluginFilesHelper filesHelper) {
70
+        this.domain = domain;
71
+        this.config = config;
72
+        this.userConfig = userConfig;
73
+        this.filesHelper = filesHelper;
74
+    }
75
+
76
+    /**
77
+     * Used to show a notification using this plugin.
78
+     *
79
+     * @param title   Title of dialog if applicable
80
+     * @param message Message to show
81
+     *
82
+     * @return True if the notification was shown.
83
+     */
84
+    public boolean showNotification(final String title, final String message) {
85
+        if (filesHelper.getFilesDir() == null) {
86
+            return false;
87
+        }
88
+
89
+        final ArrayList<String> args = new ArrayList<>();
90
+
91
+        args.add("/usr/bin/env");
92
+        args.add("python");
93
+        args.add(filesHelper.getFilesDirString() + "notify.py");
94
+        args.add("-a");
95
+        args.add("DMDirc");
96
+        args.add("-i");
97
+        args.add(icon);
98
+        args.add("-t");
99
+        args.add(Integer.toString(timeout * 1000));
100
+        args.add("-s");
101
+
102
+        if (title != null && !title.isEmpty()) {
103
+            args.add(prepareString(title));
104
+        } else {
105
+            args.add("Notification from DMDirc");
106
+        }
107
+        args.add(prepareString(message));
108
+
109
+        try {
110
+            final Process myProcess = Runtime.getRuntime().exec(args.toArray(new String[]{}));
111
+            final StringBuffer data = new StringBuffer();
112
+            new StreamReader(myProcess.getErrorStream()).start();
113
+            new StreamReader(myProcess.getInputStream(), data).start();
114
+            try {
115
+                myProcess.waitFor();
116
+            } catch (InterruptedException e) {
117
+            }
118
+            return true;
119
+        } catch (SecurityException | IOException e) {
120
+        }
121
+
122
+        return false;
123
+    }
124
+
125
+    /**
126
+     * Prepare the string for sending to dbus.
127
+     *
128
+     * @param input Input string
129
+     *
130
+     * @return Input string after being processed according to config settings.
131
+     */
132
+    public String prepareString(final String input) {
133
+        String output = input;
134
+        if (stripcodes) {
135
+            output = Styliser.stipControlCodes(output);
136
+        }
137
+        if (escapehtml) {
138
+            if (strictescape) {
139
+                output = StringEscapeUtils.escapeHtml(output);
140
+            } else {
141
+                output = output.replace("&", "&amp;");
142
+                output = output.replace("<", "&lt;");
143
+                output = output.replace(">", "&gt;");
144
+            }
145
+        }
146
+
147
+        return output;
148
+    }
149
+
150
+    private void setCachedSettings() {
151
+        timeout = config.getOptionInt(domain, "general.timeout");
152
+        icon = config.getOption(domain, "general.icon");
153
+        escapehtml = config.getOptionBool(domain, "advanced.escapehtml");
154
+        strictescape = config.getOptionBool(domain, "advanced.strictescape");
155
+        stripcodes = config.getOptionBool(domain, "advanced.stripcodes");
156
+    }
157
+
158
+    /** {@inheritDoc} */
159
+    @Override
160
+    public void configChanged(final String domain, final String key) {
161
+        setCachedSettings();
162
+    }
163
+
164
+    public void onLoad() {
165
+        config.addChangeListener(domain, this);
166
+        setCachedSettings();
167
+        // Extract the files needed
168
+        try {
169
+            filesHelper.extractResoucesEndingWith(".py");
170
+            filesHelper.extractResoucesEndingWith(".png");
171
+        } catch (IOException ex) {
172
+            Logger.userError(ErrorLevel.MEDIUM,
173
+                    "Unable to extract files for Free desktop notifications: " + ex.getMessage(), ex);
174
+        }
175
+    }
176
+
177
+    public void onUnLoad() {
178
+        config.removeListener(this);
179
+    }
180
+
181
+    void domainUpdated() {
182
+        userConfig.setOption(domain, "general.icon", filesHelper.getFilesDirString() + "icon.png");
183
+    }
184
+
185
+}

+ 56
- 0
src/com/dmdirc/addons/freedesktop_notifications/FDModule.java View File

@@ -0,0 +1,56 @@
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
+
23
+package com.dmdirc.addons.freedesktop_notifications;
24
+
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.plugins.PluginDomain;
27
+import com.dmdirc.plugins.PluginInfo;
28
+import com.dmdirc.plugins.implementations.PluginFilesHelper;
29
+
30
+import javax.inject.Singleton;
31
+
32
+import dagger.Module;
33
+import dagger.Provides;
34
+
35
+@Module(injects = {FDNotifyCommand.class, FDManager.class}, addsTo = ClientModule.class)
36
+public class FDModule {
37
+
38
+    private final PluginInfo pluginInfo;
39
+
40
+    public FDModule(final PluginInfo pluginInfo) {
41
+        this.pluginInfo = pluginInfo;
42
+    }
43
+
44
+    @Provides
45
+    @PluginDomain(FreeDesktopNotificationsPlugin.class)
46
+    public String getFDSettingsDomain() {
47
+        return pluginInfo.getDomain();
48
+    }
49
+
50
+    @Provides
51
+    @Singleton
52
+    public PluginFilesHelper getPluginFilesHelper() {
53
+        return new PluginFilesHelper(pluginInfo);
54
+    }
55
+
56
+}

+ 9
- 7
src/com/dmdirc/addons/freedesktop_notifications/FDNotifyCommand.java View File

@@ -30,6 +30,8 @@ import com.dmdirc.commandparser.commands.Command;
30 30
 import com.dmdirc.commandparser.commands.context.CommandContext;
31 31
 import com.dmdirc.interfaces.CommandController;
32 32
 
33
+import javax.inject.Inject;
34
+
33 35
 /**
34 36
  * The FDNotify Command shows a nice popup on using the FreeDesktop VisualNotifications.
35 37
  */
@@ -39,19 +41,19 @@ public final class FDNotifyCommand extends Command {
39 41
     public static final BaseCommandInfo INFO = new BaseCommandInfo("fdnotify",
40 42
             "fdnotify <message> - Show a nice popup where available",
41 43
             CommandType.TYPE_GLOBAL);
42
-    /** Plugin that owns this command. */
43
-    final FreeDesktopNotificationsPlugin myPlugin;
44
+    /** Manager to show notifications. */
45
+    private final FDManager manager;
44 46
 
45 47
     /**
46 48
      * Creates a new instance of FDNotifyCommand.
47 49
      *
48 50
      * @param controller The controller to use for command information.
49
-     * @param myPlugin   the plugin creating this command.
51
+     * @param manager   Manager to show notifications
50 52
      */
51
-    public FDNotifyCommand(final CommandController controller,
52
-            final FreeDesktopNotificationsPlugin myPlugin) {
53
+    @Inject
54
+    public FDNotifyCommand(final CommandController controller, final FDManager manager) {
53 55
         super(controller);
54
-        this.myPlugin = myPlugin;
56
+        this.manager = manager;
55 57
     }
56 58
 
57 59
     /** {@inheritDoc} */
@@ -62,7 +64,7 @@ public final class FDNotifyCommand extends Command {
62 64
             /** {@inheritDoc} */
63 65
             @Override
64 66
             public void run() {
65
-                myPlugin.showNotification("", args.getArgumentsAsString());
67
+                manager.showNotification("", args.getArgumentsAsString());
66 68
             }
67 69
         }.start();
68 70
     }

+ 18
- 139
src/com/dmdirc/addons/freedesktop_notifications/FreeDesktopNotificationsPlugin.java View File

@@ -27,66 +27,32 @@ import com.dmdirc.config.prefs.PreferencesCategory;
27 27
 import com.dmdirc.config.prefs.PreferencesDialogModel;
28 28
 import com.dmdirc.config.prefs.PreferencesSetting;
29 29
 import com.dmdirc.config.prefs.PreferencesType;
30
-import com.dmdirc.interfaces.CommandController;
31
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
32
-import com.dmdirc.interfaces.config.ConfigChangeListener;
33
-import com.dmdirc.interfaces.config.ConfigProvider;
34
-import com.dmdirc.interfaces.config.IdentityController;
35
-import com.dmdirc.logger.ErrorLevel;
36
-import com.dmdirc.logger.Logger;
37 30
 import com.dmdirc.plugins.Exported;
38 31
 import com.dmdirc.plugins.PluginInfo;
39 32
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
40
-import com.dmdirc.plugins.implementations.PluginFilesHelper;
41
-import com.dmdirc.ui.messages.Styliser;
42
-import com.dmdirc.util.io.StreamReader;
43 33
 
44
-import java.io.IOException;
45
-import java.util.ArrayList;
46
-
47
-import org.apache.commons.lang.StringEscapeUtils;
34
+import dagger.ObjectGraph;
48 35
 
49 36
 /**
50 37
  * This plugin adds freedesktop Style Notifications to dmdirc.
51 38
  */
52
-public class FreeDesktopNotificationsPlugin extends BaseCommandPlugin implements
53
-        ConfigChangeListener {
39
+public class FreeDesktopNotificationsPlugin extends BaseCommandPlugin {
54 40
 
55
-    /** notification timeout. */
56
-    private int timeout;
57
-    /** notification icon. */
58
-    private String icon;
59
-    /** Escape HTML. */
60
-    private boolean escapehtml;
61
-    /** Strict escape. */
62
-    private boolean strictescape;
63
-    /** Strip codes. */
64
-    private boolean stripcodes;
65 41
     /** This plugin's plugin info. */
66 42
     private final PluginInfo pluginInfo;
67
-    /** Global config. */
68
-    private final AggregateConfigProvider config;
69
-    /** Addon identity. */
70
-    private final ConfigProvider identity;
71
-    /** Plugin files helper. */
72
-    private final PluginFilesHelper filesHelper;
43
+    /** Manager to show notifications. */
44
+    private FDManager manager;
73 45
 
74
-    /**
75
-     * Creates a new instance of this plugin.
76
-     *
77
-     * @param pluginInfo         This plugin's plugin info
78
-     * @param identityController Identity Manager instance
79
-     * @param commandController  Command controller to register commands
80
-     */
81
-    public FreeDesktopNotificationsPlugin(final PluginInfo pluginInfo,
82
-            final IdentityController identityController,
83
-            final CommandController commandController) {
84
-        super(commandController);
46
+    public FreeDesktopNotificationsPlugin(final PluginInfo pluginInfo) {
85 47
         this.pluginInfo = pluginInfo;
86
-        this.filesHelper = new PluginFilesHelper(pluginInfo);
87
-        config = identityController.getGlobalConfiguration();
88
-        identity = identityController.getAddonSettings();
89
-        registerCommand(new FDNotifyCommand(commandController, this), FDNotifyCommand.INFO);
48
+    }
49
+
50
+    @Override
51
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
52
+        super.load(pluginInfo, graph);
53
+        setObjectGraph(graph.plus(new FDModule(pluginInfo)));
54
+        registerCommand(FDNotifyCommand.class, FDNotifyCommand.INFO);
55
+        manager = getObjectGraph().get(FDManager.class);
90 56
     }
91 57
 
92 58
     /**
@@ -99,69 +65,7 @@ public class FreeDesktopNotificationsPlugin extends BaseCommandPlugin implements
99 65
      */
100 66
     @Exported
101 67
     public boolean showNotification(final String title, final String message) {
102
-        if (filesHelper.getFilesDir() == null) {
103
-            return false;
104
-        }
105
-
106
-        final ArrayList<String> args = new ArrayList<>();
107
-
108
-        args.add("/usr/bin/env");
109
-        args.add("python");
110
-        args.add(filesHelper.getFilesDirString() + "notify.py");
111
-        args.add("-a");
112
-        args.add("DMDirc");
113
-        args.add("-i");
114
-        args.add(icon);
115
-        args.add("-t");
116
-        args.add(Integer.toString(timeout * 1000));
117
-        args.add("-s");
118
-
119
-        if (title != null && !title.isEmpty()) {
120
-            args.add(prepareString(title));
121
-        } else {
122
-            args.add("Notification from DMDirc");
123
-        }
124
-        args.add(prepareString(message));
125
-
126
-        try {
127
-            final Process myProcess = Runtime.getRuntime().exec(args.toArray(new String[]{}));
128
-            final StringBuffer data = new StringBuffer();
129
-            new StreamReader(myProcess.getErrorStream()).start();
130
-            new StreamReader(myProcess.getInputStream(), data).start();
131
-            try {
132
-                myProcess.waitFor();
133
-            } catch (InterruptedException e) {
134
-            }
135
-            return true;
136
-        } catch (SecurityException | IOException e) {
137
-        }
138
-
139
-        return false;
140
-    }
141
-
142
-    /**
143
-     * Prepare the string for sending to dbus.
144
-     *
145
-     * @param input Input string
146
-     *
147
-     * @return Input string after being processed according to config settings.
148
-     */
149
-    public String prepareString(final String input) {
150
-        String output = input;
151
-        if (stripcodes) {
152
-            output = Styliser.stipControlCodes(output);
153
-        }
154
-        if (escapehtml) {
155
-            if (strictescape) {
156
-                output = StringEscapeUtils.escapeHtml(output);
157
-            } else {
158
-                output = output.replace("&", "&amp;");
159
-                output = output.replace("<", "&lt;");
160
-                output = output.replace(">", "&gt;");
161
-            }
162
-        }
163
-
164
-        return output;
68
+        return manager.showNotification(title, message);
165 69
     }
166 70
 
167 71
     /**
@@ -169,16 +73,7 @@ public class FreeDesktopNotificationsPlugin extends BaseCommandPlugin implements
169 73
      */
170 74
     @Override
171 75
     public void onLoad() {
172
-        config.addChangeListener(pluginInfo.getDomain(), this);
173
-        setCachedSettings();
174
-        // Extract the files needed
175
-        try {
176
-            filesHelper.extractResoucesEndingWith(".py");
177
-            filesHelper.extractResoucesEndingWith(".png");
178
-        } catch (IOException ex) {
179
-            Logger.userError(ErrorLevel.MEDIUM,
180
-                    "Unable to extract files for Free desktop notifications: " + ex.getMessage(), ex);
181
-        }
76
+        manager.onLoad();
182 77
         super.onLoad();
183 78
     }
184 79
 
@@ -187,18 +82,16 @@ public class FreeDesktopNotificationsPlugin extends BaseCommandPlugin implements
187 82
      */
188 83
     @Override
189 84
     public synchronized void onUnload() {
190
-        config.removeListener(this);
85
+        manager.onUnLoad();
191 86
         super.onUnload();
192 87
     }
193 88
 
194
-    /** {@inheritDoc} */
195 89
     @Override
90
+    @Deprecated
196 91
     public void domainUpdated() {
197
-        identity.setOption(pluginInfo.getDomain(), "general.icon",
198
-                filesHelper.getFilesDirString() + "icon.png");
92
+        manager.domainUpdated();
199 93
     }
200 94
 
201
-    /** {@inheritDoc} */
202 95
     @Override
203 96
     public void showConfig(final PreferencesDialogModel manager) {
204 97
         final PreferencesCategory general = new PluginPreferencesCategory(
@@ -229,18 +122,4 @@ public class FreeDesktopNotificationsPlugin extends BaseCommandPlugin implements
229 122
         manager.getCategory("Plugins").addSubCategory(general);
230 123
     }
231 124
 
232
-    private void setCachedSettings() {
233
-        timeout = config.getOptionInt(pluginInfo.getDomain(), "general.timeout");
234
-        icon = config.getOption(pluginInfo.getDomain(), "general.icon");
235
-        escapehtml = config.getOptionBool(pluginInfo.getDomain(), "advanced.escapehtml");
236
-        strictescape = config.getOptionBool(pluginInfo.getDomain(), "advanced.strictescape");
237
-        stripcodes = config.getOptionBool(pluginInfo.getDomain(), "advanced.stripcodes");
238
-    }
239
-
240
-    /** {@inheritDoc} */
241
-    @Override
242
-    public void configChanged(final String domain, final String key) {
243
-        setCachedSettings();
244
-    }
245
-
246 125
 }

Loading…
Cancel
Save