|
@@ -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("&", "&");
|
159
|
|
- output = output.replace("<", "<");
|
160
|
|
- output = output.replace(">", ">");
|
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
|
}
|