/**
 * Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved.
 */
#include "pdfsecure.h"
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

/** 
 * A custom callback that can be used to report errors and other messages.
 */ 
char* PDFSECURE_API_CALL MyCallback(int mode, char* msg, void* user_data)
{
	if (mode == PDFSECURE_ERROR) {
		cout << "Error: " << msg << endl;
	}
	else if (mode == PDFSECURE_MSG) {
		cout << msg;
	}
	else if (mode == PDFSECURE_GETPASS) {
		static string gl_pass;
		cin >> gl_pass;
		return (char*)gl_pass.c_str();
	}
	else if (mode == PDFSECURE_OUT_FILENAME)
	{
		if(user_data != 0)
		{
			vector<string>* saved_files=(vector<string>*)user_data;
			saved_files->push_back(string(msg));
		}
	}

	return 0;
}


/**
 * You can modify the following lines with your registration information.
 */
char* username = "John Doe";
char* company = "My Company";
char* lic_key = "my license key";

/**
 * A macro that can be used to switch between different code samples.
 */
#define EXAMPLE 1

/**
 * The simplest PDFSecure program: Pass an explicit command-line string.
 *
 * This example will secure all PDF documents in 'TestFiles' using 128-bit encryption.
 * The password required to open processed files is 'secret'. 

 * - The -o parameter is used to specify the output folder/file. 
 * - The -s parameter specifies that the output document should be encrypted using 
 *   128-bit RC4 encryption.
 * - The -u parameter specifies the password required to access the secured 
 *   document.
 * 
 * For a detailed explanation of all commands, please refer to PDF Secure User Manual.
 * Also, PDF Secure Command-Line application is a great tool to experiment with the
 * available options.
 */
#if EXAMPLE == 1
int main() 
{
	PDFSecureInit(username, company, lic_key);
	PDFSecureRun("-o ../TestOutput/test1 -s 128 -u secret ../../samples/TestFiles", MyCallback, 0);
	cout << "Done!" << endl;
	return 0;
}
#endif

/**
 * An example of how to use PDFSecure SDK to implement a command-line application
 * for processing of PDF documents.
 */
#if EXAMPLE == 2
int main(int argc, char** argv) 
{
	PDFSecureInit(username, company, lic_key);
	stringstream s;
	for (int i=1; i<argc; ++i) {
		s << argv[i] << ' ';
	}

	PDFSecureRun((char*)s.str().c_str(), MyCallback, 0);
	cout << "Done!" << endl;
	return 0;
}
#endif

/** 
 * A more comlicated example that illustrates how to dynamically build a command 
 * string based on user security settings...
 * Please note that this sample does not cover all possible options offered by 
 * PDFSecure. For a detailed explanation of all commands, please refer to PDFSecure 
 * User Manual.
 */
#if EXAMPLE == 3
int main() 
{
	PDFSecureInit(username, company, lic_key);

	bool linearized_output = true;
	bool aes_encryption = false;
	string output_folder = "../TestOutput/test1";
	string open_password = "mypass";
	string title = "My Title";
	string author = "Joe Doe";
	string subject = "My Subject";
	string keywords = "key1 key2 key2";
    string creator = "PDFTron PDFNet";
    string producer = "PDFSecure";

	// Permissions
	bool printing = false;
	bool high_rez_printing = false;
	bool doc_modifications = true;  // Changing the document
	bool content_extraction = true;
	bool commenting = true;
	bool forms_editing = true;
	bool accessibility = true;
	bool document_assembly = true;

	// Given the above settings build a command string.
	stringstream s;

	if (!output_folder.empty()) s << "-o " << output_folder << " ";

	if (aes_encryption) {
		s << "-s AES ";  // Use 128-bit AES (Advanced Encryption Standard); Supported in PDF 1.6 (Acrobat 7) and above.
	}
	else {
		s << "-s 128 ";  // Use 128-bit RC4 encryption. Supported in PDF 1.4 (Acrobat 5) and above.
		// To remove security use:
		//  s << "-s R";  etc.
	}

	if (!open_password.empty()) s << "--userpass " << open_password << " ";

	// Set the permissions.
	s << "-e a -d \"";
	if (printing == false) s << "p";
	if (high_rez_printing == false) s << "h";
	if (doc_modifications == false) s << "m";
	if (content_extraction == false)  s << "c";
	if (commenting == false)  s << "o";
	if (forms_editing == false)  s << "f";
	if (accessibility == false)  s << "x";
	if (document_assembly == false)  s << "s";
	s << "\" ";

	if (linearized_output) s << "-l ";

	// Set document information.
	if (!title.empty())  s << "--title \"" << title << "\" ";
	if (!author.empty())  s << "--author \"" << author << "\" ";
	if (!subject.empty())  s << "--subject \"" << subject << "\" ";
	if (!keywords.empty())  s << "--keywords \"" << keywords << "\" ";
	if (!creator.empty())  s << "--creator \"" << creator << "\" ";
	if (!producer.empty()) s << "--producer \"" << producer << "\" ";

	// specify input files and folders...
	s << "../../samples/TestFiles/black.pdf ";
	s << "../../samples/TestFiles/blue.pdf "; 
	// ...

	// Execute the command string.
	vector<string> saved_files;
	PDFSecureRun((char*)s.str().c_str(), MyCallback, &saved_files);

	//print the saved file(s)
	cout << "The Saved Files Are:" << endl;
	for(unsigned int i=0; i<saved_files.size(); i++)
	{
		cout << saved_files[i] << endl;
	}

	cout << "Done!" << endl;
	return 0;
}
#endif

