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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_windows;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.addons.nowplaying.MediaSource;
  25. import com.dmdirc.addons.nowplaying.MediaSourceManager;
  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.util.ArrayList;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Map.Entry;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import com.dmdirc.installer.StreamReader;
  39. /**
  40. * Manages all Windows based media sources.
  41. */
  42. public class WindowsMediaSourcePlugin extends Plugin implements MediaSourceManager {
  43. /** Media sources. */
  44. private final List<MediaSource> sources;
  45. /** Files dir */
  46. private static final String filesDir = Main.getConfigDir() + "plugins/windowsmediasource_files/";
  47. /**
  48. * Creates a new instance of DcopMediaSourcePlugin.
  49. */
  50. public WindowsMediaSourcePlugin() {
  51. super();
  52. sources = new ArrayList<MediaSource>();
  53. sources.add(new DllSource("Winamp", true));
  54. sources.add(new DllSource("iTunes", false));
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. public List<MediaSource> getSources() {
  59. return sources;
  60. }
  61. /**
  62. * Get the output from GetMediaInfo.exe for the given player and method
  63. *
  64. * @param player Player to ask about
  65. * @param method Method to call
  66. * @return a MediaInfoOutput with the results
  67. */
  68. protected static MediaInfoOutput getOutput(final String player, final String method) {
  69. try {
  70. final Process myProcess = Runtime.getRuntime().exec(new String[]{filesDir + "GetMediaInfo.exe", player, method});
  71. final StringBuffer data = new StringBuffer();
  72. new StreamReader(myProcess.getErrorStream()).start();
  73. new StreamReader(myProcess.getInputStream(), data).start();
  74. try {
  75. myProcess.waitFor();
  76. } catch (InterruptedException e) {
  77. }
  78. return new MediaInfoOutput(myProcess.exitValue(), data.toString());
  79. } catch (SecurityException e) {
  80. } catch (IOException e) {
  81. }
  82. return new MediaInfoOutput(-1, "Error executing GetMediaInfo.exe");
  83. }
  84. /**
  85. * Use the given resource manager to extract files ending with the given suffix
  86. *
  87. * @param res ResourceManager
  88. * @param newDir Directory to extract to.
  89. * @param suffix Suffix to extract
  90. */
  91. private void extractFiles(final ResourceManager res, final File newDir, final String suffix) {
  92. final Map<String, byte[]> resources = res.getResourcesEndingWithAsBytes(suffix);
  93. for (Entry<String, byte[]> resource : resources.entrySet()) {
  94. try {
  95. final String key = resource.getKey();
  96. final String resourceName = key.substring(key.lastIndexOf('/'), key.length());
  97. final File newFile = new File(newDir, resourceName);
  98. if (!newFile.isDirectory()) {
  99. if (newFile.exists()) {
  100. newFile.delete();
  101. }
  102. ResourceManager.getResourceManager().resourceToFile(resource.getValue(), newFile);
  103. }
  104. } catch (IOException ex) {
  105. Logger.userError(ErrorLevel.LOW, "Failed to extract " + suffix + "s for windowsmediasource: " + ex.getMessage(), ex);
  106. }
  107. }
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public void onLoad() {
  112. // Extract GetMediaInfo.exe and required DLLs
  113. final PluginInfo pi = PluginManager.getPluginManager().getPluginInfoByName("windowsmediasource");
  114. // This shouldn't actually happen, but check to make sure.
  115. if (pi == null) {
  116. return;
  117. }
  118. // Now get the RM
  119. try {
  120. final ResourceManager res = pi.getResourceManager();
  121. // Make sure our files dir exists
  122. final File newDir = new File(filesDir);
  123. if (!newDir.exists()) {
  124. newDir.mkdirs();
  125. }
  126. // Now extract the .dlls and .exe
  127. extractFiles(res, newDir, ".dll");
  128. extractFiles(res, newDir, ".exe");
  129. } catch (IOException ioe) {
  130. Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for windowsmediasource: " + ioe.getMessage(), ioe);
  131. }
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public void onUnload() { /* Do Nothing */ }
  136. }