您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DBusMediaSourceManager.java 5.9KB

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