// This file is generated by kconfig_compiler from vpn.kcfg.
// All changes you do to this file will be lost.

#include "vpndbus.h"
#include "vpn.h"

QDBusArgument &operator<<(QDBusArgument &argument, const QStringMap & mydict)
{
    argument.beginMap( QVariant::String, QVariant::String );

    QMapIterator<QString, QString> i(mydict);
    while (i.hasNext()) {
        i.next();
        argument.beginMapEntry();
        argument << i.key() << i.value();
        argument.endMapEntry();
    }
    argument.endMap();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, QStringMap & mydict)
{
    argument.beginMap();
    mydict.clear();

    while (!argument.atEnd()) {
        QString key;
        QString value;
        argument.beginMapEntry();
        argument >> key >> value;
        argument.endMapEntry();
        mydict.insert(key, value);
    }

    argument.endMap();
    return argument;
}

VpnDbus::VpnDbus(Knm::VpnSetting * setting) : SettingDbus(setting)
{
    qDBusRegisterMetaType<QStringMap>();
    qRegisterMetaType<QStringMap>();
}

VpnDbus::~VpnDbus()
{
}

void VpnDbus::fromMap(const QVariantMap & map)
{
  Knm::VpnSetting * setting = static_cast<Knm::VpnSetting *>(m_setting);
  if (map.contains(QLatin1String(NM_SETTING_VPN_SERVICE_TYPE))) {
    setting->setServiceType(map.value(QLatin1String(NM_SETTING_VPN_SERVICE_TYPE)).value<QString>());
  }
  if (map.contains(QLatin1String(NM_SETTING_VPN_DATA))) {
      setting->setData(qdbus_cast<QStringMap>(map.value(QLatin1String(NM_SETTING_VPN_DATA))));
  }
// TODO: check if this property has been deprecated, VPNC and OpenVPN does not use it anymore;
  if (map.contains(QLatin1String(NM_SETTING_VPN_USER_NAME))) {
    setting->setUserName(map.value(QLatin1String(NM_SETTING_VPN_USER_NAME)).value<QString>());
  }
  // SECRET
  if (map.contains(QLatin1String(NM_SETTING_VPN_SECRETS))) {
      setting->setVpnSecrets(qdbus_cast<QStringMap>(map.value(QLatin1String(NM_SETTING_VPN_SECRETS))));
  }
}

QVariantMap VpnDbus::toMap()
{
  QVariantMap map;
  Knm::VpnSetting * setting = static_cast<Knm::VpnSetting *>(m_setting);
  map.insert(QLatin1String(NM_SETTING_VPN_SERVICE_TYPE), setting->serviceType());

  if (!setting->data().isEmpty())
      map.insert(QLatin1String(NM_SETTING_VPN_DATA), QVariant::fromValue(setting->data()));

  if (!setting->vpnSecrets().isEmpty())
      map.insert(QLatin1String(NM_SETTING_VPN_SECRETS), QVariant::fromValue(setting->vpnSecrets()));

  if (!setting->userName().isEmpty())
      map.insert(QLatin1String(NM_SETTING_VPN_USER_NAME), setting->userName());

  return map;
}

QVariantMap VpnDbus::toSecretsMap()
{
  //assume that the settings's vpnSecrets are ready to go
  Knm::VpnSetting * setting = static_cast<Knm::VpnSetting *>(m_setting);
  QVariantMap result;
  result.insert(QLatin1String(NM_SETTING_VPN_SECRETS), QVariant::fromValue(setting->vpnSecrets()));
  return result;
}
