summaryrefslogtreecommitdiff
path: root/XMPFiles/source/PluginHandler/FileHandlerInstance.cpp
blob: c70a794f386d91b94a4e6978883971693e4a5fc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2011 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================

#include "FileHandlerInstance.h"

namespace XMP_PLUGIN
{

FileHandlerInstance::FileHandlerInstance ( SessionRef object, FileHandlerSharedPtr handler, XMPFiles * _parent ):
XMPFileHandler( _parent ), mObject( object ), mHandler( handler )
{
	this->handlerFlags = mHandler->getHandlerFlags();
	this->stdCharForm  = kXMP_Char8Bit;
	PluginManager::addHandlerInstance( this->mObject, this );
}

FileHandlerInstance::~FileHandlerInstance()
{
	WXMP_Error error;
	mHandler->getModule()->getPluginAPIs()->mTerminateSessionProc( this->mObject, &error );
	PluginManager::removeHandlerInstance( this->mObject );
	CheckError( error );
}

bool FileHandlerInstance::GetFileModDate ( XMP_DateTime * modDate )
{
	XMP_Bool ok;
	WXMP_Error error;
	GetSessionFileModDateProc wGetFileModDate = mHandler->getModule()->getPluginAPIs()->mGetFileModDateProc;
	wGetFileModDate ( this->mObject, &ok, modDate, &error );
	CheckError ( error );
	return ConvertXMP_BoolToBool( ok );
}

void FileHandlerInstance::CacheFileData()
{
	if( this->containsXMP ) return;
	
	WXMP_Error error;
	XMP_StringPtr xmpStr = NULL;
	mHandler->getModule()->getPluginAPIs()->mCacheFileDataProc( this->mObject, this->parent->ioRef, &xmpStr, &error );
	
	if( error.mErrorID != kXMPErr_NoError )
	{
		if ( xmpStr != 0 ) free( (void*) xmpStr );
		if ( error.mErrorID == kXMPErr_FilePermission )
			throw XMP_Error( kXMPErr_FilePermission, error.mErrorMsg );
		else
			throw XMP_Error( kXMPErr_InternalFailure, error.mErrorMsg );
	}

	if( xmpStr != NULL )
	{
		this->xmpPacket.assign( xmpStr );
		free( (void*) xmpStr ); // It should be freed as documentation of mCacheFileDataProc says so.
		this->containsXMP = true;
	}
	else
		this->containsXMP = false;
}

void FileHandlerInstance::ProcessXMP()
{
	if( this->processedXMP ) return;
	this->processedXMP = true;

	SXMPUtils::RemoveProperties ( &this->xmpObj, 0, 0, kXMPUtil_DoAllProperties );
	if ( this->xmpPacket.size() != 0 )
		this->xmpObj.ParseFromBuffer ( this->xmpPacket.c_str(), (XMP_StringLen)this->xmpPacket.size() );

	WXMP_Error error;
	if ( mHandler->getModule()->getPluginAPIs()->mVersion >= 4 && mHandler->getModule()->getPluginAPIs()->mImportToXMPStringWithPacketProc )
	{
		XMP_StringPtr xmpStr = this->xmpPacket.c_str();
		XMP_StringPtr oldPacketPtr = NULL;
		XMP_PacketInfo packetInfo;
		mHandler->getModule()->getPluginAPIs()->mImportToXMPStringWithPacketProc( this->mObject, &xmpStr, &error, &oldPacketPtr, &packetInfo );
		
		if( xmpStr != NULL && xmpStr != this->xmpPacket.c_str() )
		{
			XMP_StringLen newLen = static_cast<XMP_StringLen>(strlen( xmpStr ));
			this->xmpObj.Erase();
			this->xmpObj.ParseFromBuffer( xmpStr, newLen, 0 );
			
			// Note: Freeing memory would not create any problem as plugin would have allocated memory using Host library function
			free( ( void * ) xmpStr );
			this->containsXMP = true;
		}

		if( oldPacketPtr != NULL )
		{
			this->xmpPacket.resize( strlen( oldPacketPtr ) );
			this->xmpPacket.assign( oldPacketPtr );
			this->packetInfo = packetInfo;
			
			// Note: Freeing memory would not create any problem as plugin would have allocated memory using Host library function
			free( ( void * ) oldPacketPtr );
			this->containsXMP = true;
		}
	}
	else if( mHandler->getModule()->getPluginAPIs()->mVersion >= 2 && mHandler->getModule()->getPluginAPIs()->mImportToXMPStringProc )
	{
		XMP_StringPtr xmpStr = this->xmpPacket.c_str();
		mHandler->getModule()->getPluginAPIs()->mImportToXMPStringProc( this->mObject, &xmpStr, &error );
		
		if( xmpStr != NULL && xmpStr != this->xmpPacket.c_str() )
		{
			XMP_StringLen newLen = static_cast<XMP_StringLen>(strlen( xmpStr ));
			this->xmpObj.Erase();
			this->xmpObj.ParseFromBuffer( xmpStr, newLen, 0 );
			
			// Note: Freeing memory would not create any problem as plugin would have allocated memory using Host library function
			free( ( void * ) xmpStr );
			this->containsXMP = true;
		}
	}
	else
	{
		if( mHandler->getModule()->getPluginAPIs()->mImportToXMPProc )
			mHandler->getModule()->getPluginAPIs()->mImportToXMPProc( this->mObject, this->xmpObj.GetInternalRef(), &error );
		this->containsXMP = true;
	}
	CheckError( error );
}

void FileHandlerInstance::UpdateFile ( bool doSafeUpdate )
{
	bool optimizeFileLayout = XMP_OptionIsSet ( this->parent->openFlags, kXMPFiles_OptimizeFileLayout );
	this->needsUpdate |= optimizeFileLayout;
	if( !this->needsUpdate ) return;
	WXMP_Error error;

	if ( xmpPacket.size() != 0 )
	{
		if( mHandler->getModule()->getPluginAPIs()->mExportFromXMPStringProc )
		{
			std::string xmp;
			this->xmpObj.SerializeToBuffer( &xmp, kXMP_NoOptions, 0 );
			XMP_StringPtr xmpStr = xmp.c_str();
			mHandler->getModule()->getPluginAPIs()->mExportFromXMPStringProc( this->mObject, xmpStr, &error );
			if ( xmpStr != xmp.c_str() )
				this->xmpObj.SerializeToBuffer ( &this->xmpPacket, mHandler->getSerializeOption() );
		}
		else
		{
			if( mHandler->getModule()->getPluginAPIs()->mExportFromXMPProc )
			{
				mHandler->getModule()->getPluginAPIs()->mExportFromXMPProc( this->mObject, this->xmpObj.GetInternalRef(), &error );
				this->xmpObj.SerializeToBuffer ( &this->xmpPacket, mHandler->getSerializeOption() );
			}
		}
		CheckError( error );

		
	}
	mHandler->getModule()->getPluginAPIs()->mUpdateFileProc( this->mObject, this->parent->ioRef, doSafeUpdate, this->xmpPacket.c_str(), &error );
	CheckError( error );
	this->needsUpdate = false;
}

void FileHandlerInstance::WriteTempFile( XMP_IO* tempRef )
{
	WXMP_Error error;
	if( mHandler->getModule()->getPluginAPIs()->mExportFromXMPProc )
		mHandler->getModule()->getPluginAPIs()->mExportFromXMPProc( this->mObject, this->xmpObj.GetInternalRef(), &error );
	CheckError( error );

	this->xmpObj.SerializeToBuffer ( &this->xmpPacket, mHandler->getSerializeOption() );

	mHandler->getModule()->getPluginAPIs()->mWriteTempFileProc( this->mObject, this->parent->ioRef, tempRef, this->xmpPacket.c_str(), &error );
	CheckError( error );
}

static void SetStringVector ( StringVectorRef clientPtr, XMP_StringPtr * arrayPtr, XMP_Uns32 stringCount )
{
	std::vector<std::string>* clientVec = (std::vector<std::string>*) clientPtr;
	clientVec->clear();
	for ( XMP_Uns32 i = 0; i < stringCount; ++i ) {
		std::string nextValue ( arrayPtr[i] );
		clientVec->push_back ( nextValue );
	}
}

void FileHandlerInstance::FillMetadataFiles( std::vector<std::string> * metadataFiles )
{
	WXMP_Error error;
	FillMetadataFilesProc wFillMetadataFilesProc = mHandler->getModule()->getPluginAPIs()->mFillMetadataFilesProc;
	if ( wFillMetadataFilesProc ) {
		wFillMetadataFilesProc( this->mObject, metadataFiles, SetStringVector, &error);
		CheckError( error );
	} else {
		XMP_Throw ( "This version of plugin does not support FillMetadataFiles API", kXMPErr_Unimplemented );
	}
}

void FileHandlerInstance::FillAssociatedResources( std::vector<std::string> * resourceList )
{
	WXMP_Error error;
	FillAssociatedResourcesProc wFillAssociatedResourcesProc = mHandler->getModule()->getPluginAPIs()->mFillAssociatedResourcesProc;
	if ( wFillAssociatedResourcesProc ) {
		wFillAssociatedResourcesProc( this->mObject, resourceList, SetStringVector, &error);
		CheckError( error );
	} else {
		XMP_Throw ( "This version of plugin does not support FillAssociatedResources API", kXMPErr_Unimplemented );
	}
}

bool FileHandlerInstance::IsMetadataWritable( )
{
	WXMP_Error error;
	XMP_Bool result = kXMP_Bool_False;
	IsMetadataWritableProc wIsMetadataWritableProc = mHandler->getModule()->getPluginAPIs()->mIsMetadataWritableProc;
	if ( wIsMetadataWritableProc ) {
		wIsMetadataWritableProc( this->mObject, &result, &error);
		CheckError( error );
	} else {
		XMP_Throw ( "This version of plugin does not support IsMetadataWritable API", kXMPErr_Unimplemented );
	}
	return ConvertXMP_BoolToBool( result );
}

void FileHandlerInstance::SetErrorCallback ( ErrorCallbackBox errorCallbackBox )
{
	WXMP_Error error;
	SetErrorCallbackproc wSetErrorCallbackproc = mHandler->getModule()->getPluginAPIs()->mSetErrorCallbackproc ;
	if( wSetErrorCallbackproc )	{
		wSetErrorCallbackproc( this->mObject, errorCallbackBox, &error );
		CheckError( error );
	} else {
		XMP_Throw ( "This version of plugin does not support IsMetadataWritable API", kXMPErr_Unimplemented );
	}
}

void FileHandlerInstance::SetProgressCallback ( XMP_ProgressTracker::CallbackInfo * progCBInfoPtr )
{
	WXMP_Error error;
	SetProgressCallbackproc wSetProgressCallbackproc = mHandler->getModule()->getPluginAPIs()->mSetProgressCallbackproc ;
	if( wSetProgressCallbackproc )	{
		wSetProgressCallbackproc( this->mObject, progCBInfoPtr, &error );
		CheckError( error );
	} else {
		XMP_Throw ( "This version of plugin does not support IsMetadataWritable API", kXMPErr_Unimplemented );
	}
}

} //namespace XMP_PLUGIN