Audio plugin host https://kx.studio/carla
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.

193 lines
4.8KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. PresetsStore.cpp - Presets and Clipboard store
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <iostream>
  18. #include <algorithm>
  19. #include <cctype>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <dirent.h>
  23. #include <sys/stat.h>
  24. #include "PresetsStore.h"
  25. #include "../Misc/XMLwrapper.h"
  26. #include "../Misc/Util.h"
  27. #include "../Misc/Config.h"
  28. using namespace std;
  29. //XXX to remove
  30. //PresetsStore presetsstore;
  31. PresetsStore::PresetsStore(const Config& config) : config(config)
  32. {
  33. }
  34. PresetsStore::~PresetsStore()
  35. {
  36. }
  37. //Clipboard management
  38. void PresetsStore::copyclipboard(XMLwrapper &xml, char *type)
  39. {
  40. clipboard.type = type;
  41. const char *tmp = xml.getXMLdata();
  42. clipboard.data = tmp;
  43. free((void*)tmp);
  44. }
  45. bool PresetsStore::pasteclipboard(XMLwrapper &xml)
  46. {
  47. if(clipboard.data.empty())
  48. return false;
  49. xml.putXMLdata(clipboard.data.c_str());
  50. return true;
  51. }
  52. bool PresetsStore::checkclipboardtype(const char *type)
  53. {
  54. //makes LFO's compatible
  55. if(strstr(type, "Plfo") && strstr(clipboard.type.c_str(), "Plfo"))
  56. return true;
  57. return type == clipboard.type;
  58. }
  59. //Presets management
  60. void PresetsStore::clearpresets()
  61. {
  62. presets.clear();
  63. }
  64. //a helper function that compares 2 presets[]
  65. bool PresetsStore::presetstruct::operator<(const presetstruct &b) const
  66. {
  67. return name < b.name;
  68. }
  69. void PresetsStore::scanforpresets()
  70. {
  71. clearpresets();
  72. string ftype = ".xpz";
  73. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i) {
  74. if(config.cfg.presetsDirList[i].empty())
  75. continue;
  76. //open directory
  77. string dirname = config.cfg.presetsDirList[i];
  78. DIR *dir = opendir(dirname.c_str());
  79. if(dir == NULL)
  80. continue;
  81. struct dirent *fn;
  82. //check all files in directory
  83. while((fn = readdir(dir))) {
  84. string filename = fn->d_name;
  85. if(filename.find(ftype) == string::npos)
  86. continue;
  87. //ensure proper path is formed
  88. char tmpc = dirname[dirname.size() - 1];
  89. const char *tmps;
  90. if((tmpc == '/') || (tmpc == '\\'))
  91. tmps = "";
  92. else
  93. tmps = "/";
  94. string location = "" + dirname + tmps + filename;
  95. //trim file type off of name
  96. string name_type = filename.substr(0, filename.find(ftype));
  97. size_t tmp = name_type.find_last_of(".");
  98. if(tmp == string::npos)
  99. continue;
  100. string type = name_type.substr(tmp+1);
  101. string name = name_type.substr(0, tmp);
  102. //put on list
  103. presets.push_back(presetstruct{location, name, type});
  104. }
  105. closedir(dir);
  106. }
  107. //sort the presets
  108. sort(presets.begin(), presets.end());
  109. }
  110. void PresetsStore::copypreset(XMLwrapper &xml, char *type, string name)
  111. {
  112. if(config.cfg.presetsDirList[0].empty())
  113. return;
  114. //make the filenames legal
  115. name = legalizeFilename(name);
  116. //make path legal
  117. const string dirname = config.cfg.presetsDirList[0];
  118. char tmpc = dirname[dirname.size() - 1];
  119. const char *tmps;
  120. if((tmpc == '/') || (tmpc == '\\'))
  121. tmps = "";
  122. else
  123. tmps = "/";
  124. string filename("" + dirname + tmps + name + "." + &type[1] + ".xpz");
  125. xml.saveXMLfile(filename, config.cfg.GzipCompression);
  126. }
  127. bool PresetsStore::pastepreset(XMLwrapper &xml, unsigned int npreset)
  128. {
  129. npreset--;
  130. if(npreset >= presets.size())
  131. return false;
  132. string filename = presets[npreset].file;
  133. if(filename.empty())
  134. return false;
  135. return xml.loadXMLfile(filename) >= 0;
  136. }
  137. void PresetsStore::deletepreset(unsigned int npreset)
  138. {
  139. npreset--;
  140. if(npreset >= presets.size())
  141. return;
  142. string filename = presets[npreset].file;
  143. if(filename.empty())
  144. return;
  145. remove(filename.c_str());
  146. }
  147. void PresetsStore::deletepreset(std::string filename)
  148. {
  149. for(int i=0; i<(int)presets.size(); ++i) {
  150. if(presets[i].file == filename) {
  151. presets.erase(presets.begin()+i);
  152. remove(filename.c_str());
  153. return;
  154. }
  155. }
  156. }