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.

185 lines
4.6KB

  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/Util.h"
  26. using namespace std;
  27. PresetsStore presetsstore;
  28. PresetsStore::PresetsStore()
  29. {
  30. clipboard.data = NULL;
  31. clipboard.type[0] = 0;
  32. }
  33. PresetsStore::~PresetsStore()
  34. {
  35. if(clipboard.data != NULL)
  36. free(clipboard.data);
  37. clearpresets();
  38. }
  39. //Clipboard management
  40. void PresetsStore::copyclipboard(XMLwrapper *xml, char *type)
  41. {
  42. strcpy(clipboard.type, type);
  43. if(clipboard.data != NULL)
  44. free(clipboard.data);
  45. clipboard.data = xml->getXMLdata();
  46. }
  47. bool PresetsStore::pasteclipboard(XMLwrapper *xml)
  48. {
  49. if(clipboard.data != NULL)
  50. xml->putXMLdata(clipboard.data);
  51. else
  52. return false;
  53. return true;
  54. }
  55. bool PresetsStore::checkclipboardtype(const char *type)
  56. {
  57. //makes LFO's compatible
  58. if((strstr(type,
  59. "Plfo") != NULL) && (strstr(clipboard.type, "Plfo") != NULL))
  60. return true;
  61. return strcmp(type, clipboard.type) == 0;
  62. }
  63. //Presets management
  64. void PresetsStore::clearpresets()
  65. {
  66. presets.clear();
  67. }
  68. //a helper function that compares 2 presets[]
  69. bool PresetsStore::presetstruct::operator<(const presetstruct &b) const
  70. {
  71. return name < b.name;
  72. }
  73. void PresetsStore::rescanforpresets(const string &type)
  74. {
  75. //std::cout << "Scanning For Presets" << std::endl;
  76. //std::cout << "Of Type: " << type << std::endl;
  77. clearpresets();
  78. string ftype = "." + type.substr(1) + ".xpz";
  79. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i) {
  80. if(config.cfg.presetsDirList[i].empty())
  81. continue;
  82. //open directory
  83. string dirname = config.cfg.presetsDirList[i];
  84. DIR *dir = opendir(dirname.c_str());
  85. if(dir == NULL)
  86. continue;
  87. struct dirent *fn;
  88. //check all files in directory
  89. while((fn = readdir(dir))) {
  90. string filename = fn->d_name;
  91. if(filename.find(ftype) == string::npos)
  92. continue;
  93. //ensure proper path is formed
  94. char tmpc = dirname[dirname.size() - 1];
  95. const char *tmps;
  96. if((tmpc == '/') || (tmpc == '\\'))
  97. tmps = "";
  98. else
  99. tmps = "/";
  100. string location = "" + dirname + tmps + filename;
  101. //trim file type off of name
  102. string name = filename.substr(0, filename.find(ftype));
  103. //put on list
  104. presets.push_back(presetstruct(location, name));
  105. }
  106. closedir(dir);
  107. }
  108. //sort the presets
  109. sort(presets.begin(), presets.end());
  110. }
  111. void PresetsStore::copypreset(XMLwrapper *xml, char *type, string name)
  112. {
  113. if(config.cfg.presetsDirList[0].empty())
  114. return;
  115. //make the filenames legal
  116. name = legalizeFilename(name);
  117. //make path legal
  118. const string dirname = config.cfg.presetsDirList[0];
  119. char tmpc = dirname[dirname.size() - 1];
  120. const char *tmps;
  121. if((tmpc == '/') || (tmpc == '\\'))
  122. tmps = "";
  123. else
  124. tmps = "/";
  125. string filename("" + dirname + tmps + name + "." + &type[1] + ".xpz");
  126. xml->saveXMLfile(filename);
  127. }
  128. bool PresetsStore::pastepreset(XMLwrapper *xml, unsigned int npreset)
  129. {
  130. npreset--;
  131. if(npreset >= presets.size())
  132. return false;
  133. string filename = presets[npreset].file;
  134. if(filename.empty())
  135. return false;
  136. bool result = (xml->loadXMLfile(filename) >= 0);
  137. return result;
  138. }
  139. void PresetsStore::deletepreset(unsigned int npreset)
  140. {
  141. npreset--;
  142. if(npreset >= presets.size())
  143. return;
  144. string filename = presets[npreset].file;
  145. if(filename.empty())
  146. return;
  147. remove(filename.c_str());
  148. }