/* Copyright (C) 2013 The giomm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

_CONFIGINCLUDE(giommconfig.h)

#include <giomm/iostream.h>
#include <giomm/tlscertificate.h>
#include <giomm/asyncresult.h>

_DEFS(giomm,gio)
_PINCLUDE(giomm/private/iostream_p.h)

namespace Gio
{

_WRAP_ENUM(TlsRehandshakeMode,GTlsRehandshakeMode)

class Cancellable;
class TlsDatabase;
class TlsInteraction;

/** TlsConnection - TLS connection type.
 * TlsConnection is the base TLS connection class type, which wraps an IOStream
 * and provides TLS encryption on top of it. Its subclasses,
 * TlsClientConnection and TlsServerConnection, implement client-side and
 * server-side TLS, respectively.
 * @newin{2,36}
 */
class TlsConnection : public IOStream
{
  _CLASS_GOBJECT(TlsConnection, GTlsConnection, G_TLS_CONNECTION, IOStream, GIOStream)

protected:
  _CTOR_DEFAULT

public:
  _WRAP_METHOD(void set_certificate(const Glib::RefPtr<TlsCertificate>& certificate), g_tls_connection_set_certificate)

  _WRAP_METHOD(Glib::RefPtr<TlsCertificate> get_certificate(), g_tls_connection_get_certificate, refreturn)
  _WRAP_METHOD(Glib::RefPtr<const TlsCertificate> get_certificate() const, g_tls_connection_get_certificate, constversion)

  _WRAP_METHOD(Glib::RefPtr<TlsCertificate> get_peer_certificate(), g_tls_connection_get_peer_certificate, refreturn)
  _WRAP_METHOD(Glib::RefPtr<const TlsCertificate> get_peer_certificate() const, g_tls_connection_get_peer_certificate, constversion)

  _WRAP_METHOD(TlsCertificateFlags get_peer_certificate_errors() const, g_tls_connection_get_peer_certificate_errors)

  _WRAP_METHOD(void set_require_close_notify(bool require_close_notify = true), g_tls_connection_set_require_close_notify)
  _WRAP_METHOD(bool get_require_close_notify() const, g_tls_connection_get_require_close_notify)

  _WRAP_METHOD(void set_rehandshake_mode(TlsRehandshakeMode mode), g_tls_connection_set_rehandshake_mode)
  _WRAP_METHOD(TlsRehandshakeMode get_rehandshake_mode() const, g_tls_connection_get_rehandshake_mode)

  _IGNORE(g_tls_connection_set_use_system_certdb, g_tls_connection_get_use_system_certdb)

  _WRAP_METHOD(Glib::RefPtr<TlsDatabase> get_database(), g_tls_connection_get_database)
  _WRAP_METHOD(Glib::RefPtr<const TlsDatabase> get_database() const, g_tls_connection_get_database, constversion)

  _WRAP_METHOD(void set_database(const Glib::RefPtr<TlsDatabase>& database), g_tls_connection_set_database)

  _WRAP_METHOD(Glib::RefPtr<TlsInteraction> get_interaction(), g_tls_connection_get_interaction)
  _WRAP_METHOD(Glib::RefPtr<const TlsInteraction> get_interaction() const, g_tls_connection_get_interaction, constversion)

  _WRAP_METHOD(void set_interaction(const Glib::RefPtr<TlsInteraction>& interaction), g_tls_connection_set_interaction)

  _WRAP_METHOD(bool handshake(const Glib::RefPtr<Cancellable>& cancellable{?}), g_tls_connection_handshake, errthrow)

  _WRAP_METHOD(void handshake_async(const SlotAsyncReady& slot{callback}, const Glib::RefPtr<Cancellable>& cancellable{.?}, int io_priority{.} = Glib::PRIORITY_DEFAULT), g_tls_connection_handshake_async, slot_name slot, slot_callback SignalProxy_async_callback)
  _WRAP_METHOD(bool handshake_finish(const Glib::RefPtr<AsyncResult>& result), g_tls_connection_handshake_finish, errthrow)

  _WRAP_METHOD(bool emit_accept_certificate(const Glib::RefPtr<const TlsCertificate>& peer_cert, TlsCertificateFlags errors), g_tls_connection_emit_accept_certificate)

  _WRAP_PROPERTY("base-io-stream", Glib::RefPtr<IOStream>)
  _WRAP_PROPERTY("certificate", Glib::RefPtr<TlsCertificate>)
  _WRAP_PROPERTY("database", Glib::RefPtr<TlsDatabase>)
  _WRAP_PROPERTY("interaction", Glib::RefPtr<TlsInteraction>)
  _WRAP_PROPERTY("peer-certificate", Glib::RefPtr<TlsCertificate>)
  _WRAP_PROPERTY("peer-certificate-errors", TlsCertificateFlags)
  _WRAP_PROPERTY("rehandshake-mode", TlsRehandshakeMode)
  _WRAP_PROPERTY("require-close-notify", bool)
  _IGNORE_PROPERTY("use-system-certdb")

#m4 _CONVERSION(`GTlsCertificate*',`const Glib::RefPtr<const TlsCertificate>&',`Glib::wrap($3, true)')
  _WRAP_SIGNAL(bool accept_certificate(const Glib::RefPtr<const TlsCertificate>& peer_cert, TlsCertificateFlags errors), "accept_certificate")

  _WRAP_VFUNC(bool handshake(const Glib::RefPtr<Cancellable>& cancellable), "handshake", errthrow)
  _WRAP_VFUNC(void handshake_async(const SlotAsyncReady& slot{callback}, const Glib::RefPtr<Cancellable>& cancellable{.}, int io_priority{.} = Glib::PRIORITY_DEFAULT), "handshake_async", slot_name slot, slot_callback SignalProxy_async_callback)

#m4 _CONVERSION(`GAsyncResult*',`const Glib::RefPtr<AsyncResult>&',`Glib::wrap($3, true)')
  _WRAP_VFUNC(bool handshake_finish(const Glib::RefPtr<AsyncResult>& result), "handshake_finish", errthrow)
};

} // namespace Gio
