Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / cinelerra / presets.C.sav
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcsignals.h"
23 #include "bcwindowbase.inc"
24 #include "cstrdup.h"
25 #include "file.h"
26 #include "filesystem.h"
27 #include "filexml.h"
28 #include "keyframe.h"
29 #include "messages.inc"
30 #include "mwindow.h"
31 #include "pluginserver.h"
32 #include "preferences.inc"
33 #include "presets.h"
34
35 #include <errno.h>
36 #include <string.h>
37
38 PresetsDB::PresetsDB()
39 {
40 }
41
42
43 void PresetsDB::clear()
44 {
45         plugins.remove_all_objects();
46 }
47
48 void PresetsDB::load()
49 {
50         clear();
51
52         FileXML file;
53         char path[BCTEXTLEN];
54         char string[BCTEXTLEN];
55         sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
56         FileSystem fs;
57         fs.complete_path(path);
58         file.read_from_file(path);
59         int result = 0;
60
61         do
62         {
63                 result = file.read_tag();
64                 if(!result)
65                 {
66                         if(file.tag.title_is("PLUGIN"))
67                         {
68                                 PresetsDBPlugin *plugin = 0;
69                                 sprintf(string, "Unknown");
70                                 const char *title = file.tag.get_property("TITLE", string);
71
72 // Search for existing plugin
73                                 for(int i = 0; i < plugins.size(); i++)
74                                 {
75                                         if(!strcasecmp(plugins.get(i)->title, title))
76                                         {
77                                                 plugin = plugins.get(i);
78                                                 break;
79                                         }
80                                 }
81
82 // Create new plugin
83                                 if(!plugin)
84                                 {
85                                         plugin = new PresetsDBPlugin(title);
86                                         plugins.append(plugin);
87                                 }
88
89                                 plugin->load(&file);
90                         }
91                 }
92         }while(!result);
93 }
94
95 void PresetsDB::save()
96 {
97         FileXML file;
98         for(int i = 0; i < plugins.size(); i++)
99         {
100                 PresetsDBPlugin *plugin = plugins.get(i);
101                 plugin->save(&file);
102         }
103         file.terminate_string();
104
105         char path[BCTEXTLEN];
106         sprintf(path, "%s/%s", File::get_config_path(), PRESETS_FILE);
107         FileSystem fs;
108         fs.complete_path(path);
109         file.write_to_file(path);
110 }
111
112
113 int PresetsDB::get_total_presets(char *plugin_title)
114 {
115         for(int i = 0; i < plugins.size(); i++)
116         {
117                 PresetsDBPlugin *plugin = plugins.get(i);
118                 if(!strcasecmp(plugin->title, plugin_title))
119                 {
120                         return plugin->keyframes.size();
121                 }
122         }
123
124         return 0;
125 }
126
127 char* PresetsDB::get_preset_title(char *plugin_title, int number)
128 {
129         for(int i = 0; i < plugins.size(); i++)
130         {
131                 PresetsDBPlugin *plugin = plugins.get(i);
132                 if(!strcasecmp(plugin->title, plugin_title))
133                 {
134                         if(number < plugin->keyframes.size())
135                         {
136                                 return plugin->keyframes.get(number)->title;
137                         }
138                         else
139                         {
140                                 printf("PresetsDB::get_preset_title %d buffer overrun\n", __LINE__);
141                         }
142                         break;
143                 }
144         }
145         return 0;
146 }
147
148 char* PresetsDB::get_preset_data(char *plugin_title, int number)
149 {
150         for(int i = 0; i < plugins.size(); i++)
151         {
152                 PresetsDBPlugin *plugin = plugins.get(i);
153                 if(!strcasecmp(plugin->title, plugin_title))
154                 {
155                         if(number < plugin->keyframes.size())
156                         {
157                                 return plugin->keyframes.get(number)->data;
158                         }
159                         else
160                         {
161                                 printf("PresetsDB::get_preset_data %d buffer overrun\n", __LINE__);
162                         }
163                         break;
164                 }
165         }
166         return 0;
167 }
168
169 PresetsDBPlugin* PresetsDB::get_plugin(const char *plugin_title)
170 {
171         for(int i = 0; i < plugins.size(); i++)
172         {
173                 PresetsDBPlugin *plugin = plugins.get(i);
174                 if(!strcasecmp(plugin->title, plugin_title))
175                 {
176                         return plugin;
177                 }
178         }
179         return 0;
180 }
181
182 PresetsDBPlugin* PresetsDB::new_plugin(const char *plugin_title)
183 {
184         PresetsDBPlugin *result = new PresetsDBPlugin(plugin_title);
185         plugins.append(result);
186         return result;
187 }
188
189
190 void PresetsDB::save_preset(const char *plugin_title, const char *preset_title, char *data)
191 {
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);
197         save();
198
199 }
200
201
202 void PresetsDB::delete_preset(const char *plugin_title, const char *preset_title)
203 {
204         PresetsDBPlugin *plugin = get_plugin(plugin_title);
205         if(plugin)
206         {
207                 plugin->delete_keyframe(preset_title);
208         }
209         save();
210 }
211
212 void PresetsDB::load_preset(const char *plugin_title, const char *preset_title, KeyFrame *keyframe)
213 {
214         PresetsDBPlugin *plugin = get_plugin(plugin_title);
215         if(plugin)
216         {
217                 plugin->load_preset(preset_title, keyframe);
218         }
219 }
220
221 int PresetsDB::preset_exists(const char *plugin_title, const char *preset_title)
222 {
223         PresetsDBPlugin *plugin = get_plugin(plugin_title);
224         if(plugin)
225         {
226                 return plugin->preset_exists(preset_title);
227         }
228         return 0;
229 }
230
231
232
233
234 PresetsDBKeyframe::PresetsDBKeyframe(const char *title)
235 {
236         this->title = cstrdup(title);
237         data = 0;
238 }
239
240 PresetsDBKeyframe::~PresetsDBKeyframe()
241 {
242         delete [] title;
243         delete [] data;
244 }
245
246 void PresetsDBKeyframe::set_data(char *data)
247 {
248         delete [] this->data;
249         this->data = cstrdup(data);
250 }
251
252
253
254 PresetsDBPlugin::PresetsDBPlugin(const char *title)
255 {
256         this->title = cstrdup(title);
257 }
258
259 PresetsDBPlugin::~PresetsDBPlugin()
260 {
261         keyframes.remove_all_objects();
262         delete [] title;
263 }
264
265 void PresetsDBPlugin::load(FileXML *file)
266 {
267         int result = 0;
268         char string[BCTEXTLEN];
269
270         do
271         {
272                 result = file->read_tag();
273                 if(!result)
274                 {
275                         if(file->tag.title_is("/PLUGIN")) break;
276                         else
277                         if(file->tag.title_is("KEYFRAME"))
278                         {
279                                 sprintf(string, "Unknown");
280                                 const char *keyframe_title = file->tag.get_property("TITLE", string);
281                                 PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(keyframe_title);
282
283                                 char data[MESSAGESIZE];
284                                 int len = file->read_data_until("/KEYFRAME", data, MESSAGESIZE-1);
285                                 data[len] = 0;
286                                 keyframe->set_data(data);
287                                 keyframes.append(keyframe);
288
289                         }
290                 }
291         }while(!result);
292
293
294 }
295
296 void PresetsDBPlugin::save(FileXML *file)
297 {
298         file->tag.set_title("PLUGIN");
299         file->tag.set_property("TITLE", title);
300         file->append_tag();
301         file->append_newline();
302
303         for(int j = 0; j < keyframes.size(); j++)
304         {
305                 PresetsDBKeyframe *keyframe = keyframes.get(j);
306                 file->tag.set_title("KEYFRAME");
307                 file->tag.set_property("TITLE", keyframe->title);
308                 file->append_tag();
309                 file->append_text(keyframe->data);
310                 file->tag.set_title("/KEYFRAME");
311                 file->append_tag();
312                 file->append_newline();
313         }
314
315         file->tag.set_title("/PLUGIN");
316         file->append_tag();
317         file->append_newline();
318 }
319
320 PresetsDBKeyframe* PresetsDBPlugin::get_keyframe(const char *title)
321 {
322         for(int i = 0; i < keyframes.size(); i++)
323         {
324                 PresetsDBKeyframe *keyframe = keyframes.get(i);
325                 if(!strcasecmp(keyframe->title, title)) return keyframe;
326         }
327         return 0;
328 }
329
330 void PresetsDBPlugin::delete_keyframe(const char *title)
331 {
332         for(int i = 0; i < keyframes.size(); i++)
333         {
334                 PresetsDBKeyframe *keyframe = keyframes.get(i);
335                 if(!strcasecmp(keyframe->title, title))
336                 {
337                         keyframes.remove_object_number(i);
338                         return;
339                 }
340         }
341 }
342
343
344 PresetsDBKeyframe* PresetsDBPlugin::new_keyframe(const char *title)
345 {
346         PresetsDBKeyframe *keyframe = new PresetsDBKeyframe(title);
347         keyframes.append(keyframe);
348         return keyframe;
349 }
350
351 void PresetsDBPlugin::load_preset(const char *preset_title, KeyFrame *keyframe)
352 {
353         PresetsDBKeyframe *src = get_keyframe(preset_title);
354         if(src)
355         {
356                 keyframe->set_data(src->data);
357 // Save as the plugin's default
358 // Need the path
359 //printf("PresetsDBPlugin::load_preset %d %s\n", __LINE__, title);
360                 PluginServer *server = MWindow::scan_plugindb(title, -1);
361                 if(!server)
362                 {
363                 }
364                 else
365                 {
366                         char path[BCTEXTLEN];
367                         server->get_defaults_path(path);
368                         FileSystem fs;
369                         fs.complete_path(path);
370
371                         FILE *fd = fopen(path, "w");
372                         if(fd)
373                         {
374                                 if(!fwrite(src->data, strlen(src->data), 1, fd))
375                                 {
376                                         fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
377                                                 __LINE__,
378                                                 path,
379                                                 strerror(errno));
380                                 }
381
382                                 fclose(fd);
383                         }
384                         else
385                         {
386                                 fprintf(stderr, "PresetsDBPlugin::load_preset %d \"%s\": %s\n",
387                                         __LINE__,
388                                         path,
389                                         strerror(errno));
390                         }
391                 }
392         }
393 }
394
395 int PresetsDBPlugin::preset_exists(const char *preset_title)
396 {
397         PresetsDBKeyframe *src = get_keyframe(preset_title);
398         if(src)
399         {
400                 return 1;
401         }
402         return 0;
403 }
404
405
406
407
408
409
410