1// =================================================================================================
2// ADOBE SYSTEMS INCORPORATED
3// Copyright 2002 Adobe Systems Incorporated
4// All Rights Reserved
5//
6// NOTICE:	Adobe permits you to use, modify, and distribute this file in accordance with the terms
7// of the Adobe license agreement accompanying it.
8// =================================================================================================
9
10//  ================================================================================================
11/// \file TXMPFiles.incl_cpp
12/// \brief The implementation of the TXMPFiles template class.
13
14#if XMP_WinBuild
15	#pragma warning ( disable : 4003 )	// not enough actual parameters for macro
16	#pragma warning ( disable : 4800 )	// forcing value to bool 'true' or 'false' (performance warning)
17#endif
18
19#include "client-glue/WXMP_Common.hpp"
20
21#include "client-glue/WXMPFiles.hpp"
22
23// =================================================================================================
24// Implementation Guidelines
25// =========================
26//
27// The implementations of the template functions are very stylized. The jobs done in this code are:
28//
29//	1. ...
30//
31// =================================================================================================
32
33#ifndef XMPFiles_TraceCTorDTor
34	#define XMPFiles_TraceCTorDTor 0
35#endif
36
37#if XMPFiles_TraceCTorDTor
38	class XFPeek {	// Hack to peek at the client ref count in the internal object.
39	public:
40		XFPeek();
41		virtual ~XFPeek();
42		XMP_Int32 clientRefs;
43	};
44#endif
45
46// =================================================================================================
47
48XMP_MethodIntro(TXMPFiles,void)::
49SetClientString ( void * clientPtr, XMP_StringPtr valuePtr, XMP_StringLen valueLen )
50{
51	tStringObj * clientStr = (tStringObj*) clientPtr;
52	clientStr->assign ( valuePtr, valueLen );
53}
54
55// -------------------------------------------------------------------------------------------------
56
57XMP_MethodIntro(TXMPFiles,void)::
58SetClientStringVector ( void * clientPtr, XMP_StringPtr * arrayPtr, XMP_Uns32 stringCount )
59{
60	std::vector<tStringObj>* clientVec = (std::vector<tStringObj>*) clientPtr;
61	clientVec->clear();
62	for ( XMP_Uns32 i = 0; i < stringCount; ++i ) {
63		tStringObj nextValue ( arrayPtr[i] );
64		clientVec->push_back ( nextValue );
65	}
66}
67
68// -------------------------------------------------------------------------------------------------
69
70XMP_MethodIntro(TXMPFiles,void)::
71GetVersionInfo ( XMP_VersionInfo * versionInfo )
72{
73	WrapNoCheckVoid ( zXMPFiles_GetVersionInfo_1 ( versionInfo ) );
74}
75
76// -------------------------------------------------------------------------------------------------
77
78XMP_MethodIntro(TXMPFiles,bool)::
79Initialize()
80{
81	WrapCheckBool ( ok, zXMPFiles_Initialize_1 ( 0 ) );
82	return ok;
83}
84
85// -------------------------------------------------------------------------------------------------
86
87XMP_MethodIntro(TXMPFiles,bool)::
88Initialize( const char* pluginFolder, const char* plugins )
89{
90	WrapCheckBool ( ok, zXMPFiles_Initialize_2 ( 0, pluginFolder, plugins ) );
91	return ok;
92}
93
94// -------------------------------------------------------------------------------------------------
95
96XMP_MethodIntro(TXMPFiles,bool)::
97Initialize ( XMP_OptionBits options )
98{
99	WrapCheckBool ( ok, zXMPFiles_Initialize_1 ( options ) );
100	return ok;
101}
102
103// -------------------------------------------------------------------------------------------------
104
105XMP_MethodIntro(TXMPFiles,bool)::
106Initialize ( XMP_OptionBits options, const char* pluginFolder, const char* plugins )
107{
108	WrapCheckBool ( ok, zXMPFiles_Initialize_2 ( options, pluginFolder, plugins ) );
109	return ok;
110}
111
112// -------------------------------------------------------------------------------------------------
113
114XMP_MethodIntro(TXMPFiles,void)::
115Terminate()
116{
117	WrapNoCheckVoid ( zXMPFiles_Terminate_1() );
118}
119
120// =================================================================================================
121
122static XMPFilesRef Default_CTor()
123{
124	WrapCheckXMPFilesRef ( newRef, zXMPFiles_CTor_1() );
125	return newRef;
126}
127
128// -------------------------------------------------------------------------------------------------
129
130XMP_CTorDTorIntro(TXMPFiles)::
131TXMPFiles() : xmpFilesRef(Default_CTor())
132{
133	#if XMPFiles_TraceCTorDTor
134		XFPeek* xfPtr = (XFPeek*)this->xmpFilesRef;
135		printf ( "Default construct TXMPFiles @ %.8X, ref = %.8X, count = %d\n", this, xfPtr, xfPtr->clientRefs );
136	#endif
137}
138
139// -------------------------------------------------------------------------------------------------
140
141XMP_CTorDTorIntro(TXMPFiles)::
142TXMPFiles ( const TXMPFiles<tStringObj> & original ) : xmpFilesRef(original.xmpFilesRef)
143{
144	WXMPFiles_IncrementRefCount_1 ( this->xmpFilesRef );
145	#if XMPFiles_TraceCTorDTor
146		XFPeek* xfPtr = (XFPeek*)this->xmpFilesRef;
147		printf ( "Copy construct TXMPFiles @ %.8X, ref = %.8X, count = %d\n", this, xfPtr, xfPtr->clientRefs );
148	#endif
149}
150
151// -------------------------------------------------------------------------------------------------
152
153XMP_MethodIntro(TXMPFiles,void)::
154operator= ( const TXMPFiles<tStringObj> & rhs )
155{
156	#if XMPFiles_TraceCTorDTor
157		XFPeek* xfLHS = (XFPeek*)this->xmpFilesRef;
158		XFPeek* xfRHS = (XFPeek*)rhs.xmpFilesRef;
159		printf ( "Assign TXMPFiles, lhs @ %.8X, rhs @ %.8X\n", this, &rhs );
160		printf ( "   original lhs ref = %.8X, count = %d\n", xfLHS, xfLHS->clientRefs );
161		printf ( "   original rhs ref = %.8X, count = %d\n", xfRHS, xfRHS->clientRefs );
162	#endif
163	XMPFilesRef oldRef = this->xmpFilesRef;					// ! Decrement last so errors leave client object OK.
164	this->xmpFilesRef = rhs.xmpFilesRef;
165	WXMPFiles_IncrementRefCount_1 ( this->xmpFilesRef );	// Increment the count on the new ref.
166	WXMPFiles_DecrementRefCount_1 ( oldRef );				// Decrement the count on the old ref.
167	#if XMPFiles_TraceCTorDTor
168		printf ( "   result   lhs ref = %.8X, count = %d\n", xfLHS, xfLHS->clientRefs );
169	#endif
170}
171
172// -------------------------------------------------------------------------------------------------
173
174XMP_CTorDTorIntro(TXMPFiles)::
175TXMPFiles ( XMPFilesRef _xmpFilesRef ) : xmpFilesRef(_xmpFilesRef)
176{
177	WXMPFiles_IncrementRefCount_1 ( this->xmpFilesRef );
178	#if XMPFiles_TraceCTorDTor
179		XFPeek* xfPtr = (XFPeek*)this->xmpFilesRef;
180		printf ( "Ref construct TXMPFiles @ %.8X, ref = %.8X, count = %d\n", this, xfPtr, xfPtr->clientRefs );
181	#endif
182}
183
184// -------------------------------------------------------------------------------------------------
185
186XMP_CTorDTorIntro(TXMPFiles)::
187TXMPFiles ( XMP_StringPtr  filePath,
188	        XMP_FileFormat format /* = kXMP_UnknownFile */,
189			XMP_OptionBits openFlags /* = 0 */ ) : xmpFilesRef(Default_CTor())
190{
191	#if XMPFiles_TraceCTorDTor
192		XFPeek* xfPtr = (XFPeek*)this->xmpFilesRef;
193		printf ( "File construct TXMPFiles @ %.8X, ref = %.8X, count = %d\n", this, xfPtr, xfPtr->clientRefs );
194	#endif
195	bool ok = this->OpenFile ( filePath, format, openFlags );
196	if ( ! ok ) throw XMP_Error ( kXMPErr_NoFileHandler, "OpenFile returned false" );
197}
198
199// -------------------------------------------------------------------------------------------------
200
201XMP_CTorDTorIntro(TXMPFiles)::
202TXMPFiles ( const tStringObj & filePath,
203	        XMP_FileFormat     format /* = kXMP_UnknownFile */,
204			XMP_OptionBits     openFlags /* = 0 */ ) : xmpFilesRef(Default_CTor())
205{
206	#if XMPFiles_TraceCTorDTor
207		XFPeek* xfPtr = (XFPeek*)this->xmpFilesRef;
208		printf ( "File construct TXMPFiles @ %.8X, ref = %.8X, count = %d\n", this, xfPtr, xfPtr->clientRefs );
209	#endif
210	bool ok = this->OpenFile ( filePath.c_str(), format, openFlags );
211	if ( ! ok ) throw XMP_Error ( kXMPErr_NoFileHandler, "OpenFile returned false" );
212}
213
214// -------------------------------------------------------------------------------------------------
215
216XMP_CTorDTorIntro(TXMPFiles)::
217~TXMPFiles () throw()
218{
219	#if XMPFiles_TraceCTorDTor
220		XFPeek* xfPtr = (XFPeek*)this->xmpFilesRef;
221		printf ( "Destruct TXMPFiles @ %.8X, ref= %.8X, count = %d\n", this, xfPtr, xfPtr->clientRefs );
222	#endif
223	WXMPFiles_DecrementRefCount_1 ( this->xmpFilesRef );
224	this->xmpFilesRef = 0;
225}
226
227// =================================================================================================
228
229XMP_MethodIntro(TXMPFiles,bool)::
230GetFormatInfo ( XMP_FileFormat   format,
231    			XMP_OptionBits * flags )
232{
233	WrapCheckBool ( found, zXMPFiles_GetFormatInfo_1 ( format, flags ) );
234	return found;
235}
236
237// =================================================================================================
238
239XMP_MethodIntro(TXMPFiles,XMPFilesRef)::
240GetInternalRef()
241{
242	return this->xmpFilesRef;
243}
244
245// -------------------------------------------------------------------------------------------------
246
247XMP_MethodIntro(TXMPFiles,XMP_FileFormat)::
248CheckFileFormat ( XMP_StringPtr filePath )
249{
250	WrapCheckFormat ( format, zXMPFiles_CheckFileFormat_1 ( filePath ) );
251	return format;
252}
253
254// -------------------------------------------------------------------------------------------------
255
256XMP_MethodIntro(TXMPFiles,XMP_FileFormat)::
257CheckPackageFormat ( XMP_StringPtr folderPath )
258{
259	WrapCheckFormat ( format, zXMPFiles_CheckPackageFormat_1 ( folderPath ) );
260	return format;
261}
262
263// -------------------------------------------------------------------------------------------------
264
265XMP_MethodIntro(TXMPFiles,bool)::
266GetFileModDate ( XMP_StringPtr filePath, XMP_DateTime * modDate, XMP_FileFormat * format, XMP_OptionBits options )
267{
268	WrapCheckBool ( ok, zXMPFiles_GetFileModDate_1 ( filePath, modDate, format, options ) );
269	return ok;
270}
271
272// -------------------------------------------------------------------------------------------------
273
274XMP_MethodIntro(TXMPFiles,bool)::
275GetAssociatedResources ( XMP_StringPtr            filePath,
276                         std::vector<tStringObj>* resourceList,
277                         XMP_FileFormat           format /* = kXMP_UnknownFile */,
278                         XMP_OptionBits           options /* = 0 */)
279{
280	WrapCheckBool ( ok, zXMPFiles_GetAssociatedResources_1 ( filePath, resourceList, format, options, SetClientStringVector ) );
281	return ok;
282}
283
284// -------------------------------------------------------------------------------------------------
285
286XMP_MethodIntro(TXMPFiles,bool)::
287IsMetadataWritable ( XMP_StringPtr  filePath,
288                     bool *         writable,
289                     XMP_FileFormat format /* = kXMP_UnknownFile */,
290                     XMP_OptionBits options /* = 0 */)
291{
292	if ( writable)
293	{
294		XMP_Bool internalWritable = ConvertBoolToXMP_Bool( *writable );
295		WrapCheckBool ( ok, zXMPFiles_IsMetadataWritable_1 ( filePath, &internalWritable, format, options ) );
296		*writable = ConvertXMP_BoolToBool( internalWritable );
297		return ok;
298	}
299	else
300	{
301		WrapCheckBool ( ok, zXMPFiles_IsMetadataWritable_1 ( filePath, NULL, format, options ) );
302		return ok;
303	}
304}
305
306// -------------------------------------------------------------------------------------------------
307
308XMP_MethodIntro(TXMPFiles,bool)::
309OpenFile ( XMP_StringPtr  filePath,
310		   XMP_FileFormat format /* = kXMP_UnknownFile */,
311		   XMP_OptionBits openFlags /* = 0 */ )
312{
313	WrapCheckBool ( ok, zXMPFiles_OpenFile_1 ( filePath, format, openFlags ) );
314	return ok;
315}
316
317// -------------------------------------------------------------------------------------------------
318
319XMP_MethodIntro(TXMPFiles,bool)::
320OpenFile ( const tStringObj & filePath,
321		   XMP_FileFormat     format /* = kXMP_UnknownFile */,
322		   XMP_OptionBits     openFlags /* = 0 */ )
323{
324	return this->OpenFile ( filePath.c_str(), format, openFlags );
325}
326
327// -------------------------------------------------------------------------------------------------
328
329#if XMP_StaticBuild	// ! Client XMP_IO objects can only be used in static builds.
330XMP_MethodIntro(TXMPFiles,bool)::
331OpenFile ( XMP_IO *       clientIO,
332		   XMP_FileFormat format /* = kXMP_UnknownFile */,
333		   XMP_OptionBits openFlags /* = 0 */ )
334{
335	WrapCheckBool ( ok, zXMPFiles_OpenFile_2 ( clientIO, format, openFlags ) );
336	return ok;
337}
338#endif
339
340// -------------------------------------------------------------------------------------------------
341
342XMP_MethodIntro(TXMPFiles,void)::
343CloseFile ( XMP_OptionBits closeFlags /* = 0 */ )
344{
345	WrapCheckVoid ( zXMPFiles_CloseFile_1 ( closeFlags ) );
346}
347
348// -------------------------------------------------------------------------------------------------
349
350XMP_MethodIntro(TXMPFiles,bool)::
351GetFileInfo ( tStringObj *     filePath /* = 0 */,
352		      XMP_OptionBits * openFlags /* = 0 */,
353		      XMP_FileFormat * format /* = 0 */,
354		      XMP_OptionBits * handlerFlags /* = 0 */ )
355{
356	WrapCheckBool ( isOpen, zXMPFiles_GetFileInfo_1 ( filePath, openFlags, format, handlerFlags, SetClientString ) );
357	return isOpen;
358}
359
360// -------------------------------------------------------------------------------------------------
361
362XMP_MethodIntro(TXMPFiles,void)::
363SetAbortProc ( XMP_AbortProc abortProc,
364			   void *        abortArg )
365{
366	WrapCheckVoid ( zXMPFiles_SetAbortProc_1 ( abortProc, abortArg ) );
367}
368
369// -------------------------------------------------------------------------------------------------
370
371XMP_MethodIntro(TXMPFiles,bool)::
372GetXMP ( SXMPMeta *       xmpObj /* = 0 */,
373    	 tStringObj *     xmpPacket /* = 0 */,
374    	 XMP_PacketInfo * packetInfo /* = 0 */ )
375{
376	XMPMetaRef xmpRef = 0;
377	if ( xmpObj != 0 ) {
378		SXMPUtils::RemoveProperties ( xmpObj, 0, 0, kXMPUtil_DoAllProperties );	// *** Need an SXMPMeta::Clear method:
379		xmpRef = xmpObj->GetInternalRef();
380	}
381
382	WrapCheckBool ( hasXMP, zXMPFiles_GetXMP_1 ( xmpRef, xmpPacket, packetInfo, SetClientString ) );
383	return hasXMP;
384}
385
386// -------------------------------------------------------------------------------------------------
387
388XMP_MethodIntro(TXMPFiles,void)::
389PutXMP ( const SXMPMeta & xmpObj )
390{
391	WrapCheckVoid ( zXMPFiles_PutXMP_1 ( xmpObj.GetInternalRef(), 0, 0 ) );
392}
393
394// -------------------------------------------------------------------------------------------------
395
396XMP_MethodIntro(TXMPFiles,void)::
397PutXMP ( XMP_StringPtr xmpPacket,
398         XMP_StringLen xmpLength /* = kXMP_UseNullTermination */ )
399{
400	WrapCheckVoid ( zXMPFiles_PutXMP_1 ( 0, xmpPacket, xmpLength ) );
401}
402
403// -------------------------------------------------------------------------------------------------
404
405XMP_MethodIntro(TXMPFiles,void)::
406PutXMP ( const tStringObj & xmpPacket )
407{
408	this->PutXMP ( xmpPacket.c_str(), (XMP_StringLen)xmpPacket.size() );
409}
410
411// -------------------------------------------------------------------------------------------------
412
413XMP_MethodIntro(TXMPFiles,bool)::
414CanPutXMP ( const SXMPMeta & xmpObj )
415{
416	WrapCheckBool ( canPut, zXMPFiles_CanPutXMP_1 ( xmpObj.GetInternalRef(), 0, 0 ) );
417	return canPut;
418}
419
420// -------------------------------------------------------------------------------------------------
421
422XMP_MethodIntro(TXMPFiles,bool)::
423CanPutXMP ( XMP_StringPtr xmpPacket,
424            XMP_StringLen xmpLength /* = kXMP_UseNullTermination */ )
425{
426	WrapCheckBool ( canPut, zXMPFiles_CanPutXMP_1 ( 0, xmpPacket, xmpLength ) );
427	return canPut;
428}
429
430// -------------------------------------------------------------------------------------------------
431
432XMP_MethodIntro(TXMPFiles,bool)::
433CanPutXMP ( const tStringObj & xmpPacket )
434{
435	return this->CanPutXMP ( xmpPacket.c_str(), (XMP_StringLen)xmpPacket.size() );
436}
437
438// =================================================================================================
439
440XMP_MethodIntro(TXMPFiles,void)::
441SetDefaultProgressCallback ( XMP_ProgressReportProc proc, void * context /* = 0 */,
442	                         float interval /* = 1.0 */, bool sendStartStop /* = false */ )
443{
444	XMP_Bool internalsendStartStop = ConvertBoolToXMP_Bool( sendStartStop );
445	WrapCheckVoid ( zXMPFiles_SetDefaultProgressCallback_1 ( proc, context, interval, internalsendStartStop ) );
446}
447
448// -------------------------------------------------------------------------------------------------
449
450XMP_MethodIntro(TXMPFiles,void)::
451SetProgressCallback ( XMP_ProgressReportProc proc, void * context /* = 0 */,
452	                  float interval /* = 1.0 */, bool sendStartStop /* = false */ )
453{
454	XMP_Bool internalsendStartStop = ConvertBoolToXMP_Bool( sendStartStop );
455	WrapCheckVoid ( zXMPFiles_SetProgressCallback_1 ( proc, context, interval, internalsendStartStop ) );
456}
457
458// -------------------------------------------------------------------------------------------------
459
460XMP_MethodIntro(TXMPFiles,void)::
461SetDefaultErrorCallback ( XMPFiles_ErrorCallbackProc proc,
462	                      void * context /* = 0 */, XMP_Uns32 limit /*= 1 */ )
463{
464	WrapCheckVoid ( zXMPFiles_SetDefaultErrorCallback_1 ( proc, context, limit ) );
465}
466
467// -------------------------------------------------------------------------------------------------
468
469XMP_MethodIntro(TXMPFiles,void)::
470SetErrorCallback ( XMPFiles_ErrorCallbackProc proc,
471	               void * context /* = 0 */, XMP_Uns32 limit /*= 1 */ )
472{
473	WrapCheckVoid ( zXMPFiles_SetErrorCallback_1 ( proc, context, limit ) );
474}
475
476// -------------------------------------------------------------------------------------------------
477
478XMP_MethodIntro(TXMPFiles,void)::
479ResetErrorCallbackLimit ( XMP_Uns32 limit /* = 1 */ )
480{
481	WrapCheckVoid ( zXMPFiles_ResetErrorCallbackLimit_1 ( limit ) );
482}
483
484// =================================================================================================
485