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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2006-2014 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_windows;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceState;
  25. import com.dmdirc.util.DateUtils;
  26. /**
  27. * Uses WindowsMediaSourcePlugin to retrieve now playing info.
  28. */
  29. public class DllSource implements MediaSource {
  30. /** Player name */
  31. private final String playerName;
  32. /** Use getArtistTitle */
  33. private final boolean useArtistTitle;
  34. /** Parent Plugin. */
  35. final WindowsMediaSourcePlugin parent;
  36. /**
  37. * Instantiates the media source.
  38. *
  39. * @param parent The plugin that owns this DllSource
  40. * @param playerName Name of Player and DLL
  41. */
  42. public DllSource(final WindowsMediaSourcePlugin parent, final String playerName) {
  43. this(parent, playerName, false);
  44. }
  45. /**
  46. * Instantiates the media source.
  47. *
  48. * @param parent The plugin that owns this DllSource
  49. * @param playerName Name of Player and DLL
  50. * @param useArtistTitle True if getArtistTitle should be parsed rather than using getArtist()
  51. * and getTitle()
  52. */
  53. public DllSource(final WindowsMediaSourcePlugin parent, final String playerName,
  54. final boolean useArtistTitle) {
  55. this.playerName = playerName;
  56. this.useArtistTitle = useArtistTitle;
  57. this.parent = parent;
  58. }
  59. @Override
  60. public String getAppName() {
  61. return playerName;
  62. }
  63. /**
  64. * Get the "goodoutput" from GetMediaInfo for the given command
  65. *
  66. * @param command Command to run
  67. *
  68. * @return "Good" Output
  69. */
  70. private String getOutput(final String command) {
  71. return parent.getOutput(playerName, command).getGoodOutput();
  72. }
  73. @Override
  74. public MediaSourceState getState() {
  75. final MediaInfoOutput result = parent.getOutput(playerName, "getPlayState");
  76. if (result.getExitCode() == 0) {
  77. final String output = result.getGoodOutput();
  78. if ("stopped".equalsIgnoreCase(output)) {
  79. return MediaSourceState.STOPPED;
  80. } else if ("playing".equalsIgnoreCase(output)) {
  81. return MediaSourceState.PLAYING;
  82. } else if ("paused".equalsIgnoreCase(output)) {
  83. return MediaSourceState.PAUSED;
  84. } else {
  85. return MediaSourceState.NOTKNOWN;
  86. }
  87. } else {
  88. return MediaSourceState.CLOSED;
  89. }
  90. }
  91. @Override
  92. public String getArtist() {
  93. if (useArtistTitle) {
  94. return getOutput("getArtistTitle").split("\\s-\\s", 2)[0];
  95. } else {
  96. return getOutput("getArtist");
  97. }
  98. }
  99. @Override
  100. public String getTitle() {
  101. if (useArtistTitle) {
  102. final String[] bits = getOutput("getArtistTitle").split("\\s-\\s", 2);
  103. return bits.length > 1 ? bits[1] : "";
  104. } else {
  105. return getOutput("getTitle");
  106. }
  107. }
  108. @Override
  109. public String getAlbum() {
  110. return getOutput("getAlbum");
  111. }
  112. @Override
  113. public String getLength() {
  114. try {
  115. final int seconds = Integer.parseInt(getOutput("getLength"));
  116. return DateUtils.formatDurationAsTime(seconds);
  117. } catch (NumberFormatException nfe) {
  118. }
  119. return "Unknown";
  120. }
  121. @Override
  122. public String getTime() {
  123. try {
  124. final int seconds = Integer.parseInt(getOutput("getTime"));
  125. return DateUtils.formatDurationAsTime(seconds);
  126. } catch (NumberFormatException nfe) {
  127. }
  128. return "Unknown";
  129. }
  130. @Override
  131. public String getFormat() {
  132. return getOutput("getFormat");
  133. }
  134. @Override
  135. public String getBitrate() {
  136. return getOutput("getBitrate");
  137. }
  138. }