/* 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/>.
 */

#include <glibmm/interface.h>
#include <giomm/tlsconnection.h>
#include <giomm/enums.h>

_DEFS(giomm,gio)
_PINCLUDE(glibmm/private/interface_p.h)
_PINCLUDE(gio/gio.h)

#ifndef DOXYGEN_SHOULD_SKIP_THIS
typedef struct _GTlsClientConnectionInterface GTlsClientConnectionInterface;
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

namespace Gio
{

class GIOMM_API SocketConnectable;

/** TlsClientConnection - TLS client-side connection.
 * TlsClientConnection is the client-side subclass of TlsConnection,
 * representing a client-side TLS connection.
 * @newin{2,36}
 */
class GIOMM_API TlsClientConnection
: public Glib::Interface,
  public TlsConnection
{
  _CLASS_INTERFACE(TlsClientConnection, GTlsClientConnection, G_TLS_CLIENT_CONNECTION, GTlsClientConnectionInterface, , , GIOMM_API)
  _CUSTOM_CTOR_CAST

public:
  //TODO: It's not possible to use _WRAP_CTOR/_WRAP_CREATE to wrap the new
  //function because this is an interface.
#m4 _CONVERSION(`GIOStream*',`Glib::RefPtr<TlsClientConnection>',`Glib::wrap(G_TLS_CLIENT_CONNECTION($3))')
  _WRAP_METHOD(static Glib::RefPtr<TlsClientConnection> create(const Glib::RefPtr<IOStream>& base_io_stream, const Glib::RefPtr<const SocketConnectable>& server_identity{?}), g_tls_client_connection_new, errthrow)

  _WRAP_METHOD(void set_server_identity(const Glib::RefPtr<SocketConnectable>& identity), g_tls_client_connection_set_server_identity)

  _WRAP_METHOD(Glib::RefPtr<SocketConnectable> get_server_identity(), g_tls_client_connection_get_server_identity, refreturn)
  _WRAP_METHOD(Glib::RefPtr<const SocketConnectable> get_server_identity() const, g_tls_client_connection_get_server_identity, constversion)

  // g_tls_client_connection_set/get_validation_flags() are deprecated in glib 2.72.
  /** Sets this client connection's validation flags, to override the default set of
   * checks performed when validating a server certificate. By default,
   * Gio::TLS_CERTIFICATE_VALIDATE_ALL is used.
   *
   * This function does not work as originally designed and is impossible
   * to use correctly.
   *
   * @newin{2,28}
   *
   * @param flags The TlsCertificateFlags to use.
   */
  _WRAP_METHOD(void set_validation_flags(TlsCertificateFlags flags),
    g_tls_client_connection_set_validation_flags, ignore_deprecations)

  /** Gets this client connection's validation flags
   *
   * This function does not work as originally designed and is impossible
   * to use correctly.
   *
   * @newin{2,28}
   *
   * @return The validation flags.
   */
  _WRAP_METHOD(TlsCertificateFlags get_validation_flags() const,
    g_tls_client_connection_get_validation_flags, ignore_deprecations)

  _WRAP_METHOD(void set_use_ssl3(bool use_ssl3 = true), g_tls_client_connection_set_use_ssl3,
    deprecated "SSL 3.0 is insecure, and this function does not generally enable or disable it, despite its name.")
  _WRAP_METHOD(bool get_use_ssl3() const, g_tls_client_connection_get_use_ssl3,
    deprecated "SSL 3.0 is insecure, and this function does not actually indicate whether it is enabled.")

#m4 _CONVERSION(`GList*',`std::vector< Glib::RefPtr<Glib::ByteArray> >',`Glib::ListHandler< Glib::RefPtr<Glib::ByteArray> >::list_to_vector($3, Glib::OWNERSHIP_DEEP)')
  _WRAP_METHOD(std::vector< Glib::RefPtr<Glib::ByteArray> > get_accepted_cas(), g_tls_client_connection_get_accepted_cas)

#m4 _CONVERSION(`GList*',`std::vector< Glib::RefPtr<const Glib::ByteArray> >',`Glib::ListHandler< Glib::RefPtr<const Glib::ByteArray> >::list_to_vector($3, Glib::OWNERSHIP_DEEP)')
  _WRAP_METHOD(std::vector< Glib::RefPtr<const Glib::ByteArray> > get_accepted_cas() const, g_tls_client_connection_get_accepted_cas)


  _WRAP_METHOD(void copy_session_state(const Glib::RefPtr<TlsClientConnection>& source), g_tls_client_connection_copy_session_state)

  // property_accepted_cas() won't work unless a Glib::Value<std::vector<Glib::RefPtr<Glib::ByteArray>>>
  // specialization is added. Workaround: Use get_accepted_cas().
  _WRAP_PROPERTY("accepted-cas", std::vector< Glib::RefPtr<Glib::ByteArray> >,
    deprecated "property_accepted_cas() does not work. Use get_accepted_cas() instead.")
  _WRAP_PROPERTY("server-identity", Glib::RefPtr<SocketConnectable>)
  _WRAP_PROPERTY("use-ssl3", bool, deprecated "SSL 3.0 is insecure, and this property does not generally enable or disable it, despite its name.")
  _WRAP_PROPERTY("validation-flags", TlsCertificateFlags)
};

} // namespace Gio
