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.

FreeDesktopNotificationsPlugin.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.addons.freedesktop_notifications.commons.StringEscapeUtils;
  24. import com.dmdirc.commandparser.CommandManager;
  25. import com.dmdirc.config.IdentityManager;
  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.installer.StreamReader;
  32. import com.dmdirc.interfaces.ConfigChangeListener;
  33. import com.dmdirc.logger.ErrorLevel;
  34. import com.dmdirc.logger.Logger;
  35. import com.dmdirc.plugins.Plugin;
  36. import com.dmdirc.plugins.PluginInfo;
  37. import com.dmdirc.plugins.PluginManager;
  38. import com.dmdirc.ui.messages.Styliser;
  39. import com.dmdirc.util.resourcemanager.ResourceManager;
  40. import java.io.IOException;
  41. import java.util.ArrayList;
  42. /**
  43. * This plugin adds freedesktop Style Notifications to dmdirc.
  44. *
  45. * @author Shane 'Dataforce' McCormack
  46. */
  47. public final class FreeDesktopNotificationsPlugin extends Plugin implements ConfigChangeListener {
  48. /** The DcopCommand we created */
  49. private FDNotifyCommand command = null;
  50. /** notification timeout. */
  51. private int timeout;
  52. /** notification icon. */
  53. private String icon;
  54. /** Escape HTML. */
  55. private boolean escapehtml;
  56. /** Strict escape. */
  57. private boolean strictescape;
  58. /** Strip codes. */
  59. private boolean stripcodes;
  60. /**
  61. * Creates a new instance of the FreeDesktopNotifications Plugin.
  62. */
  63. public FreeDesktopNotificationsPlugin() {
  64. super();
  65. }
  66. /**
  67. * Used to show a notification using this plugin.
  68. *
  69. * @param title Title of dialog if applicable
  70. * @param message Message to show
  71. * @return True if the notification was shown.
  72. */
  73. public boolean showNotification(final String title, final String message) {
  74. if (getFilesDir() == null) { return false; }
  75. final ArrayList<String> args = new ArrayList<String>();
  76. args.add("/usr/bin/env");
  77. args.add("python");
  78. args.add(getFilesDirString() + "notify.py");
  79. args.add("-a");
  80. args.add("DMDirc");
  81. args.add("-i");
  82. args.add(icon);
  83. args.add("-t");
  84. args.add(Integer.toString(timeout * 1000));
  85. args.add("-s");
  86. if (title != null && !title.isEmpty()) {
  87. args.add(prepareString(title));
  88. } else {
  89. args.add("Notification from DMDirc");
  90. }
  91. args.add(prepareString(message));
  92. try {
  93. final Process myProcess = Runtime.getRuntime().exec(args.toArray(new String[]{}));
  94. final StringBuffer data = new StringBuffer();
  95. new StreamReader(myProcess.getErrorStream()).start();
  96. new StreamReader(myProcess.getInputStream(), data).start();
  97. try { myProcess.waitFor(); } catch (InterruptedException e) { }
  98. return true;
  99. } catch (SecurityException e) {
  100. } catch (IOException e) {
  101. }
  102. return false;
  103. }
  104. /**
  105. * Prepare the string for sending to dbus.
  106. *
  107. * @param input Input string
  108. * @return Input string after being processed according to config settings.
  109. */
  110. public String prepareString(final String input) {
  111. String output = input;
  112. if (stripcodes) { output = Styliser.stipControlCodes(output); }
  113. if (escapehtml) {
  114. if (strictescape) {
  115. output = StringEscapeUtils.escapeHtml(output);
  116. } else {
  117. output = output.replace("&", "&amp;");
  118. output = output.replace("<", "&lt;");
  119. output = output.replace(">", "&gt;");
  120. }
  121. }
  122. return output;
  123. }
  124. /**
  125. * Called when the plugin is loaded.
  126. */
  127. @Override
  128. public void onLoad() {
  129. IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
  130. setCachedSettings();
  131. command = new FDNotifyCommand(this);
  132. CommandManager.registerCommand(command);
  133. // Extract required Files
  134. final PluginInfo pi = PluginManager.getPluginManager().getPluginInfoByName("freedesktop_notifications");
  135. // This shouldn't actually happen, but check to make sure.
  136. if (pi != null) {
  137. // Now get the RM
  138. try {
  139. final ResourceManager res = pi.getResourceManager();
  140. // Extract the files needed
  141. try {
  142. res.extractResoucesEndingWith(getFilesDir(), ".py");
  143. res.extractResoucesEndingWith(getFilesDir(), ".png");
  144. } catch (IOException ex) {
  145. Logger.userError(ErrorLevel.MEDIUM, "Unable to extract files for Free desktop notifications: " + ex.getMessage(), ex);
  146. }
  147. } catch (IOException ioe) {
  148. Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for freedesktop_notifications: "+ioe.getMessage(), ioe);
  149. }
  150. }
  151. }
  152. /**
  153. * Called when this plugin is Unloaded.
  154. */
  155. @Override
  156. public synchronized void onUnload() {
  157. CommandManager.unregisterCommand(command);
  158. IdentityManager.getGlobalConfig().removeListener(this);
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public void domainUpdated() {
  163. IdentityManager.getAddonIdentity().setOption(getDomain(), "general.icon", getFilesDirString() + "icon.png");
  164. }
  165. /** {@inheritDoc} */
  166. @Override
  167. public void showConfig(final PreferencesDialogModel manager) {
  168. final PreferencesCategory general = new PluginPreferencesCategory(getPluginInfo(), "FreeDesktop Notifications", "General configuration for FreeDesktop Notifications plugin.");
  169. general.addSetting(new PreferencesSetting(PreferencesType.INTEGER, getDomain(), "general.timeout", "Timeout", "Length of time in seconds before the notification popup closes."));
  170. general.addSetting(new PreferencesSetting(PreferencesType.FILE, getDomain(), "general.icon", "icon", "Path to icon to use on the notification."));
  171. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, getDomain(), "advanced.escapehtml", "Escape HTML", "Some Implementations randomly parse HTML, escape it before showing?"));
  172. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, getDomain(), "advanced.strictescape", "Strict Escape HTML", "Strictly escape HTML or just the basic characters? (&, < and >)"));
  173. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, getDomain(), "advanced.stripcodes", "Strip Control Codes", "Strip IRC Control codes from messages?"));
  174. manager.getCategory("Plugins").addSubCategory(general);
  175. }
  176. private void setCachedSettings() {
  177. timeout = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "general.timeout");
  178. icon = IdentityManager.getGlobalConfig().getOption(getDomain(), "general.icon");
  179. escapehtml = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.escapehtml");
  180. strictescape = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.strictescape");
  181. stripcodes = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.stripcodes");
  182. }
  183. /** {@inheritDoc} */
  184. @Override
  185. public void configChanged(final String domain, final String key) {
  186. setCachedSettings();
  187. }
  188. }