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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2006-2015 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.config.prefs.PluginPreferencesCategory;
  26. import com.dmdirc.config.prefs.PreferencesCategory;
  27. import com.dmdirc.config.prefs.PreferencesDialogModel;
  28. import com.dmdirc.config.prefs.PreferencesSetting;
  29. import com.dmdirc.config.prefs.PreferencesType;
  30. import com.dmdirc.events.ClientPrefsOpenedEvent;
  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.plugins.PluginDomain;
  35. import com.dmdirc.plugins.PluginInfo;
  36. import com.dmdirc.plugins.implementations.PluginFilesHelper;
  37. import com.dmdirc.ui.messages.Styliser;
  38. import com.dmdirc.util.io.StreamUtils;
  39. import com.google.common.base.Strings;
  40. import com.google.common.html.HtmlEscapers;
  41. import java.io.IOException;
  42. import javax.inject.Inject;
  43. import javax.inject.Singleton;
  44. import org.slf4j.Logger;
  45. import org.slf4j.LoggerFactory;
  46. import net.engio.mbassy.listener.Handler;
  47. import static com.dmdirc.util.LogUtils.USER_ERROR;
  48. @Singleton
  49. public class FDManager implements ConfigChangeListener {
  50. private static final Logger LOG = LoggerFactory.getLogger(FDManager.class);
  51. /** Global configuration. */
  52. private final AggregateConfigProvider config;
  53. /** User configuration. */
  54. private final ConfigProvider userConfig;
  55. /** This plugin's settings domain. */
  56. private final String domain;
  57. /** Plugin files helper. */
  58. private final PluginFilesHelper filesHelper;
  59. private final PluginInfo pluginInfo;
  60. /** notification timeout. */
  61. private int timeout;
  62. /** notification icon. */
  63. private String icon;
  64. /** Escape HTML. */
  65. private boolean escapehtml;
  66. /** Strip codes. */
  67. private boolean stripcodes;
  68. @Inject
  69. public FDManager(
  70. @GlobalConfig final AggregateConfigProvider config,
  71. @UserConfig final ConfigProvider userConfig,
  72. @PluginDomain(FreeDesktopNotificationsPlugin.class) final String domain,
  73. final PluginFilesHelper filesHelper,
  74. @PluginDomain(FreeDesktopNotificationsPlugin.class) final PluginInfo pluginInfo) {
  75. this.domain = domain;
  76. this.config = config;
  77. this.userConfig = userConfig;
  78. this.filesHelper = filesHelper;
  79. this.pluginInfo = pluginInfo;
  80. }
  81. /**
  82. * Used to show a notification using this plugin.
  83. *
  84. * @param title Title of dialog if applicable
  85. * @param message Message to show
  86. *
  87. * @return True if the notification was shown.
  88. */
  89. public boolean showNotification(final String title, final String message) {
  90. if (filesHelper.getFilesDir() == null) {
  91. return false;
  92. }
  93. final String[] args = {
  94. "/usr/bin/env",
  95. "python",
  96. filesHelper.getFilesDirString() + "notify.py",
  97. "-a",
  98. "DMDirc",
  99. "-i",
  100. icon,
  101. "-t",
  102. Integer.toString(timeout * 1000),
  103. "-s",
  104. Strings.isNullOrEmpty(title) ? "Notification from DMDirc" : prepareString(title),
  105. prepareString(message)
  106. };
  107. try {
  108. final Process myProcess = Runtime.getRuntime().exec(args);
  109. StreamUtils.readStream(myProcess.getErrorStream());
  110. StreamUtils.readStream(myProcess.getInputStream());
  111. try {
  112. myProcess.waitFor();
  113. } catch (InterruptedException e) {
  114. //Not a proble, carry on
  115. }
  116. return true;
  117. } catch (SecurityException | IOException e) {
  118. LOG.info(USER_ERROR, "Unable to show notification", e);
  119. }
  120. return false;
  121. }
  122. /**
  123. * Prepare the string for sending to dbus.
  124. *
  125. * @param input Input string
  126. *
  127. * @return Input string after being processed according to config settings.
  128. */
  129. public String prepareString(final String input) {
  130. String output = input;
  131. if (stripcodes) {
  132. output = Styliser.stipControlCodes(output);
  133. }
  134. if (escapehtml) {
  135. output = HtmlEscapers.htmlEscaper().escape(output);
  136. }
  137. return output;
  138. }
  139. private void setCachedSettings() {
  140. timeout = config.getOptionInt(domain, "general.timeout");
  141. icon = config.getOption(domain, "general.icon");
  142. escapehtml = config.getOptionBool(domain, "advanced.escapehtml");
  143. stripcodes = config.getOptionBool(domain, "advanced.stripcodes");
  144. }
  145. @Override
  146. public void configChanged(final String domain, final String key) {
  147. setCachedSettings();
  148. }
  149. public void onLoad() {
  150. config.addChangeListener(domain, this);
  151. userConfig.setOption(domain, "general.icon", filesHelper.getFilesDirString() + "icon.png");
  152. setCachedSettings();
  153. // Extract the files needed
  154. try {
  155. filesHelper.extractResourcesEndingWith(".py");
  156. filesHelper.extractResourcesEndingWith(".png");
  157. } catch (IOException ex) {
  158. LOG.warn(USER_ERROR, "Unable to extract files for Free desktop notifications: {}",
  159. ex.getMessage(), ex);
  160. }
  161. }
  162. public void onUnLoad() {
  163. config.removeListener(this);
  164. }
  165. @Handler
  166. public void showConfig(final ClientPrefsOpenedEvent event) {
  167. final PreferencesDialogModel manager = event.getModel();
  168. final PreferencesCategory general = new PluginPreferencesCategory(
  169. pluginInfo, "FreeDesktop Notifications",
  170. "General configuration for FreeDesktop Notifications plugin.");
  171. general.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  172. pluginInfo.getDomain(), "general.timeout", "Timeout",
  173. "Length of time in seconds before the notification popup closes.",
  174. manager.getConfigManager(), manager.getIdentity()));
  175. general.addSetting(new PreferencesSetting(PreferencesType.FILE,
  176. pluginInfo.getDomain(), "general.icon", "icon",
  177. "Path to icon to use on the notification.",
  178. manager.getConfigManager(), manager.getIdentity()));
  179. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  180. pluginInfo.getDomain(), "advanced.escapehtml", "Escape HTML",
  181. "Some Implementations randomly parse HTML, escape it before showing?",
  182. manager.getConfigManager(), manager.getIdentity()));
  183. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  184. pluginInfo.getDomain(), "advanced.stripcodes", "Strip Control Codes",
  185. "Strip IRC Control codes from messages?",
  186. manager.getConfigManager(), manager.getIdentity()));
  187. manager.getCategory("Plugins").addSubCategory(general);
  188. }
  189. }