You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FDManager.java 6.2KB

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