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.

184 lines
4.4KB

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