Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DBusMediaSource.java 5.5KB

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