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.8KB

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