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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.mediasource_dbus;
  18. import com.dmdirc.addons.nowplaying.MediaSource;
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import javax.inject.Inject;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import static com.dmdirc.util.LogUtils.USER_ERROR;
  32. /**
  33. * Provides a media source for DBUS players.
  34. */
  35. public class DBusMediaSourceManager {
  36. private static final Logger LOG = LoggerFactory.getLogger(DBusMediaSourceManager.class);
  37. /** A map of discovered mpris sources. */
  38. private final Map<String, MediaSource> mprisSources;
  39. /** The sources used by this media source. */
  40. private final List<MediaSource> sources;
  41. /** The path to qdbus. */
  42. private String qdbus;
  43. @Inject
  44. public DBusMediaSourceManager() {
  45. sources = new ArrayList<>();
  46. mprisSources = new HashMap<>();
  47. }
  48. /**
  49. * Called when the plugin is loaded to initialise settings.
  50. */
  51. public void onLoad() {
  52. if (new File("/usr/bin/qdbus").exists()) {
  53. qdbus = "/usr/bin/qdbus";
  54. } else if (new File("/bin/qdbus").exists()) {
  55. qdbus = "/bin/qdbus";
  56. }
  57. }
  58. /**
  59. * Called when the plugin is unloaded to uninitialise settings.
  60. */
  61. public void onUnload() {
  62. sources.clear();
  63. mprisSources.clear();
  64. }
  65. /**
  66. * Returns the source again for this plugin.
  67. *
  68. * @return List of available MPRIS media sources.
  69. */
  70. public List<MediaSource> getSources() {
  71. for (String mpris : doDBusCall("org.mpris.*", "/", "/")) {
  72. try {
  73. final String service = mpris.substring(10);
  74. if (!mprisSources.containsKey(service)) {
  75. mprisSources.put(service, new MPRISSource(this, service));
  76. sources.add(mprisSources.get(service));
  77. }
  78. } catch (IllegalArgumentException ex) {
  79. // The service either stopped after the initial call and before
  80. // we created an MRPIS Source, or otherwise doesn't correctly
  81. // implement MPRIS. Either way, ignore it.
  82. }
  83. }
  84. return Collections.unmodifiableList(sources);
  85. }
  86. /**
  87. * Performs a dbus call.
  88. *
  89. * @param service The name of the service
  90. * @param iface The name of the interface
  91. * @param method The name of the method
  92. * @param args Any arguments to the method
  93. *
  94. * @return A list of output (one entry per line)
  95. */
  96. public List<String> doDBusCall(final String service, final String iface,
  97. final String method, final String... args) {
  98. final String[] exeArgs = new String[4 + args.length];
  99. exeArgs[0] = qdbus;
  100. exeArgs[1] = service;
  101. exeArgs[2] = iface;
  102. exeArgs[3] = method;
  103. System.arraycopy(args, 0, exeArgs, 4, args.length);
  104. return getInfo(exeArgs);
  105. }
  106. /**
  107. * Executes the specified command and arguments and returns the results.
  108. *
  109. * @param args The command/arguments to be executed
  110. *
  111. * @return The output of the specified command
  112. */
  113. protected List<String> getInfo(final String... args) {
  114. final List<String> result = new ArrayList<>();
  115. Process process = null;
  116. try {
  117. process = Runtime.getRuntime().exec(args);
  118. try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
  119. BufferedReader input = new BufferedReader(reader)){
  120. String line;
  121. while ((line = input.readLine()) != null) {
  122. result.add(line);
  123. }
  124. }
  125. } catch (IOException ex) {
  126. LOG.error(USER_ERROR, "Unable to get DBUS info", ex);
  127. } finally {
  128. if (process != null) {
  129. process.destroy();
  130. }
  131. }
  132. return result;
  133. }
  134. /**
  135. * Parses a dbus dictionary into a {@link Map}.
  136. *
  137. * @param lines The lines to be parsed as a dictionary
  138. *
  139. * @return A map corresponding to the specified dictionary
  140. */
  141. protected Map<String, String> parseDictionary(final Iterable<String> lines) {
  142. final Map<String, String> res = new HashMap<>();
  143. for (String line : lines) {
  144. final int index = line.indexOf(':', line.indexOf(':') + 1);
  145. if (index == -1 || index >= line.length() - 2) {
  146. continue;
  147. }
  148. final String key = line.substring(0, index);
  149. final String value = line.substring(index + 2);
  150. res.put(key, value);
  151. }
  152. return res;
  153. }
  154. }