Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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