/*
# Copyright (C) 2002 John Goerzen
# <jgoerzen@complete.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/***************
 NOTE:
 gpgme.i0 should be edited
 gpgme.i is generated by mkcallback.pl -- see the Makefile
 ***************/

%module gpgme
%include "cpointer.i"
%include "cstring.i"

// Allow use of None for strings.

%typemap(python,in) const char * {
  if ($input == Py_None)
    $1 = NULL;
  else if (PyString_Check($input))
    $1 = PyString_AsString($input);
  else {
    PyErr_Format(PyExc_TypeError,
                 "arg %d: expected string or None, got %s",
		 $argnum,
		 $input == Py_None ? "None" : $input->ob_type->tp_name);
    return NULL;
  }
}

// Release returned buffers as necessary.

%typemap(newfree) char * "free($1);";
%newobject gpgme_data_release_and_get_mem;
%newobject gpgme_key_get_as_xml;
%newobject gpgme_get_notation;
%newobject gpgme_get_op_info;

// Special handling for binary data.

// for gpgme_data_write
%apply (char *STRING, int LENGTH) {
  (const void *buffer, size_t length)
};

// Special handling for references to our objects.

%typemap(python,in) GpgmeData DATAIN, GpgmeRecipients DATAIN {
  PyObject *pyname = NULL, *pypointer = NULL;
  pyname = PyObject_CallMethod($input, "_getctype", NULL);
  if (pyname == NULL) {
    PyErr_Format(PyExc_TypeError,
		 "arg %d: expected an instance of type $1, but got %s",
		 $argnum,
		 $input == Py_None ? "None" : $input->ob_type->tp_name);
    return NULL;
  }
  if (strcmp(PyString_AsString(pyname), "$1_type") != 0) {
    PyErr_Format(PyExc_TypeError,
		 "arg %d: Expected value of type $1_type, but got %s",
		 $argnum, PyString_AsString(pyname));
    Py_DECREF(pyname);
    return NULL;
  }
  Py_DECREF(pyname);
  pypointer = PyObject_GetAttrString($input, "wrapped");
  if (pypointer == NULL) {
    PyErr_Format(PyExc_TypeError,
		 "arg %d: Use of uninitialized Python object $1_type",
		 $argnum);
    return NULL;
  }
  
  /* input = $input, 1 = $1, 1_descriptor = $1_descriptor */

  // Following code is from swig's python.swg

  if ((SWIG_ConvertPtr(pypointer,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) {
    Py_DECREF(pypointer);
    return NULL;
  }
  Py_DECREF(pypointer);
}

%apply GpgmeData DATAIN { GpgmeData plain, GpgmeData cipher, GpgmeData sig,
			    GpgmeData text, GpgmeData keydata,
			    GpgmeData pubkey, GpgmeData seckey, GpgmeData out};

%apply GpgmeRecipients DATAIN { GpgmeRecipients recp};

// Include the header file both for cc (first) and for swig (second)

%{
#include <gpgme.h>
%}
%include <gpgme.h>

// Generating and handling pointers-to-pointers.

%pointer_functions(GpgmeCtx, GpgmeCtx_p);
%pointer_functions(GpgmeData, GpgmeData_p);
%pointer_functions(GpgmeRecipients, GpgmeRecipients_p);
%pointer_functions(GpgmeKey, GpgmeKey_p);
%pointer_functions(GpgmeTrustItem, GpgmeTrustItem_p);
%pointer_functions(PyObject *, PyObject_p_p);
%pointer_functions(void *, void_p_p);

// Helper functions.

%{
#include <stdio.h>
%}
FILE *fdopen(int fildes, const char *mode);

%cstring_output_withsize(char *buffer, size_t *length);

%{
#include "helpers.h"
%}
%include "helpers.h"
