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"
26 #include "filesystem.h"
29 #include "messages.inc"
31 #include "pluginserver.h"
32 #include "preferences.inc"
38 PresetsDB::PresetsDB()
43 void PresetsDB::clear()
45 plugins.remove_all_objects();
48 void PresetsDB::load()
54 char string[BCTEXTLEN];
55 sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
57 fs.complete_path(path);
58 file.read_from_file(path);
63 result = file.read_tag();
66 if(file.tag.title_is("PLUGIN"))
68 PresetsDBPlugin *plugin = 0;
69 sprintf(string, "Unknown");
70 const char *title = file.tag.get_property("TITLE", string);
72 // Search for existing plugin
73 for(int i = 0; i < plugins.size(); i++)
75 if(!strcasecmp(plugins.get(i)->title, title))
77 plugin = plugins.get(i);
85 plugin = new PresetsDBPlugin(title);
86 plugins.append(plugin);
95 void PresetsDB::save()
98 for(int i = 0; i < plugins.size(); i++)
100 PresetsDBPlugin *plugin = plugins.get(i);
103 file.terminate_string();
105 char path[BCTEXTLEN];
106 sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
108 fs.complete_path(path);
109 file.write_to_file(path);
113 int PresetsDB::get_total_presets(char *plugin_title)
115 for(int i = 0; i < plugins.size(); i++)
117 PresetsDBPlugin *plugin = plugins.get(i);
118 if(!strcasecmp(plugin->title, plugin_title))
120 return plugin->keyframes.size();
127 char* PresetsDB::get_preset_title(char *plugin_title, int number)
129 for(int i = 0; i < plugins.size(); i++)
131 PresetsDBPlugin *plugin = plugins.get(i);
132 if(!strcasecmp(plugin->title, plugin_title))
134 if(number < plugin->keyframes.size())
136 return plugin->keyframes.get(number)->title;
140 printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
148 char* PresetsDB::get_preset_data(char *plugin_title, int number)
150 for(int i = 0; i < plugins.size(); i++)
152 PresetsDBPlugin *plugin = plugins.get(i);
153 if(!strcasecmp(plugin->title, plugin_title))
155 if(number < plugin->keyframes.size())
157 return plugin->keyframes.get(number)->data;
161 printf("PresetsDB::get_preset_data %d buffer overrun\n", __LINE__);
169 PresetsDBPlugin* PresetsDB::get_plugin(const char *plugin_title)
171 for(int i = 0; i < plugins.size(); i++)
173 PresetsDBPlugin *plugin = plugins.get(i);
174 if(!strcasecmp(plugin->title, plugin_title))
182 PresetsDBPlugin* PresetsDB::new_plugin(const char *plugin_title)
184 PresetsDBPlugin *result = new PresetsDBPlugin(plugin_title);
185 plugins.append(result);
190 void PresetsDB::save_preset(const char *plugin_title, const char *preset_title, char *data)
192 PresetsDBPlugin *plugin = get_plugin(plugin_title);
193 if(!plugin) plugin = new_plugin(plugin_title);
194 PresetsDBKeyframe *keyframe = plugin->get_keyframe(preset_title);
195 if(!keyframe) keyframe = plugin->new_keyframe(preset_title);
196 keyframe->set_data(data);
202 void PresetsDB::delete_preset(const char *plugin_title, const char *preset_title)
204 PresetsDBPlugin *plugin = get_plugin(plugin_title);
207 plugin->delete_keyframe(preset_title);
212 void PresetsDB::load_preset(const char *plugin_title, const char *preset_title, KeyFrame *keyframe)
214 PresetsDBPlugin *plugin = get_plugin(plugin_title);
217 plugin->load_preset(preset_title, keyframe);
221 int PresetsDB::preset_exists(const char *plugin_title, const char *preset_title)
223 PresetsDBPlugin *plugin = get_plugin(plugin_title);
226 return plugin->preset_exists(preset_title);
234 PresetsDBKeyframe::PresetsDBKeyframe(const char *title)
236 this->title = cstrdup(title);
240 PresetsDBKeyframe::~PresetsDBKeyframe()
246 void PresetsDBKeyframe::set_data(char *data)
248 delete [] this->data;
249 this->data = cstrdup(data);
254 PresetsDBPlugin::PresetsDBPlugin(const char *title)
256 this->title = cstrdup(title);
259 PresetsDBPlugin::~PresetsDBPlugin()
261 keyframes.remove_all_objects();
265 void PresetsDBPlugin::load(FileXML *file)
268 char string[BCTEXTLEN];
272 result = file->read_tag();
275 if(file->tag.title_is("/PLUGIN")) break;
277 if(file->tag.title_is("KEYFRAME"))
279 sprintf(string, "Unknown");
280 const char *keyframe_title = file->tag.get_property("TITLE", string);
281 PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(keyframe_title);
283 char data[MESSAGESIZE];
284 int len = file->read_data_until("/KEYFRAME", data, MESSAGESIZE-1);
286 keyframe->set_data(data);
287 keyframes.append(keyframe);
296 void PresetsDBPlugin::save(FileXML *file)
298 file->tag.set_title("PLUGIN");
299 file->tag.set_property("TITLE", title);
301 file->append_newline();
303 for(int j = 0; j < keyframes.size(); j++)
305 PresetsDBKeyframe *keyframe = keyframes.get(j);
306 file->tag.set_title("KEYFRAME");
307 file->tag.set_property("TITLE", keyframe->title);
309 file->append_text(keyframe->data);
310 file->tag.set_title("/KEYFRAME");
312 file->append_newline();
315 file->tag.set_title("/PLUGIN");
317 file->append_newline();
320 PresetsDBKeyframe* PresetsDBPlugin::get_keyframe(const char *title)
322 for(int i = 0; i < keyframes.size(); i++)
324 PresetsDBKeyframe *keyframe = keyframes.get(i);
325 if(!strcasecmp(keyframe->title, title)) return keyframe;
330 void PresetsDBPlugin::delete_keyframe(const char *title)
332 for(int i = 0; i < keyframes.size(); i++)
334 PresetsDBKeyframe *keyframe = keyframes.get(i);
335 if(!strcasecmp(keyframe->title, title))
337 keyframes.remove_object_number(i);
344 PresetsDBKeyframe* PresetsDBPlugin::new_keyframe(const char *title)
346 PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(title);
347 keyframes.append(keyframe);
351 void PresetsDBPlugin::load_preset(const char *preset_title, KeyFrame *keyframe)
353 PresetsDBKeyframe *src = get_keyframe(preset_title);
356 keyframe->set_data(src->data);
357 // Save as the plugin's default
359 //printf("PresetsDBPlugin::load_preset %d %s\n", __LINE__, title);
360 PluginServer *server = MWindow::scan_plugindb(title, -1);
366 char path[BCTEXTLEN];
367 server->get_defaults_path(path);
369 fs.complete_path(path);
371 FILE *fd = fopen(path, "w");
374 if(!fwrite(src->data, strlen(src->data), 1, fd))
376 fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
386 fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
395 int PresetsDBPlugin::preset_exists(const char *preset_title)
397 PresetsDBKeyframe *src = get_keyframe(preset_title);