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.

DBusMediaSource.java 5.6KB

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