summaryrefslogtreecommitdiff
path: root/XMPFilesPlugins/PluginTemplate/source/Template_Handler.cpp
blob: 6d4e7309c33ccd18e5a99aaab4da25f01ce4e550 (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
// =================================================================================================
// Copyright Adobe
// Copyright 2011 Adobe
// 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. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#include "XMPFilesPlugins/api/source/PluginBase.h"
#include "XMPFilesPlugins/api/source/PluginRegistry.h"
#include "XMPFilesPlugins/api/source/HostAPIAccess.h"

#include "source/XMPFiles_IO.hpp"
#include "source/XIO.hpp"
#include "source/XMP_LibUtils.hpp"

namespace XMP_PLUGIN
{

// Plugin template file handler class. 
// This handler can be used to read/write XMP from/to text files
// All file handlers should be derived from PluginBase.
class TEMP_MetaHandler : public PluginBase
{
public:
	TEMP_MetaHandler( const std::string& filePath, XMP_Uns32 openFlags, XMP_Uns32 format, XMP_Uns32 handlerFlags )
		: PluginBase( filePath, openFlags, format, handlerFlags ) {}
	~TEMP_MetaHandler() {}

	//Minimum required virtual functions which need to be implemented by the plugin developer.
	virtual void cacheFileData( const IOAdapter& file, std::string& xmpStr );
	virtual void updateFile( const IOAdapter& file, bool doSafeUpdate, const std::string& xmpStr );
	// In case you want to directly support crash-safe updates
	virtual void writeTempFile( const IOAdapter& srcFile, const IOAdapter& tmpFile, const std::string& xmpStr );

	//Static functions which also need to be implemented by the plugin developer.
	
	// This function is called to initialize the file handler. Don't get confused by the file handler instance. 
	// It may be an empty function if nothing needs to be initialized.
	//@return true on success otherwise false.
	static bool initialize();
	
	//This function is called to terminate the file handler.
	//@return true on success otherwise false.
	static bool terminate();

	//The following two functions need to be implemented to check the format. Though file handler 
	//will need only one of these, both of them need to be implemented. The actual check format 
	//function should be implemented properly while the other one will just return false.
	
	//@return true on success otherwise false.
	static bool checkFileFormat( const std::string& filePath, const IOAdapter& file );
	
	// This function is not needed by a normal handler but an "owning" handler needs to implement it.
	static inline bool checkFolderFormat(	const std::string& rootPath, 
											const std::string& gpName, 
											const std::string& parentName, 
											const std::string& leafName ) { return false; }

private:
	void ReconcileXMP( const std::string &xmpStr, std::string *outStr );

	static int init;
	static const std::string XPACKET_HEADER;
	static const std::string XMPMETA_HEADER;
};

int TEMP_MetaHandler::init = 0;
const std::string TEMP_MetaHandler::XPACKET_HEADER = "<?xpacket";
const std::string TEMP_MetaHandler::XMPMETA_HEADER = "<x:xmpmeta";

//============================================================================
// registerHandler TEMP_MetaHandler
//============================================================================

// Return module identifier string. This string should be same as the string present in 
// the resource file otherwise plugin won't be loaded.
// This function needs to be implemented by the plugin developer.
const char* GetModuleIdentifier()
{
	static const char* kModuleIdentifier = "com.adobe.xmp.plugins.template";
	return kModuleIdentifier;
}

// This function will be called during initialization of the plugin.
// Additional host API suite can be requested using RequestAPISuite().
// The initialization will be aborted if false is returned.
bool SetupPlugin()
{
	/*
	 ExampleSuite* exampleSuite =
	 	reinterpret_cast<ExampleSuite*>( RequestAPISuite( "exampleSuite", 1 ) );
	 
	 return (exampleSuite != NULL);
	 */
	
	return true;
}

// All the file handlers present in this module will be registered. There may be many file handlers
// inside one module. Only those file handlers which are registered here will be visible to XMPFiles.
// This function need to be implemented by the plugin developer.
//@param sXMPTemplate uid of the file handler, 
void RegisterFileHandlers()
{
	//uid of the file handler. It's different from the module identifier.
	const char* sXMPTemplate = "com.adobe.xmp.plugins.template.handler"; 
	PluginRegistry::registerHandler( new PluginCreator<TEMP_MetaHandler>(sXMPTemplate) );
}

//============================================================================
// TEMP_MetaHandler specific functions
//============================================================================

bool TEMP_MetaHandler::initialize()
{
	init++;

	if(init != 1) return true;

	{
		SXMPMeta::Initialize();
	
		// Do init work here
	}

	return true;
}


bool TEMP_MetaHandler::terminate()
{
	init--;

	if(init != 0) return true;

	// Do termination work here

	SXMPMeta::Terminate();
	
	return true;
}


bool TEMP_MetaHandler::checkFileFormat( const std::string& filePath, const IOAdapter& file )
{
	try 
	{
		const XMP_Uns8 MIN_CHECK_LENGTH = 9;

		if ( file.Length() < MIN_CHECK_LENGTH ) return false;

		char buffer[MIN_CHECK_LENGTH];
		XMP_Int64 offset = 0;
		file.Seek ( offset, kXMP_SeekFromStart );
		file.Read ( buffer, MIN_CHECK_LENGTH, true );

		return ( strncmp(buffer, XPACKET_HEADER.c_str(), MIN_CHECK_LENGTH) == 0 || 
				strncmp(buffer, XMPMETA_HEADER.c_str(), MIN_CHECK_LENGTH) == 0 );
	} 
	catch ( ... ) 
	{
		return false;
	}

	return false;
}


void TEMP_MetaHandler::cacheFileData( const IOAdapter& file, std::string& xmpStr )
{
	xmpStr.erase();
		
	// Extract the contents.

	XMP_Int64 xmpLength = file.Length();
	if ( xmpLength > 1024*1024*1024 ) 
	{	// Sanity check and to not overflow std::string.
		XMP_Throw ( "XMP file is too large", kXMPErr_PluginCacheFileData );
	}

	if ( xmpLength > 0 ) 
	{
		xmpStr.assign ( (size_t)xmpLength, ' ' );
		XMP_Int64 offset = 0;
		file.Seek ( offset, kXMP_SeekFromStart );
		file.Read ( (void*)xmpStr.c_str(), (XMP_Uns32)xmpLength, true );
	}
}


void TEMP_MetaHandler::updateFile( const IOAdapter& file, bool doSafeUpdate, const std::string& xmpStr )
{
	// safe update option only relevant for handlers which "own" the file I/O
	// for other handlers the method writeTempFile is called in case the client wants a safe update
	
	// Example how to use XMPCore to edit the XMP data before export
	// Please note that if you do not want to explicitely edit the XMP at this point
	// you can write it directly to the file as it is already correctly serialized
	// as indicated by the plugin manifest serialize options
	std::string outStr;
	ReconcileXMP(xmpStr, &outStr);
	
	// overwrite existing file
	XMP_Int64 offset = 0;
	file.Seek ( offset, kXMP_SeekFromStart );
	file.Write((void *)outStr.c_str(), static_cast<XMP_Uns32>(outStr.length()));
	file.Truncate(static_cast<XMP_Uns32>(outStr.length()));
}


void TEMP_MetaHandler::writeTempFile( const IOAdapter& srcFile, const IOAdapter& tmpFile, const std::string& xmpStr )
{
	// The source file is in this case irrelevant

	// Please note that if you are creating a plugin for Adobe applications like Premiere there is a bug
	// in the implementation of the caller of this function which passes in the "xmpStr" parameter
	// the path to the media file instead of the serialized XMP packet!
	// If the plugin is for your own application using the XMP SDK, the parameter value is correct

	// example how to use XMPCore to edit the XMP data before export
	std::string outStr;
	ReconcileXMP(xmpStr, &outStr);

	tmpFile.Write((void *)outStr.c_str(), static_cast<XMP_Uns32>(outStr.length()));
}


void TEMP_MetaHandler::ReconcileXMP( const std::string &xmpStr, std::string *outStr ) 
{
	// This is an example how to use XMPCore to edit the XMP data
	SXMPMeta xmp;
	xmp.ParseFromBuffer( xmpStr.c_str(), xmpStr.length() );

	xmp.SetProperty( kXMP_NS_XMP, "CreatorTool", "My plugin" );

	xmp.SerializeToBuffer( outStr, kXMP_UseCompactFormat | kXMP_OmitPacketWrapper );
}

} //namespace XMP_PLUGIN