zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

PresetsStore.cpp (4149B)


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