#include "bcsignals.h"
#include "bcwindowbase.inc"
-#include "cstrdup.h"
#include "file.h"
#include "filesystem.h"
#include "filexml.h"
#include <errno.h>
#include <string.h>
-
+
PresetsDB::PresetsDB()
{
}
-
void PresetsDB::clear()
{
plugins.remove_all_objects();
}
-void PresetsDB::load()
+void PresetsDB::load_from_file(char *path, int is_factory, int clear_it)
{
- clear();
-
+ if( clear_it ) clear();
FileXML file;
- char path[BCTEXTLEN];
- char string[BCTEXTLEN];
- sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
- FileSystem fs;
- fs.complete_path(path);
+
file.read_from_file(path);
+ load_common(&file, is_factory);
+}
+
+void PresetsDB::load_from_string(char *string, int is_factory, int clear_it)
+{
+ if( clear_it ) clear();
+
+ FileXML file;
+ file.read_from_string(string);
+ load_common(&file, is_factory);
+}
+
+
+
+void PresetsDB::load_common(FileXML *file, int is_factory)
+{
int result = 0;
+ char string[BCTEXTLEN];
- do
- {
- result = file.read_tag();
- if(!result)
- {
- if(file.tag.title_is("PLUGIN"))
- {
+ do {
+ result = file->read_tag();
+ if( ! result ) {
+ if( file->tag.title_is("PLUGIN") ) {
PresetsDBPlugin *plugin = 0;
sprintf(string, "Unknown");
- const char *title = file.tag.get_property("TITLE", string);
+ const char *title = file->tag.get_property("TITLE", string);
// Search for existing plugin
- for(int i = 0; i < plugins.size(); i++)
- {
- if(!strcasecmp(plugins.get(i)->title, title))
- {
- plugin = plugins.get(i);
+ for( int i=0; i<plugins.size(); ++i ) {
+ if( !strcasecmp(plugins[i]->title, title) ) {
+ plugin = plugins[i];
break;
}
}
// Create new plugin
- if(!plugin)
- {
+ if( !plugin ) {
plugin = new PresetsDBPlugin(title);
plugins.append(plugin);
}
- plugin->load(&file);
+ plugin->load(file, is_factory);
}
}
- }while(!result);
+ } while(!result);
}
+
void PresetsDB::save()
{
FileXML file;
- for(int i = 0; i < plugins.size(); i++)
- {
- PresetsDBPlugin *plugin = plugins.get(i);
- plugin->save(&file);
+ for( int i=0; i<plugins.size(); ++i ) {
+ PresetsDBPlugin *plugin = plugins[i];
+ if( plugin->get_total_presets(1) > 0 ) {
+ plugin->save(&file);
+ }
}
file.terminate_string();
}
-int PresetsDB::get_total_presets(char *plugin_title)
+int PresetsDB::get_total_presets(char *plugin_title, int user_only)
{
- for(int i = 0; i < plugins.size(); i++)
- {
- PresetsDBPlugin *plugin = plugins.get(i);
- if(!strcasecmp(plugin->title, plugin_title))
- {
- return plugin->keyframes.size();
+ for( int i=0; i<plugins.size(); ++i ) {
+ PresetsDBPlugin *plugin = plugins[i];
+ if( !strcasecmp(plugin->title, plugin_title) ) {
+ return plugin->get_total_presets(user_only);
}
}
return 0;
}
-char* PresetsDB::get_preset_title(char *plugin_title, int number)
+
+// move factory presets to the start, followed by sorted preset titles
+void PresetsDB::sort(char *plugin_title)
{
- for(int i = 0; i < plugins.size(); i++)
- {
- PresetsDBPlugin *plugin = plugins.get(i);
- if(!strcasecmp(plugin->title, plugin_title))
- {
- if(number < plugin->keyframes.size())
- {
- return plugin->keyframes.get(number)->title;
- }
- else
- {
- printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
+ PresetsDBPlugin *plugin = 0;
+ for( int i=0; !plugin && i<plugins.size(); ++i ) {
+ if( !strcasecmp(plugins[i]->title, plugin_title) )
+ plugin = plugins[i];
+ }
+
+ if( plugin ) {
+ int done = 0;
+ int total_presets = plugin->get_total_presets(0);
+ while( !done ) {
+ done = 1;
+ for( int i=0; i<total_presets-1; ++i ) {
+ PresetsDBKeyframe *keyframe1 = plugin->keyframes[i];
+ PresetsDBKeyframe *keyframe2 = plugin->keyframes[i+1];
+
+ if( (keyframe2->is_factory && !keyframe1->is_factory) ||
+ (keyframe2->is_factory == keyframe1->is_factory &&
+ strcmp(keyframe2->title, keyframe1->title) < 0) ) {
+ plugin->keyframes.set(i, keyframe2);
+ plugin->keyframes.set(i + 1, keyframe1);
+ done = 0;
+ }
}
+ }
+ }
+}
+
+
+char* PresetsDB::get_preset_title(char *plugin_title, int number)
+{
+ for( int i=0; i<plugins.size(); ++i ) {
+ PresetsDBPlugin *plugin = plugins[i];
+ if( !strcasecmp(plugin->title, plugin_title) ) {
+ if( number < plugin->keyframes.size() )
+ return plugin->keyframes[number]->title;
+ printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
+ break;
+ }
+ }
+ return 0;
+}
+
+
+int PresetsDB::get_is_factory(char *plugin_title, int number)
+{
+ for( int i=0; i<plugins.size(); ++i ) {
+ PresetsDBPlugin *plugin = plugins[i];
+ if( !strcasecmp(plugin->title, plugin_title) ) {
+ if( number < plugin->keyframes.size() )
+ return plugin->keyframes[number]->is_factory;
+ printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
break;
}
}
return 0;
}
+
char* PresetsDB::get_preset_data(char *plugin_title, int number)
{
- for(int i = 0; i < plugins.size(); i++)
- {
- PresetsDBPlugin *plugin = plugins.get(i);
- if(!strcasecmp(plugin->title, plugin_title))
- {
- if(number < plugin->keyframes.size())
- {
- return plugin->keyframes.get(number)->data;
- }
- else
- {
- printf("PresetsDB::get_preset_data %d buffer overrun\n", __LINE__);
- }
+ for( int i=0; i<plugins.size(); ++i ) {
+ PresetsDBPlugin *plugin = plugins[i];
+ if( !strcasecmp(plugin->title, plugin_title) ) {
+ if( number < plugin->keyframes.size() )
+ return plugin->keyframes[number]->data;
+ printf("PresetsDB::get_preset_data %d buffer overrun\n", __LINE__);
break;
}
}
PresetsDBPlugin* PresetsDB::get_plugin(const char *plugin_title)
{
- for(int i = 0; i < plugins.size(); i++)
- {
- PresetsDBPlugin *plugin = plugins.get(i);
- if(!strcasecmp(plugin->title, plugin_title))
- {
+ for( int i=0; i<plugins.size(); ++i ) {
+ PresetsDBPlugin *plugin = plugins[i];
+ if( !strcasecmp(plugin->title, plugin_title) )
return plugin;
- }
}
return 0;
}
}
-void PresetsDB::save_preset(const char *plugin_title, const char *preset_title, char *data)
+void PresetsDB::save_preset(const char *plugin_title,
+ const char *preset_title,
+ char *data)
{
PresetsDBPlugin *plugin = get_plugin(plugin_title);
- if(!plugin) plugin = new_plugin(plugin_title);
- PresetsDBKeyframe *keyframe = plugin->get_keyframe(preset_title);
- if(!keyframe) keyframe = plugin->new_keyframe(preset_title);
+ if( !plugin ) plugin = new_plugin(plugin_title);
+ PresetsDBKeyframe *keyframe = plugin->get_keyframe(preset_title, 0);
+ if( !keyframe ) keyframe = plugin->new_keyframe(preset_title);
keyframe->set_data(data);
save();
}
-void PresetsDB::delete_preset(const char *plugin_title, const char *preset_title)
+void PresetsDB::delete_preset(const char *plugin_title,
+ const char *preset_title,
+ int is_factory)
{
PresetsDBPlugin *plugin = get_plugin(plugin_title);
- if(plugin)
- {
+ if( plugin ) {
plugin->delete_keyframe(preset_title);
}
save();
}
-void PresetsDB::load_preset(const char *plugin_title, const char *preset_title, KeyFrame *keyframe)
+void PresetsDB::load_preset(const char *plugin_title,
+ const char *preset_title,
+ KeyFrame *keyframe,
+ int is_factory)
{
PresetsDBPlugin *plugin = get_plugin(plugin_title);
- if(plugin)
- {
- plugin->load_preset(preset_title, keyframe);
+ if( plugin ) {
+ plugin->load_preset(preset_title, keyframe, is_factory);
}
}
-int PresetsDB::preset_exists(const char *plugin_title, const char *preset_title)
+int PresetsDB::preset_exists(const char *plugin_title,
+ const char *preset_title,
+ int is_factory)
{
PresetsDBPlugin *plugin = get_plugin(plugin_title);
- if(plugin)
- {
- return plugin->preset_exists(preset_title);
+ if( plugin ) {
+ return plugin->preset_exists(preset_title, is_factory);
}
return 0;
}
-PresetsDBKeyframe::PresetsDBKeyframe(const char *title)
+PresetsDBKeyframe::PresetsDBKeyframe(const char *title, int is_factory)
{
- this->title = cstrdup(title);
+ this->title = strdup(title);
data = 0;
+ this->is_factory = is_factory;
}
PresetsDBKeyframe::~PresetsDBKeyframe()
void PresetsDBKeyframe::set_data(char *data)
{
delete [] this->data;
- this->data = cstrdup(data);
+ this->data = new char[strlen(data) + 1];
+ strcpy(this->data, data);
}
-
PresetsDBPlugin::PresetsDBPlugin(const char *title)
{
- this->title = cstrdup(title);
+ this->title = strdup(title);
}
PresetsDBPlugin::~PresetsDBPlugin()
delete [] title;
}
-void PresetsDBPlugin::load(FileXML *file)
+int PresetsDBPlugin::get_total_presets(int user_only)
+{
+ if( !user_only )
+ return keyframes.size();
+ int result = 0;
+ for( int j=0; j<keyframes.size(); ++j )
+ if( !keyframes[j]->is_factory ) ++result;
+ return result;
+}
+
+void PresetsDBPlugin::load(FileXML *file, int is_factory)
{
int result = 0;
char string[BCTEXTLEN];
- do
- {
+ do {
result = file->read_tag();
- if(!result)
- {
- if(file->tag.title_is("/PLUGIN")) break;
+ if( !result ) {
+ if( file->tag.title_is("/PLUGIN") ) break;
else
- if(file->tag.title_is("KEYFRAME"))
- {
+ if( file->tag.title_is("KEYFRAME") ) {
sprintf(string, "Unknown");
const char *keyframe_title = file->tag.get_property("TITLE", string);
- PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(keyframe_title);
+ PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(keyframe_title, is_factory);
char data[MESSAGESIZE];
- int len = file->read_data_until("/KEYFRAME", data, MESSAGESIZE-1);
- data[len] = 0;
+ file->read_text_until("/KEYFRAME", data, MESSAGESIZE);
keyframe->set_data(data);
keyframes.append(keyframe);
}
}
- }while(!result);
-
-
+ } while(!result);
}
void PresetsDBPlugin::save(FileXML *file)
file->append_tag();
file->append_newline();
- for(int j = 0; j < keyframes.size(); j++)
- {
- PresetsDBKeyframe *keyframe = keyframes.get(j);
- file->tag.set_title("KEYFRAME");
- file->tag.set_property("TITLE", keyframe->title);
- file->append_tag();
- file->append_text(keyframe->data);
- file->tag.set_title("/KEYFRAME");
- file->append_tag();
- file->append_newline();
+ for( int j=0; j<keyframes.size(); ++j ) {
+ PresetsDBKeyframe *keyframe = keyframes[j];
+
+ if( !keyframe->is_factory ) {
+ file->tag.set_title("KEYFRAME");
+ file->tag.set_property("TITLE", keyframe->title);
+ file->append_tag();
+ file->append_text(keyframe->data);
+ file->tag.set_title("/KEYFRAME");
+ file->append_tag();
+ file->append_newline();
+ }
}
file->tag.set_title("/PLUGIN");
file->append_newline();
}
-PresetsDBKeyframe* PresetsDBPlugin::get_keyframe(const char *title)
+PresetsDBKeyframe* PresetsDBPlugin::get_keyframe(const char *title,
+ int is_factory)
{
- for(int i = 0; i < keyframes.size(); i++)
- {
- PresetsDBKeyframe *keyframe = keyframes.get(i);
- if(!strcasecmp(keyframe->title, title)) return keyframe;
+ for( int i=0; i<keyframes.size(); ++i ) {
+ PresetsDBKeyframe *keyframe = keyframes[i];
+ if( !strcasecmp(keyframe->title, title) &&
+ keyframe->is_factory == is_factory )
+ return keyframe;
}
return 0;
}
void PresetsDBPlugin::delete_keyframe(const char *title)
{
- for(int i = 0; i < keyframes.size(); i++)
- {
- PresetsDBKeyframe *keyframe = keyframes.get(i);
- if(!strcasecmp(keyframe->title, title))
- {
+ for( int i=0; i<keyframes.size(); ++i ) {
+ PresetsDBKeyframe *keyframe = keyframes[i];
+ if( !strcasecmp(keyframe->title, title) && !keyframe->is_factory ) {
keyframes.remove_object_number(i);
return;
}
PresetsDBKeyframe* PresetsDBPlugin::new_keyframe(const char *title)
{
- PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(title);
+ PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(title, 0);
keyframes.append(keyframe);
return keyframe;
}
-void PresetsDBPlugin::load_preset(const char *preset_title, KeyFrame *keyframe)
+void PresetsDBPlugin::load_preset(const char *preset_title,
+ KeyFrame *keyframe,
+ int is_factory)
{
- PresetsDBKeyframe *src = get_keyframe(preset_title);
- if(src)
- {
+ PresetsDBKeyframe *src = get_keyframe(preset_title, is_factory);
+ if( src ) {
keyframe->set_data(src->data);
-// Save as the plugin's default
-// Need the path
+// Save as the plugin's default, Need the path
//printf("PresetsDBPlugin::load_preset %d %s\n", __LINE__, title);
PluginServer *server = MWindow::scan_plugindb(title, -1);
- if(!server)
- {
- }
- else
- {
+ if( server ) {
char path[BCTEXTLEN];
server->get_defaults_path(path);
FileSystem fs;
fs.complete_path(path);
FILE *fd = fopen(path, "w");
- if(fd)
- {
- if(!fwrite(src->data, strlen(src->data), 1, fd))
- {
+ if( fd ) {
+ if( !fwrite(src->data, strlen(src->data), 1, fd) ) {
fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
__LINE__,
path,
fclose(fd);
}
- else
- {
+ else {
fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
- __LINE__,
- path,
- strerror(errno));
+ __LINE__, path, strerror(errno));
}
}
}
}
-int PresetsDBPlugin::preset_exists(const char *preset_title)
+int PresetsDBPlugin::preset_exists(const char *preset_title, int is_factory)
{
- PresetsDBKeyframe *src = get_keyframe(preset_title);
- if(src)
- {
- return 1;
- }
- return 0;
+ PresetsDBKeyframe *src = get_keyframe(preset_title, is_factory);
+ return src ? 1 : 0;
}
-
-
-
-
-
-