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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.freedesktop_notifications;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.config.UserConfig;
  20. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  21. import com.dmdirc.config.prefs.PreferencesCategory;
  22. import com.dmdirc.config.prefs.PreferencesDialogModel;
  23. import com.dmdirc.config.prefs.PreferencesSetting;
  24. import com.dmdirc.config.prefs.PreferencesType;
  25. import com.dmdirc.events.ClientPrefsOpenedEvent;
  26. import com.dmdirc.config.provider.AggregateConfigProvider;
  27. import com.dmdirc.config.provider.ConfigChangeListener;
  28. import com.dmdirc.config.provider.ConfigProvider;
  29. import com.dmdirc.plugins.PluginDomain;
  30. import com.dmdirc.plugins.PluginInfo;
  31. import com.dmdirc.plugins.implementations.PluginFilesHelper;
  32. import com.dmdirc.ui.messages.StyledMessageUtils;
  33. import com.dmdirc.util.io.StreamUtils;
  34. import com.google.common.base.Strings;
  35. import com.google.common.html.HtmlEscapers;
  36. import java.io.IOException;
  37. import javax.inject.Inject;
  38. import javax.inject.Singleton;
  39. import net.engio.mbassy.listener.Handler;
  40. import org.slf4j.Logger;
  41. import org.slf4j.LoggerFactory;
  42. import static com.dmdirc.util.LogUtils.USER_ERROR;
  43. @Singleton
  44. public class FDManager implements ConfigChangeListener {
  45. private static final Logger LOG = LoggerFactory.getLogger(FDManager.class);
  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. private final PluginInfo pluginInfo;
  55. /** notification timeout. */
  56. private int timeout;
  57. /** notification icon. */
  58. private String icon;
  59. /** Escape HTML. */
  60. private boolean escapehtml;
  61. /** Strip codes. */
  62. private boolean stripcodes;
  63. @Inject
  64. public FDManager(
  65. @GlobalConfig final AggregateConfigProvider config,
  66. @UserConfig final ConfigProvider userConfig,
  67. @PluginDomain(FreeDesktopNotificationsPlugin.class) final String domain,
  68. final PluginFilesHelper filesHelper,
  69. @PluginDomain(FreeDesktopNotificationsPlugin.class) final PluginInfo pluginInfo) {
  70. this.domain = domain;
  71. this.config = config;
  72. this.userConfig = userConfig;
  73. this.filesHelper = filesHelper;
  74. this.pluginInfo = pluginInfo;
  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. final String[] args = {
  89. "/usr/bin/env",
  90. "python",
  91. filesHelper.getFilesDirString() + "notify.py",
  92. "-a",
  93. "DMDirc",
  94. "-i",
  95. icon,
  96. "-t",
  97. Integer.toString(timeout * 1000),
  98. "-s",
  99. Strings.isNullOrEmpty(title) ? "Notification from DMDirc" : prepareString(title),
  100. prepareString(message)
  101. };
  102. try {
  103. final Process myProcess = Runtime.getRuntime().exec(args);
  104. StreamUtils.readStream(myProcess.getErrorStream());
  105. StreamUtils.readStream(myProcess.getInputStream());
  106. try {
  107. myProcess.waitFor();
  108. } catch (InterruptedException e) {
  109. //Not a problem, carry on
  110. }
  111. return true;
  112. } catch (SecurityException | IOException e) {
  113. LOG.info(USER_ERROR, "Unable to show notification", e);
  114. }
  115. return false;
  116. }
  117. /**
  118. * Prepare the string for sending to dbus.
  119. *
  120. * @param input Input string
  121. *
  122. * @return Input string after being processed according to config settings.
  123. */
  124. public String prepareString(final String input) {
  125. String output = input;
  126. if (stripcodes) {
  127. output = new StyledMessageUtils().stripControlCodes(output);
  128. }
  129. if (escapehtml) {
  130. output = HtmlEscapers.htmlEscaper().escape(output);
  131. }
  132. return output;
  133. }
  134. private void setCachedSettings() {
  135. timeout = config.getOptionInt(domain, "general.timeout");
  136. icon = config.getOption(domain, "general.icon");
  137. escapehtml = config.getOptionBool(domain, "advanced.escapehtml");
  138. stripcodes = config.getOptionBool(domain, "advanced.stripcodes");
  139. }
  140. @Override
  141. public void configChanged(final String domain, final String key) {
  142. setCachedSettings();
  143. }
  144. public void onLoad() {
  145. config.addChangeListener(domain, this);
  146. userConfig.setOption(domain, "general.icon", filesHelper.getFilesDirString() + "icon.png");
  147. setCachedSettings();
  148. // Extract the files needed
  149. try {
  150. filesHelper.extractResourcesEndingWith(".py");
  151. filesHelper.extractResourcesEndingWith(".png");
  152. } catch (IOException ex) {
  153. LOG.warn(USER_ERROR, "Unable to extract files for Free desktop notifications: {}",
  154. ex.getMessage(), ex);
  155. }
  156. }
  157. public void onUnLoad() {
  158. config.removeListener(this);
  159. }
  160. @Handler
  161. public void showConfig(final ClientPrefsOpenedEvent event) {
  162. final PreferencesDialogModel manager = event.getModel();
  163. final PreferencesCategory general = new PluginPreferencesCategory(
  164. pluginInfo, "FreeDesktop Notifications",
  165. "General configuration for FreeDesktop Notifications plugin.");
  166. general.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  167. pluginInfo.getDomain(), "general.timeout", "Timeout",
  168. "Length of time in seconds before the notification popup closes.",
  169. manager.getConfigManager(), manager.getIdentity()));
  170. general.addSetting(new PreferencesSetting(PreferencesType.FILE,
  171. pluginInfo.getDomain(), "general.icon", "icon",
  172. "Path to icon to use on the notification.",
  173. manager.getConfigManager(), manager.getIdentity()));
  174. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  175. pluginInfo.getDomain(), "advanced.escapehtml", "Escape HTML",
  176. "Some Implementations randomly parse HTML, escape it before showing?",
  177. manager.getConfigManager(), manager.getIdentity()));
  178. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  179. pluginInfo.getDomain(), "advanced.stripcodes", "Strip Control Codes",
  180. "Strip IRC Control codes from messages?",
  181. manager.getConfigManager(), manager.getIdentity()));
  182. manager.getCategory("Plugins").addSubCategory(general);
  183. }
  184. }