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.

WindowsMediaSourcePlugin.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_windows;
  23. import com.dmdirc.addons.nowplaying.MediaSource;
  24. import com.dmdirc.addons.nowplaying.MediaSourceManager;
  25. import com.dmdirc.installer.StreamReader;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.logger.Logger;
  28. import com.dmdirc.plugins.Plugin;
  29. import com.dmdirc.plugins.PluginInfo;
  30. import com.dmdirc.plugins.PluginManager;
  31. import com.dmdirc.util.resourcemanager.ResourceManager;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. /**
  36. * Manages all Windows based media sources.
  37. */
  38. public class WindowsMediaSourcePlugin extends Plugin implements MediaSourceManager {
  39. /** Media sources. */
  40. private final List<MediaSource> sources;
  41. /**
  42. * Creates a new instance of DcopMediaSourcePlugin.
  43. */
  44. public WindowsMediaSourcePlugin() {
  45. super();
  46. sources = new ArrayList<MediaSource>();
  47. sources.add(new DllSource(this, "Winamp", true));
  48. sources.add(new DllSource(this, "iTunes", false));
  49. }
  50. /** {@inheritDoc} */
  51. @Override
  52. public List<MediaSource> getSources() {
  53. return sources;
  54. }
  55. /**
  56. * Get the output from GetMediaInfo.exe for the given player and method
  57. *
  58. * @param player Player to ask about
  59. * @param method Method to call
  60. * @return a MediaInfoOutput with the results
  61. */
  62. protected MediaInfoOutput getOutput(final String player, final String method) {
  63. try {
  64. final Process myProcess = Runtime.getRuntime().exec(new String[]{getFilesDirString() + "GetMediaInfo.exe", player, method});
  65. final StringBuffer data = new StringBuffer();
  66. new StreamReader(myProcess.getErrorStream()).start();
  67. new StreamReader(myProcess.getInputStream(), data).start();
  68. try {
  69. myProcess.waitFor();
  70. } catch (InterruptedException e) {
  71. }
  72. return new MediaInfoOutput(myProcess.exitValue(), data.toString());
  73. } catch (SecurityException e) {
  74. } catch (IOException e) {
  75. }
  76. return new MediaInfoOutput(-1, "Error executing GetMediaInfo.exe");
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public void onLoad() {
  81. // Extract GetMediaInfo.exe and required DLLs
  82. final PluginInfo pi = PluginManager.getPluginManager().getPluginInfoByName("windowsmediasource");
  83. // This shouldn't actually happen, but check to make sure.
  84. if (pi == null) {
  85. return;
  86. }
  87. // Now get the RM
  88. try {
  89. final ResourceManager res = pi.getResourceManager();
  90. // Extract the .dlls and .exe
  91. try {
  92. res.extractResoucesEndingWith(getFilesDir(), ".dll");
  93. res.extractResoucesEndingWith(getFilesDir(), ".exe");
  94. } catch (IOException ex) {
  95. Logger.userError(ErrorLevel.MEDIUM, "Unable to extract files for windows media source: " + ex.getMessage(), ex);
  96. }
  97. } catch (IOException ioe) {
  98. Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for windowsmediasource: " + ioe.getMessage(), ioe);
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public void onUnload() { /* Do Nothing */ }
  104. }