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()
42 void PresetsDB::clear()
44 plugins.remove_all_objects();
47 void PresetsDB::load()
53 char string[BCTEXTLEN];
54 sprintf(path, "%s%s", BCASTDIR, PRESETS_FILE);
56 fs.complete_path(path);
57 file.read_from_file(path);
62 result = file.read_tag();
65 if(file.tag.title_is("PLUGIN"))
67 PresetsDBPlugin *plugin = 0;
68 sprintf(string, "Unknown");
69 const char *title = file.tag.get_property("TITLE", string);
71 // Search for existing plugin
72 for(int i = 0; i < plugins.size(); i++)
74 if(!strcasecmp(plugins.get(i)->title, title))
76 plugin = plugins.get(i);
84 plugin = new PresetsDBPlugin(title);
85 plugins.append(plugin);
94 void PresetsDB::save()
97 for(int i = 0; i < plugins.size(); i++)
99 PresetsDBPlugin *plugin = plugins.get(i);
102 file.terminate_string();
104 char path[BCTEXTLEN];
105 sprintf(path, "%s%s", BCASTDIR, PRESETS_FILE);
107 fs.complete_path(path);
108 file.write_to_file(path);
112 int PresetsDB::get_total_presets(char *plugin_title)
114 for(int i = 0; i < plugins.size(); i++)
116 PresetsDBPlugin *plugin = plugins.get(i);
117 if(!strcasecmp(plugin->title, plugin_title))
119 return plugin->keyframes.size();
126 char* PresetsDB::get_preset_title(char *plugin_title, int number)
128 for(int i = 0; i < plugins.size(); i++)
130 PresetsDBPlugin *plugin = plugins.get(i);
131 if(!strcasecmp(plugin->title, plugin_title))
133 if(number < plugin->keyframes.size())
135 return plugin->keyframes.get(number)->title;
139 printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
147 char* PresetsDB::get_preset_data(char *plugin_title, int number)
149 for(int i = 0; i < plugins.size(); i++)
151 PresetsDBPlugin *plugin = plugins.get(i);
152 if(!strcasecmp(plugin->title, plugin_title))
154 if(number < plugin->keyframes.size())
156 return plugin->keyframes.get(number)->data;
160 printf("PresetsDB::get_preset_data %d buffer overrun\n", __LINE__);
168 PresetsDBPlugin* PresetsDB::get_plugin(const char *plugin_title)
170 for(int i = 0; i < plugins.size(); i++)
172 PresetsDBPlugin *plugin = plugins.get(i);
173 if(!strcasecmp(plugin->title, plugin_title))
181 PresetsDBPlugin* PresetsDB::new_plugin(const char *plugin_title)
183 PresetsDBPlugin *result = new PresetsDBPlugin(plugin_title);
184 plugins.append(result);
189 void PresetsDB::save_preset(const char *plugin_title, const char *preset_title, char *data)
191 PresetsDBPlugin *plugin = get_plugin(plugin_title);
192 if(!plugin) plugin = new_plugin(plugin_title);
193 PresetsDBKeyframe *keyframe = plugin->get_keyframe(preset_title);
194 if(!keyframe) keyframe = plugin->new_keyframe(preset_title);
195 keyframe->set_data(data);
201 void PresetsDB::delete_preset(const char *plugin_title, const char *preset_title)
203 PresetsDBPlugin *plugin = get_plugin(plugin_title);
206 plugin->delete_keyframe(preset_title);
211 void PresetsDB::load_preset(const char *plugin_title, const char *preset_title, KeyFrame *keyframe)
213 PresetsDBPlugin *plugin = get_plugin(plugin_title);
216 plugin->load_preset(preset_title, keyframe);
220 int PresetsDB::preset_exists(const char *plugin_title, const char *preset_title)
222 PresetsDBPlugin *plugin = get_plugin(plugin_title);
225 return plugin->preset_exists(preset_title);
233 PresetsDBKeyframe::PresetsDBKeyframe(const char *title)
235 this->title = cstrdup(title);
239 PresetsDBKeyframe::~PresetsDBKeyframe()
245 void PresetsDBKeyframe::set_data(char *data)
247 delete [] this->data;
248 this->data = cstrdup(data);
253 PresetsDBPlugin::PresetsDBPlugin(const char *title)
255 this->title = cstrdup(title);
258 PresetsDBPlugin::~PresetsDBPlugin()
260 keyframes.remove_all_objects();
264 void PresetsDBPlugin::load(FileXML *file)
267 char string[BCTEXTLEN];
271 result = file->read_tag();
274 if(file->tag.title_is("/PLUGIN")) break;
276 if(file->tag.title_is("KEYFRAME"))
278 sprintf(string, "Unknown");
279 const char *keyframe_title = file->tag.get_property("TITLE", string);
280 PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(keyframe_title);
282 char data[MESSAGESIZE];
283 int len = file->read_data_until("/KEYFRAME", data, MESSAGESIZE-1);
285 keyframe->set_data(data);
286 keyframes.append(keyframe);
295 void PresetsDBPlugin::save(FileXML *file)
297 file->tag.set_title("PLUGIN");
298 file->tag.set_property("TITLE", title);
300 file->append_newline();
302 for(int j = 0; j < keyframes.size(); j++)
304 PresetsDBKeyframe *keyframe = keyframes.get(j);
305 file->tag.set_title("KEYFRAME");
306 file->tag.set_property("TITLE", keyframe->title);
308 file->append_text(keyframe->data);
309 file->tag.set_title("/KEYFRAME");
311 file->append_newline();
314 file->tag.set_title("/PLUGIN");
316 file->append_newline();
319 PresetsDBKeyframe* PresetsDBPlugin::get_keyframe(const char *title)
321 for(int i = 0; i < keyframes.size(); i++)
323 PresetsDBKeyframe *keyframe = keyframes.get(i);
324 if(!strcasecmp(keyframe->title, title)) return keyframe;
329 void PresetsDBPlugin::delete_keyframe(const char *title)
331 for(int i = 0; i < keyframes.size(); i++)
333 PresetsDBKeyframe *keyframe = keyframes.get(i);
334 if(!strcasecmp(keyframe->title, title))
336 keyframes.remove_object_number(i);
343 PresetsDBKeyframe* PresetsDBPlugin::new_keyframe(const char *title)
345 PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(title);
346 keyframes.append(keyframe);
350 void PresetsDBPlugin::load_preset(const char *preset_title, KeyFrame *keyframe)
352 PresetsDBKeyframe *src = get_keyframe(preset_title);
355 keyframe->set_data(src->data);
356 // Save as the plugin's default
358 //printf("PresetsDBPlugin::load_preset %d %s\n", __LINE__, title);
359 PluginServer *server = MWindow::scan_plugindb(title, -1);
365 char path[BCTEXTLEN];
366 server->get_defaults_path(path);
368 fs.complete_path(path);
370 FILE *fd = fopen(path, "w");
373 if(!fwrite(src->data, strlen(src->data), 1, fd))
375 fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
385 fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
394 int PresetsDBPlugin::preset_exists(const char *preset_title)
396 PresetsDBKeyframe *src = get_keyframe(preset_title);