Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DBusMediaSource.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.mediasource_dbus;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceManager;
  25. import com.dmdirc.plugins.Plugin;
  26. import java.io.BufferedReader;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.io.InputStreamReader;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. /**
  36. * Provides a media source for dbus players.
  37. */
  38. public class DBusMediaSource extends Plugin implements MediaSourceManager {
  39. /** The sources used by this media source. */
  40. private List<MediaSource> sources;
  41. /** A map of discovered mpris sources. */
  42. private final Map<String, MediaSource> mprisSources = new HashMap<String, MediaSource>();
  43. /** The path to qdbus. */
  44. private String qdbus;
  45. /**
  46. * Creates a new instance of DBusMediaSource.
  47. */
  48. public DBusMediaSource() {
  49. super();
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public void onLoad() {
  54. sources = new ArrayList<MediaSource>(Arrays.asList(new MediaSource[]{
  55. new BansheeSource(this),
  56. }));
  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. /** {@inheritDoc} */
  64. @Override
  65. public void onUnload() {
  66. // Do nothing
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  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 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. * @return A list of output (one entry per line)
  94. */
  95. public List<String> doDBusCall(final String service, final String iface,
  96. final String method, final String ... args) {
  97. final String[] exeArgs = new String[4 + args.length];
  98. exeArgs[0] = qdbus;
  99. exeArgs[1] = service;
  100. exeArgs[2] = iface;
  101. exeArgs[3] = method;
  102. for (int i = 0; i < args.length; i++) {
  103. exeArgs[4 + i] = args[i];
  104. }
  105. return getInfo(exeArgs);
  106. }
  107. /**
  108. * Executes the specified command and arguments and returns the results.
  109. *
  110. * @param args The command/arguments to be executed
  111. * @return The output of the specified command
  112. */
  113. protected static List<String> getInfo(final String[] args) {
  114. final ArrayList<String> result = new ArrayList<String>();
  115. InputStreamReader reader;
  116. BufferedReader input;
  117. Process process;
  118. try {
  119. process = Runtime.getRuntime().exec(args);
  120. reader = new InputStreamReader(process.getInputStream());
  121. input = new BufferedReader(reader);
  122. String line = "";
  123. while ((line = input.readLine()) != null) {
  124. result.add(line);
  125. }
  126. reader.close();
  127. input.close();
  128. process.destroy();
  129. } catch (IOException ex) {
  130. ex.printStackTrace();
  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. * @return A map corresponding to the specified dictionary
  139. */
  140. protected static Map<String, String> parseDictionary(final List<String> lines) {
  141. final Map<String, String> res = new HashMap<String, String>();
  142. for (String line : lines) {
  143. final int index = line.indexOf(':');
  144. if (index == -1 || index >= line.length() - 2) {
  145. continue;
  146. }
  147. final String key = line.substring(0, index);
  148. final String value = line.substring(index + 2);
  149. res.put(key, value);
  150. }
  151. return res;
  152. }
  153. }