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.

BansheeSource.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.MediaSourceState;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * A media source for Banshee.
  29. *
  30. * @author chris
  31. */
  32. public class BansheeSource implements MediaSource {
  33. /** The dbus service name. */
  34. private static final String SERVICE = "org.bansheeproject.Banshee";
  35. /** The dbus interface name. */
  36. private static final String IFACE = "/org/bansheeproject/Banshee/PlayerEngine";
  37. /** The method to get the current state. */
  38. private static final String STATE = "org.bansheeproject.Banshee.PlayerEngine.GetCurrentState";
  39. /** The method to get the current track. */
  40. private static final String TRACK = "org.bansheeproject.Banshee.PlayerEngine.GetCurrentTrack";
  41. /** The method to get the current position. */
  42. private static final String POSITION = "org.bansheeproject.Banshee.PlayerEngine.GetPosition";
  43. /** The media source that owns this source. */
  44. private final DBusMediaSource source;
  45. /** A cache of track information. */
  46. private Map<String, String> trackInfo;
  47. public BansheeSource(final DBusMediaSource source) {
  48. this.source = source;
  49. }
  50. /** {@inheritDoc} */
  51. @Override
  52. public MediaSourceState getState() {
  53. final List<String> res = source.doDBusCall(SERVICE, IFACE, STATE);
  54. if (res.isEmpty()) {
  55. trackInfo = null;
  56. return MediaSourceState.CLOSED;
  57. } else if ("playing".equals(res.get(0))) {
  58. trackInfo = getTrackInfo();
  59. return MediaSourceState.PLAYING;
  60. } else if ("paused".equals(res.get(0))) {
  61. trackInfo = getTrackInfo();
  62. return MediaSourceState.PAUSED;
  63. } else if ("idle".equals(res.get(0))) {
  64. trackInfo = getTrackInfo();
  65. return MediaSourceState.STOPPED;
  66. } else {
  67. trackInfo = null;
  68. return MediaSourceState.NOTKNOWN;
  69. }
  70. }
  71. /**
  72. * Retrieves a map of track information from the dbus service.
  73. *
  74. * @return A map of track information
  75. */
  76. protected Map<String, String> getTrackInfo() {
  77. return DBusMediaSource.parseDictionary(source.doDBusCall(SERVICE, IFACE, TRACK));
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public String getAppName() {
  82. return "Banshee";
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public String getArtist() {
  87. return trackInfo == null ? "Unknown" : trackInfo.get("artist");
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public String getTitle() {
  92. return trackInfo == null ? "Unknown" : trackInfo.get("name");
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public String getAlbum() {
  97. return trackInfo == null ? "Unknown" : trackInfo.get("album");
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public String getLength() {
  102. return trackInfo == null ? "Unknown" : duration((long)
  103. Double.parseDouble(trackInfo.get("length")));
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public String getTime() {
  108. return duration((long) Double.parseDouble(source.doDBusCall(SERVICE,
  109. IFACE, POSITION).get(0)) / 1000);
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public String getFormat() {
  114. return trackInfo == null ? "Unknown" : trackInfo.get("mime-type");
  115. }
  116. /** {@inheritDoc} */
  117. @Override
  118. public String getBitrate() {
  119. return trackInfo == null ? "Unknown" : trackInfo.get("bit-rate");
  120. }
  121. /**
  122. * Get the duration in seconds as a string.
  123. *
  124. * @param seconds Input to get duration for
  125. * @return Duration as a string
  126. */
  127. private String duration(final long secondsInput) {
  128. final StringBuilder result = new StringBuilder();
  129. final long hours = secondsInput / 3600;
  130. final long minutes = secondsInput / 60 % 60;
  131. final long seconds = secondsInput % 60;
  132. if (hours > 0) {
  133. if (hours < 10) {
  134. result.append('0');
  135. }
  136. result.append(hours).append(":");
  137. }
  138. if (minutes < 10) {
  139. result.append('0');
  140. }
  141. result.append(minutes).append(":");
  142. if (seconds < 10) {
  143. result.append('0');
  144. }
  145. result.append(seconds);
  146. return result.toString();
  147. }
  148. }