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.

PluginFilesHelper.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.plugins.implementations;
  23. import com.dmdirc.plugins.PluginInfo;
  24. import com.dmdirc.plugins.PluginMetaData;
  25. import java.io.File;
  26. import java.io.IOException;
  27. /**
  28. * Helper file to manage plugins that need extracted files on disk.
  29. */
  30. public class PluginFilesHelper {
  31. /** This plugins meta data object. */
  32. private final PluginMetaData metaData;
  33. /** This plugins information object. */
  34. private final PluginInfo pluginInfo;
  35. /** This plugins files directory. */
  36. private File filesDir;
  37. /**
  38. * Creates a new instance of this helper
  39. *
  40. * @param pluginInfo This plugin's information object
  41. */
  42. public PluginFilesHelper(final PluginInfo pluginInfo) {
  43. super();
  44. this.pluginInfo = pluginInfo;
  45. this.metaData = pluginInfo.getMetaData();
  46. filesDir = initFilesDir();
  47. }
  48. public File getFilesDir() {
  49. return filesDir;
  50. }
  51. /**
  52. * Initialises the files directory for this plugin. This will attempt to create the directory if
  53. * it doesn't exist the first time the directory name is requested.
  54. *
  55. * @return Files directory for this plugin.
  56. */
  57. private File initFilesDir() {
  58. if (filesDir == null) {
  59. final String fs = System.getProperty("file.separator");
  60. final String dir = metaData.getManager().getFilesDirectory();
  61. filesDir = new File(dir + metaData.getName() + fs);
  62. if (!filesDir.exists()) {
  63. filesDir.mkdirs();
  64. }
  65. }
  66. return filesDir;
  67. }
  68. /**
  69. * Returns the path of the directory this plugin should use for files it requires extracted.
  70. *
  71. * @return File directory as a string
  72. */
  73. public String getFilesDirString() {
  74. return getFilesDir().getAbsolutePath()
  75. + System.getProperty("file.separator");
  76. }
  77. /**
  78. * Extracts the specified resource to the specified directory.
  79. *
  80. * @param resourceName The name of the resource to extract
  81. *
  82. * @throws IOException if the write operation fails
  83. */
  84. public void extractResource(final String resourceName) throws IOException {
  85. pluginInfo.getResourceManager().extractResource(resourceName, filesDir.toString(), true);
  86. }
  87. /**
  88. * Extracts files starting with the given prefix to the files directory.
  89. *
  90. * @param prefix Prefix to extract
  91. *
  92. * @throws IOException if the resources failed to extract
  93. */
  94. public void extractResourcesStartingWith(final String prefix) throws IOException {
  95. pluginInfo.getResourceManager().extractResources(prefix, filesDir.toString(), true);
  96. }
  97. /**
  98. * Extracts files ending with the given suffix to the files directory.
  99. *
  100. * @param suffix Suffix to extract
  101. *
  102. * @throws IOException if the resources failed to extract
  103. */
  104. public void extractResourcesEndingWith(final String suffix) throws IOException {
  105. pluginInfo.getResourceManager().extractResourcesEndingWith(filesDir, suffix);
  106. }
  107. }