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.

DBusMediaSourceManager.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.mediasource_dbus;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.addons.nowplaying.MediaSource;
  25. import com.dmdirc.events.UserErrorEvent;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import java.io.BufferedReader;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.io.InputStreamReader;
  31. import java.util.ArrayList;
  32. import java.util.Collections;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import javax.inject.Inject;
  37. /**
  38. * Provides a media source for DBUS players.
  39. */
  40. public class DBusMediaSourceManager {
  41. /** A map of discovered mpris sources. */
  42. private final Map<String, MediaSource> mprisSources;
  43. /** The event bus to post errors to. */
  44. private final DMDircMBassador eventBus;
  45. /** The sources used by this media source. */
  46. private final List<MediaSource> sources;
  47. /** The path to qdbus. */
  48. private String qdbus;
  49. @Inject
  50. public DBusMediaSourceManager(final DMDircMBassador eventBus) {
  51. this.eventBus = eventBus;
  52. sources = new ArrayList<>();
  53. mprisSources = new HashMap<>();
  54. }
  55. /**
  56. * Called when the plugin is loaded to initialise settings.
  57. */
  58. public void onLoad() {
  59. if (new File("/usr/bin/qdbus").exists()) {
  60. qdbus = "/usr/bin/qdbus";
  61. } else if (new File("/bin/qdbus").exists()) {
  62. qdbus = "/bin/qdbus";
  63. }
  64. }
  65. /**
  66. * Called when the plugin is unloaded to uninitialise settings.
  67. */
  68. public void onUnload() {
  69. sources.clear();
  70. mprisSources.clear();
  71. }
  72. /**
  73. * Returns the source again for this plugin.
  74. *
  75. * @return List of available MPRIS media sources.
  76. */
  77. public List<MediaSource> getSources() {
  78. for (String mpris : doDBusCall("org.mpris.*", "/", "/")) {
  79. try {
  80. final String service = mpris.substring(10);
  81. if (!mprisSources.containsKey(service)) {
  82. mprisSources.put(service, new MPRISSource(this, service));
  83. sources.add(mprisSources.get(service));
  84. }
  85. } catch (IllegalArgumentException ex) {
  86. // The service either stopped after the initial call and before
  87. // we created an MRPIS Source, or otherwise doesn't correctly
  88. // implement MPRIS. Either way, ignore it.
  89. }
  90. }
  91. return Collections.unmodifiableList(sources);
  92. }
  93. /**
  94. * Performs a dbus call.
  95. *
  96. * @param service The name of the service
  97. * @param iface The name of the interface
  98. * @param method The name of the method
  99. * @param args Any arguments to the method
  100. *
  101. * @return A list of output (one entry per line)
  102. */
  103. public List<String> doDBusCall(final String service, final String iface,
  104. final String method, final String... args) {
  105. final String[] exeArgs = new String[4 + args.length];
  106. exeArgs[0] = qdbus;
  107. exeArgs[1] = service;
  108. exeArgs[2] = iface;
  109. exeArgs[3] = method;
  110. System.arraycopy(args, 0, exeArgs, 4, args.length);
  111. return getInfo(exeArgs);
  112. }
  113. /**
  114. * Executes the specified command and arguments and returns the results.
  115. *
  116. * @param args The command/arguments to be executed
  117. *
  118. * @return The output of the specified command
  119. */
  120. protected List<String> getInfo(final String... args) {
  121. final List<String> result = new ArrayList<>();
  122. Process process = null;
  123. try {
  124. process = Runtime.getRuntime().exec(args);
  125. try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
  126. BufferedReader input = new BufferedReader(reader)){
  127. String line;
  128. while ((line = input.readLine()) != null) {
  129. result.add(line);
  130. }
  131. }
  132. } catch (IOException ex) {
  133. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.HIGH, ex, "Unable to get DBUS info", ""));
  134. } finally {
  135. if (process != null) {
  136. process.destroy();
  137. }
  138. }
  139. return result;
  140. }
  141. /**
  142. * Parses a dbus dictionary into a {@link Map}.
  143. *
  144. * @param lines The lines to be parsed as a dictionary
  145. *
  146. * @return A map corresponding to the specified dictionary
  147. */
  148. protected Map<String, String> parseDictionary(final Iterable<String> lines) {
  149. final Map<String, String> res = new HashMap<>();
  150. for (String line : lines) {
  151. final int index = line.indexOf(':', line.indexOf(':') + 1);
  152. if (index == -1 || index >= line.length() - 2) {
  153. continue;
  154. }
  155. final String key = line.substring(0, index);
  156. final String value = line.substring(index + 2);
  157. res.put(key, value);
  158. }
  159. return res;
  160. }
  161. }