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