Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.MediaSourceState;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * A media source for anything that's compatible with MRPIS.
  29. *
  30. * @author chris
  31. */
  32. public class MPRISSource implements MediaSource {
  33. /** The media source manager. */
  34. private final DBusMediaSource source;
  35. /** The service name. */
  36. private final String service;
  37. /** The name of the source. */
  38. private final String name;
  39. /** A temporary cache of track data. */
  40. private Map<String, String> data;
  41. /**
  42. * Creates a new MPRIS source for the specified service name.
  43. *
  44. * @param source The manager which owns this source
  45. * @param service The service name of the MRPIS service
  46. */
  47. public MPRISSource(final DBusMediaSource source, final String service) {
  48. this.source = source;
  49. this.service = service;
  50. final List<String> info = source.doDBusCall("org.mpris." + service, "/",
  51. "org.freedesktop.MediaPlayer.Identity");
  52. if (info.isEmpty()) {
  53. throw new IllegalArgumentException("No service with that name found");
  54. }
  55. this.name = info.get(0).replace(' ', '_');
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public MediaSourceState getState() {
  60. final char[] status = getStatus();
  61. if (status == null) {
  62. data = null;
  63. return MediaSourceState.CLOSED;
  64. }
  65. data = getTrackInfo();
  66. if (status[0] == '0') {
  67. return MediaSourceState.PLAYING;
  68. } else if (status[0] == '1') {
  69. return MediaSourceState.PAUSED;
  70. } else if (status[0] == '2') {
  71. return MediaSourceState.STOPPED;
  72. } else {
  73. return MediaSourceState.NOTKNOWN;
  74. }
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. public String getAppName() {
  79. return name;
  80. }
  81. /**
  82. * Utility method to return the value of the specified key if it exists,
  83. * or "Unknown" if it doesn't.
  84. *
  85. * @param key The key to be retrieved
  86. * @return The value of the specified key or "Unknown".
  87. */
  88. protected String getData(final String key) {
  89. return data == null || !data.containsKey(key) || data.get(key) == null
  90. ? "Unknown" : data.get(key);
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public String getArtist() {
  95. return getData("artist");
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public String getTitle() {
  100. return getData("title");
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public String getAlbum() {
  105. return getData("album");
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public String getLength() {
  110. return getData("time");
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public String getTime() {
  115. return "Unknown";
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public String getFormat() {
  120. return "Unknown";
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public String getBitrate() {
  125. return getData("audio-bitrate");
  126. }
  127. /**
  128. * Retrieves a map of track information from the service.
  129. *
  130. * @return A map of metadata returned by the MPRIS service
  131. */
  132. protected Map<String, String> getTrackInfo() {
  133. // If only there were a standard...
  134. final List<String> list = source.doDBusCall("org.mpris." + service,
  135. "/Player", "org.freedesktop.MediaPlayer.GetMetadata");
  136. list.addAll(source.doDBusCall("org.mpris." + service,
  137. "/Player", "org.freedesktop.MediaPlayer.GetMetaData"));
  138. return DBusMediaSource.parseDictionary(list);
  139. }
  140. /**
  141. * Retrieves the 'status' result from the MPRIS service.
  142. *
  143. * @return The returned status or null if the service isn't running
  144. */
  145. protected char[] getStatus() {
  146. if (source.doDBusCall("org.mpris." + service, "/",
  147. "org.freedesktop.MediaPlayer.Identity").isEmpty()) {
  148. // Calling dbus-send can seemingly start applications that have
  149. // quit (not entirely sure how), so check that it's running first.
  150. return null;
  151. }
  152. final List<String> res = DBusMediaSource.getInfo(new String[]{
  153. "/usr/bin/dbus-send", "--print-reply", "--dest=org.mpris." + service,
  154. "/Player", "org.freedesktop.MediaPlayer.GetStatus"
  155. });
  156. if (res.isEmpty()) {
  157. return null;
  158. }
  159. final char[] result = new char[4];
  160. int i = 0;
  161. for (String line : res) {
  162. final String tline = line.trim();
  163. if (tline.startsWith("int32")) {
  164. result[i++] = tline.charAt(tline.length() - 1);
  165. }
  166. }
  167. return result;
  168. }
  169. }