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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2006-2014 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.events.UserErrorEvent;
  27. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  28. import com.dmdirc.interfaces.config.ConfigChangeListener;
  29. import com.dmdirc.interfaces.config.ConfigProvider;
  30. import com.dmdirc.logger.ErrorLevel;
  31. import com.dmdirc.plugins.PluginDomain;
  32. import com.dmdirc.plugins.implementations.PluginFilesHelper;
  33. import com.dmdirc.ui.messages.Styliser;
  34. import com.dmdirc.util.io.StreamUtils;
  35. import com.google.common.base.Strings;
  36. import com.google.common.html.HtmlEscapers;
  37. import java.io.IOException;
  38. import javax.inject.Inject;
  39. import javax.inject.Singleton;
  40. @Singleton
  41. public class FDManager implements ConfigChangeListener {
  42. /** Global configuration. */
  43. private final AggregateConfigProvider config;
  44. /** User configuration. */
  45. private final ConfigProvider userConfig;
  46. /** This plugin's settings domain. */
  47. private final String domain;
  48. /** Plugin files helper. */
  49. private final PluginFilesHelper filesHelper;
  50. /** The event bus to post errors to. */
  51. private final DMDircMBassador eventBus;
  52. /** notification timeout. */
  53. private int timeout;
  54. /** notification icon. */
  55. private String icon;
  56. /** Escape HTML. */
  57. private boolean escapehtml;
  58. /** Strip codes. */
  59. private boolean stripcodes;
  60. @Inject
  61. public FDManager(
  62. @GlobalConfig final AggregateConfigProvider config,
  63. @UserConfig final ConfigProvider userConfig,
  64. @PluginDomain(FreeDesktopNotificationsPlugin.class) final String domain,
  65. final PluginFilesHelper filesHelper,
  66. final DMDircMBassador eventBus) {
  67. this.domain = domain;
  68. this.config = config;
  69. this.userConfig = userConfig;
  70. this.filesHelper = filesHelper;
  71. this.eventBus = eventBus;
  72. }
  73. /**
  74. * Used to show a notification using this plugin.
  75. *
  76. * @param title Title of dialog if applicable
  77. * @param message Message to show
  78. *
  79. * @return True if the notification was shown.
  80. */
  81. public boolean showNotification(final String title, final String message) {
  82. if (filesHelper.getFilesDir() == null) {
  83. return false;
  84. }
  85. final String[] args = {
  86. "/usr/bin/env",
  87. "python",
  88. filesHelper.getFilesDirString() + "notify.py",
  89. "-a",
  90. "DMDirc",
  91. "-i",
  92. icon,
  93. "-t",
  94. Integer.toString(timeout * 1000),
  95. "-s",
  96. Strings.isNullOrEmpty(title) ? "Notification from DMDirc" : prepareString(title),
  97. prepareString(message)
  98. };
  99. try {
  100. final Process myProcess = Runtime.getRuntime().exec(args);
  101. final StringBuffer data = new StringBuffer();
  102. StreamUtils.readStream(myProcess.getErrorStream());
  103. StreamUtils.readStreamIntoStringBuffer(myProcess.getInputStream(), data);
  104. try {
  105. myProcess.waitFor();
  106. } catch (InterruptedException e) {
  107. }
  108. return true;
  109. } catch (SecurityException | IOException e) {
  110. }
  111. return false;
  112. }
  113. /**
  114. * Prepare the string for sending to dbus.
  115. *
  116. * @param input Input string
  117. *
  118. * @return Input string after being processed according to config settings.
  119. */
  120. public String prepareString(final String input) {
  121. String output = input;
  122. if (stripcodes) {
  123. output = Styliser.stipControlCodes(output);
  124. }
  125. if (escapehtml) {
  126. output = HtmlEscapers.htmlEscaper().escape(output);
  127. }
  128. return output;
  129. }
  130. private void setCachedSettings() {
  131. timeout = config.getOptionInt(domain, "general.timeout");
  132. icon = config.getOption(domain, "general.icon");
  133. escapehtml = config.getOptionBool(domain, "advanced.escapehtml");
  134. stripcodes = config.getOptionBool(domain, "advanced.stripcodes");
  135. }
  136. @Override
  137. public void configChanged(final String domain, final String key) {
  138. setCachedSettings();
  139. }
  140. public void onLoad() {
  141. config.addChangeListener(domain, this);
  142. userConfig.setOption(domain, "general.icon", filesHelper.getFilesDirString() + "icon.png");
  143. setCachedSettings();
  144. // Extract the files needed
  145. try {
  146. filesHelper.extractResourcesEndingWith(".py");
  147. filesHelper.extractResourcesEndingWith(".png");
  148. } catch (IOException ex) {
  149. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
  150. "Unable to extract files for Free desktop notifications: " + ex.getMessage(),
  151. ""));
  152. }
  153. }
  154. public void onUnLoad() {
  155. config.removeListener(this);
  156. }
  157. }