Sound packing/unpacking operators.
[blender.git] / source / blender / editors / sound / sound_ops.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (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 Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "DNA_packedFile_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_sound_types.h"
37 #include "DNA_sequence_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_main.h"
43 #include "BKE_report.h"
44 #include "BKE_packedFile.h"
45 #include "BKE_sound.h"
46
47 #include "BLI_blenlib.h"
48
49 #include "ED_sound.h"
50
51 #include "RNA_access.h"
52 #include "RNA_define.h"
53 #include "RNA_enum_types.h"
54
55 #include "UI_interface.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "AUD_C-API.h"
61
62 #include "sound_intern.h"
63
64 /******************** open sound operator ********************/
65
66 static int open_exec(bContext *C, wmOperator *op)
67 {
68         char filename[FILE_MAX];
69         bSound *sound;
70         AUD_SoundInfo info;
71
72         RNA_string_get(op->ptr, "filename", filename);
73
74         sound = sound_new_file(CTX_data_main(C), filename);
75
76         if (sound==NULL || sound->handle == NULL) {
77                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
78                 return OPERATOR_CANCELLED;
79         }
80
81         info = AUD_getInfo(sound->handle);
82
83         if (info.specs.format == AUD_FORMAT_INVALID) {
84                 sound_delete(C, sound);
85                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
86                 return OPERATOR_CANCELLED;
87         }
88
89         return OPERATOR_FINISHED;
90 }
91
92 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
93 {
94         return WM_operator_filesel(C, op, event);
95 }
96
97 void SOUND_OT_open(wmOperatorType *ot)
98 {
99         /* identifiers */
100         ot->name= "Open Sound";
101         ot->idname= "SOUND_OT_open";
102         ot->description= "Load a sound file into blender";
103
104         /* api callbacks */
105         ot->exec= open_exec;
106         ot->invoke= open_invoke;
107
108         /* flags */
109         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
110
111         /* properties */
112         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE|MOVIEFILE);
113 }
114
115 /* ******************************************************* */
116
117 static int sound_poll(bContext *C)
118 {
119         Editing* ed = CTX_data_scene(C)->ed;
120
121         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND || !ed->act_seq->sound)
122                 return 0;
123
124         return 1;
125 }
126 /********************* pack operator *********************/
127
128 static int pack_exec(bContext *C, wmOperator *op)
129 {
130         Editing* ed = CTX_data_scene(C)->ed;
131         bSound* sound;
132
133         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
134                 return OPERATOR_CANCELLED;
135
136         sound = ed->act_seq->sound;
137
138         if(!sound || sound->packedfile)
139                 return OPERATOR_CANCELLED;
140
141         sound->packedfile= newPackedFile(op->reports, sound->name);
142         sound_load(CTX_data_main(C), sound);
143
144         return OPERATOR_FINISHED;
145 }
146
147 void SOUND_OT_pack(wmOperatorType *ot)
148 {
149         /* identifiers */
150         ot->name= "Pack Sound";
151         ot->idname= "SOUND_OT_pack";
152
153         /* api callbacks */
154         ot->exec= pack_exec;
155         ot->poll= sound_poll;
156
157         /* flags */
158         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
159 }
160
161 /********************* unpack operator *********************/
162
163 // XXX this function is in image_ops.c too, exactly the same, should be moved to a generally accessible position
164 void unpack_menu(bContext *C, char *opname, char *abs_name, char *folder, PackedFile *pf)
165 {
166         uiPopupMenu *pup;
167         uiLayout *layout;
168         char line[FILE_MAX + 100];
169         char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
170
171         strcpy(local_name, abs_name);
172         BLI_splitdirstring(local_name, fi);
173         sprintf(local_name, "//%s/%s", folder, fi);
174
175         pup= uiPupMenuBegin(C, "Unpack file", 0);
176         layout= uiPupMenuLayout(pup);
177
178         uiItemEnumO(layout, "Remove Pack", 0, opname, "method", PF_REMOVE);
179
180         if(strcmp(abs_name, local_name)) {
181                 switch(checkPackedFile(local_name, pf)) {
182                         case PF_NOFILE:
183                                 sprintf(line, "Create %s", local_name);
184                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
185                                 break;
186                         case PF_EQUAL:
187                                 sprintf(line, "Use %s (identical)", local_name);
188                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
189                                 break;
190                         case PF_DIFFERS:
191                                 sprintf(line, "Use %s (differs)", local_name);
192                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
193                                 sprintf(line, "Overwrite %s", local_name);
194                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
195                                 break;
196                 }
197         }
198
199         switch(checkPackedFile(abs_name, pf)) {
200                 case PF_NOFILE:
201                         sprintf(line, "Create %s", abs_name);
202                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
203                         break;
204                 case PF_EQUAL:
205                         sprintf(line, "Use %s (identical)", abs_name);
206                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
207                         break;
208                 case PF_DIFFERS:
209                         sprintf(line, "Use %s (differs)", local_name);
210                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
211                         sprintf(line, "Overwrite %s", local_name);
212                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
213                         break;
214         }
215
216         uiPupMenuEnd(C, pup);
217 }
218
219 static int unpack_exec(bContext *C, wmOperator *op)
220 {
221         int method= RNA_enum_get(op->ptr, "method");
222         Editing* ed = CTX_data_scene(C)->ed;
223         bSound* sound;
224
225         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
226                 return OPERATOR_CANCELLED;
227
228         sound = ed->act_seq->sound;
229
230         if(!sound || !sound->packedfile)
231                 return OPERATOR_CANCELLED;
232
233         if(G.fileflags & G_AUTOPACK)
234                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
235
236         unpackSound(op->reports, sound, method);
237
238         return OPERATOR_FINISHED;
239 }
240
241 static int unpack_invoke(bContext *C, wmOperator *op, wmEvent *event)
242 {
243         Editing* ed = CTX_data_scene(C)->ed;
244         bSound* sound;
245
246         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
247                 return OPERATOR_CANCELLED;
248
249         sound = ed->act_seq->sound;
250
251         if(!sound || !sound->packedfile)
252                 return OPERATOR_CANCELLED;
253
254         if(G.fileflags & G_AUTOPACK)
255                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
256
257         unpack_menu(C, "SOUND_OT_unpack", sound->name, "audio", sound->packedfile);
258
259         return OPERATOR_FINISHED;
260 }
261
262 void SOUND_OT_unpack(wmOperatorType *ot)
263 {
264         /* identifiers */
265         ot->name= "Unpack Sound";
266         ot->idname= "SOUND_OT_unpack";
267
268         /* api callbacks */
269         ot->exec= unpack_exec;
270         ot->invoke= unpack_invoke;
271         ot->poll= sound_poll;
272
273         /* flags */
274         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
275
276         /* properties */
277         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
278 }
279
280 /* ******************************************************* */
281
282 void ED_operatortypes_sound(void)
283 {
284         WM_operatortype_append(SOUND_OT_open);
285         WM_operatortype_append(SOUND_OT_pack);
286         WM_operatortype_append(SOUND_OT_unpack);
287 }