4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcsignals.h"
23 #include "bcwindowbase.inc"
25 #include "filesystem.h"
28 #include "messages.inc"
30 #include "pluginserver.h"
31 #include "preferences.inc"
37 PresetsDB::PresetsDB()
41 void PresetsDB::clear()
43 plugins.remove_all_objects();
46 void PresetsDB::load_from_file(char *path, int is_factory, int clear_it)
48 if( clear_it ) clear();
51 file.read_from_file(path, 1);
52 load_common(&file, is_factory);
55 void PresetsDB::load_from_string(char *string, int is_factory, int clear_it)
57 if( clear_it ) clear();
60 file.read_from_string(string);
61 load_common(&file, is_factory);
66 void PresetsDB::load_common(FileXML *file, int is_factory)
69 char string[BCTEXTLEN];
72 result = file->read_tag();
74 if( file->tag.title_is("PLUGIN") ) {
75 PresetsDBPlugin *plugin = 0;
76 sprintf(string, "Unknown");
77 const char *title = file->tag.get_property("TITLE", string);
79 // Search for existing plugin
80 for( int i=0; i<plugins.size(); ++i ) {
81 if( !strcasecmp(plugins[i]->title, title) ) {
89 plugin = new PresetsDBPlugin(title);
90 plugins.append(plugin);
93 plugin->load(file, is_factory);
100 void PresetsDB::save()
103 for( int i=0; i<plugins.size(); ++i ) {
104 PresetsDBPlugin *plugin = plugins[i];
105 if( plugin->get_total_presets(1) > 0 ) {
109 file.terminate_string();
111 char path[BCTEXTLEN];
112 sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
114 fs.complete_path(path);
115 file.write_to_file(path);
119 int PresetsDB::get_total_presets(char *plugin_title, int user_only)
121 for( int i=0; i<plugins.size(); ++i ) {
122 PresetsDBPlugin *plugin = plugins[i];
123 if( !strcasecmp(plugin->title, plugin_title) ) {
124 return plugin->get_total_presets(user_only);
132 // move factory presets to the start, followed by sorted preset titles
133 void PresetsDB::sort(char *plugin_title)
135 PresetsDBPlugin *plugin = 0;
136 for( int i=0; !plugin && i<plugins.size(); ++i ) {
137 if( !strcasecmp(plugins[i]->title, plugin_title) )
143 int total_presets = plugin->get_total_presets(0);
146 for( int i=0; i<total_presets-1; ++i ) {
147 PresetsDBKeyframe *keyframe1 = plugin->keyframes[i];
148 PresetsDBKeyframe *keyframe2 = plugin->keyframes[i+1];
150 if( (keyframe2->is_factory && !keyframe1->is_factory) ||
151 (keyframe2->is_factory == keyframe1->is_factory &&
152 strcmp(keyframe2->title, keyframe1->title) < 0) ) {
153 plugin->keyframes.set(i, keyframe2);
154 plugin->keyframes.set(i + 1, keyframe1);
163 char* PresetsDB::get_preset_title(char *plugin_title, int number)
165 for( int i=0; i<plugins.size(); ++i ) {
166 PresetsDBPlugin *plugin = plugins[i];
167 if( !strcasecmp(plugin->title, plugin_title) ) {
168 if( number < plugin->keyframes.size() )
169 return plugin->keyframes[number]->title;
170 printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
178 int PresetsDB::get_is_factory(char *plugin_title, int number)
180 for( int i=0; i<plugins.size(); ++i ) {
181 PresetsDBPlugin *plugin = plugins[i];
182 if( !strcasecmp(plugin->title, plugin_title) ) {
183 if( number < plugin->keyframes.size() )
184 return plugin->keyframes[number]->is_factory;
185 printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
193 char* PresetsDB::get_preset_data(char *plugin_title, int number)
195 for( int i=0; i<plugins.size(); ++i ) {
196 PresetsDBPlugin *plugin = plugins[i];
197 if( !strcasecmp(plugin->title, plugin_title) ) {
198 if( number < plugin->keyframes.size() )
199 return plugin->keyframes[number]->data;
200 printf("PresetsDB::get_preset_data %d buffer overrun\n", __LINE__);
207 PresetsDBPlugin* PresetsDB::get_plugin(const char *plugin_title)
209 for( int i=0; i<plugins.size(); ++i ) {
210 PresetsDBPlugin *plugin = plugins[i];
211 if( !strcasecmp(plugin->title, plugin_title) )
217 PresetsDBPlugin* PresetsDB::new_plugin(const char *plugin_title)
219 PresetsDBPlugin *result = new PresetsDBPlugin(plugin_title);
220 plugins.append(result);
225 void PresetsDB::save_preset(const char *plugin_title,
226 const char *preset_title,
229 PresetsDBPlugin *plugin = get_plugin(plugin_title);
230 if( !plugin ) plugin = new_plugin(plugin_title);
231 PresetsDBKeyframe *keyframe = plugin->get_keyframe(preset_title, 0);
232 if( !keyframe ) keyframe = plugin->new_keyframe(preset_title);
233 keyframe->set_data(data);
239 void PresetsDB::delete_preset(const char *plugin_title,
240 const char *preset_title,
243 PresetsDBPlugin *plugin = get_plugin(plugin_title);
245 plugin->delete_keyframe(preset_title);
250 void PresetsDB::load_preset(const char *plugin_title,
251 const char *preset_title,
255 PresetsDBPlugin *plugin = get_plugin(plugin_title);
257 plugin->load_preset(preset_title, keyframe, is_factory);
261 int PresetsDB::preset_exists(const char *plugin_title,
262 const char *preset_title,
265 PresetsDBPlugin *plugin = get_plugin(plugin_title);
267 return plugin->preset_exists(preset_title, is_factory);
275 PresetsDBKeyframe::PresetsDBKeyframe(const char *title, int is_factory)
277 this->title = strdup(title);
279 this->is_factory = is_factory;
282 PresetsDBKeyframe::~PresetsDBKeyframe()
288 void PresetsDBKeyframe::set_data(char *data)
290 delete [] this->data;
291 this->data = new char[strlen(data) + 1];
292 strcpy(this->data, data);
296 PresetsDBPlugin::PresetsDBPlugin(const char *title)
298 this->title = strdup(title);
301 PresetsDBPlugin::~PresetsDBPlugin()
303 keyframes.remove_all_objects();
307 int PresetsDBPlugin::get_total_presets(int user_only)
310 return keyframes.size();
312 for( int j=0; j<keyframes.size(); ++j )
313 if( !keyframes[j]->is_factory ) ++result;
317 void PresetsDBPlugin::load(FileXML *file, int is_factory)
320 char string[BCTEXTLEN];
323 result = file->read_tag();
325 if( file->tag.title_is("/PLUGIN") ) break;
327 if( file->tag.title_is("KEYFRAME") ) {
328 sprintf(string, "Unknown");
329 const char *keyframe_title = file->tag.get_property("TITLE", string);
330 PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(keyframe_title, is_factory);
332 file->read_data_until("/KEYFRAME", &data);
333 keyframe->set_data(data.cstr());
334 keyframes.append(keyframe);
341 void PresetsDBPlugin::save(FileXML *file)
343 file->tag.set_title("PLUGIN");
344 file->tag.set_property("TITLE", title);
346 file->append_newline();
348 for( int j=0; j<keyframes.size(); ++j ) {
349 PresetsDBKeyframe *keyframe = keyframes[j];
351 if( !keyframe->is_factory ) {
352 file->tag.set_title("KEYFRAME");
353 file->tag.set_property("TITLE", keyframe->title);
355 file->append_data(keyframe->data);
356 file->tag.set_title("/KEYFRAME");
358 file->append_newline();
362 file->tag.set_title("/PLUGIN");
364 file->append_newline();
367 PresetsDBKeyframe* PresetsDBPlugin::get_keyframe(const char *title,
370 for( int i=0; i<keyframes.size(); ++i ) {
371 PresetsDBKeyframe *keyframe = keyframes[i];
372 if( !strcasecmp(keyframe->title, title) &&
373 keyframe->is_factory == is_factory )
379 void PresetsDBPlugin::delete_keyframe(const char *title)
381 for( int i=0; i<keyframes.size(); ++i ) {
382 PresetsDBKeyframe *keyframe = keyframes[i];
383 if( !strcasecmp(keyframe->title, title) && !keyframe->is_factory ) {
384 keyframes.remove_object_number(i);
391 PresetsDBKeyframe* PresetsDBPlugin::new_keyframe(const char *title)
393 PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(title, 0);
394 keyframes.append(keyframe);
398 void PresetsDBPlugin::load_preset(const char *preset_title,
402 PresetsDBKeyframe *src = get_keyframe(preset_title, is_factory);
404 keyframe->set_data(src->data);
405 // Save as the plugin's default, Need the path
406 //printf("PresetsDBPlugin::load_preset %d %s\n", __LINE__, title);
407 PluginServer *server = MWindow::scan_plugindb(title, -1);
409 char path[BCTEXTLEN];
410 server->get_defaults_path(path);
412 fs.complete_path(path);
414 FILE *fd = fopen(path, "w");
416 if( !fwrite(src->data, strlen(src->data), 1, fd) ) {
417 fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
426 fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
427 __LINE__, path, strerror(errno));
433 int PresetsDBPlugin::preset_exists(const char *preset_title, int is_factory)
435 PresetsDBKeyframe *src = get_keyframe(preset_title, is_factory);