QVariantMap WirelessSecurityDbus::toSecretsMap()
{
    QVariantMap map;
    Knm::WirelessSecuritySetting *setting = static_cast<Knm::WirelessSecuritySetting *>(m_setting);
    if (setting->securityType() != Knm::WirelessSecuritySetting::EnumSecurityType::None)
        // don't return anything if there is no security
    {
        if (setting->securityType() == Knm::WirelessSecuritySetting::EnumSecurityType::StaticWep ||
                setting->securityType() == Knm::WirelessSecuritySetting::EnumSecurityType::DynamicWep)
        {
            if (setting->wepKeyType() == Knm::WirelessSecuritySetting::Hex)
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE), 1);
            else
                if (setting->wepKeyType() == Knm::WirelessSecuritySetting::Passphrase)
                    map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE), 2);
                else
                    qWarning() << "Wep key type is not set!";
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX), setting->weptxkeyindex());
            if (!setting->wepkey0().isEmpty())
            {
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_KEY0), setting->wepkey0());
            }
            if (!setting->wepkey1().isEmpty())
            {
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_KEY1), setting->wepkey1());
            }
            if (!setting->wepkey2().isEmpty())
            {
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_KEY2), setting->wepkey2());
            }
            if (!setting->wepkey3().isEmpty())
            {
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_KEY3), setting->wepkey3());
            }
        }
        else
            if (setting->securityType() == Knm::WirelessSecuritySetting::EnumSecurityType::WpaPsk || setting->securityType() == Knm::WirelessSecuritySetting::EnumSecurityType::Wpa2Psk)
            {
                if (!setting->psk().isEmpty())
                {
                    map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_PSK), setting->psk());
                }
            }
            else
                if (setting->securityType() == Knm::WirelessSecuritySetting::EnumSecurityType::Leap)
                {
                    if (!setting->leappassword().isEmpty())
                    {
                        map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD), setting->leappassword());
                    }
                }



    } // end of if not clear
    return map;
}
void WirelessInterfaceConnectionBuilder::init(WirelessInterfaceConnection *ic)
{
    InterfaceConnectionBuilder::init(ic);

    Knm::WirelessSetting * wirelessSetting = dynamic_cast<Knm::WirelessSetting *>(m_connection->setting(Knm::Setting::Wireless));
    if (wirelessSetting) {
        int strength = -1;
        NetworkManager::AccessPoint::Capabilities caps = 0;
        NetworkManager::AccessPoint::WpaFlags wpaFlags = 0;
        NetworkManager::AccessPoint::WpaFlags rsnFlags = 0;
        NetworkManager::AccessPoint::OperationMode mode
            = NetworkManager::AccessPoint::Infra;

        // show connections where the network is present OR adhoc connections
        NetworkManager::WirelessNetwork::Ptr network = m_interface->findNetwork(wirelessSetting->ssid());
        if (network) {
            strength = network->signalStrength();
            NetworkManager::AccessPoint::Ptr ap = network->referenceAccessPoint();
            if (ap) {
                caps = ap->capabilities();
                wpaFlags = ap->wpaFlags();
                rsnFlags = ap->rsnFlags();
                mode = ap->mode();
            }
        }
        else if (wirelessSetting->mode() == Knm::WirelessSetting::EnumMode::adhoc ||
                 wirelessSetting->mode() == Knm::WirelessSetting::EnumMode::apMode) {
                mode = (wirelessSetting->mode() == Knm::WirelessSetting::EnumMode::adhoc) ? NetworkManager::AccessPoint::Adhoc : NetworkManager::AccessPoint::ApMode;
                Knm::WirelessSecuritySetting * wirelessSecuritySetting = dynamic_cast<Knm::WirelessSecuritySetting *>(m_connection->setting(Knm::Setting::WirelessSecurity));
                switch( wirelessSecuritySetting->securityType())
                {
                    case Knm::WirelessSecuritySetting::EnumSecurityType::StaticWep:
                    case Knm::WirelessSecuritySetting::EnumSecurityType::Leap:
                    case Knm::WirelessSecuritySetting::EnumSecurityType::DynamicWep:
                        caps |= NetworkManager::AccessPoint::Privacy;
                        break;
                    case Knm::WirelessSecuritySetting::EnumSecurityType::WpaPsk:
                        wpaFlags |= NetworkManager::AccessPoint::KeyMgmtPsk | NetworkManager::AccessPoint::PairTkip | NetworkManager::AccessPoint::PairCcmp;
                        break;
                    case Knm::WirelessSecuritySetting::EnumSecurityType::WpaEap:
                        wpaFlags |= NetworkManager::AccessPoint::KeyMgmt8021x;
                        break;
                    case Knm::WirelessSecuritySetting::EnumSecurityType::Wpa2Psk:
                        rsnFlags |= NetworkManager::AccessPoint::KeyMgmtPsk | NetworkManager::AccessPoint::PairTkip | NetworkManager::AccessPoint::PairCcmp;
                        break;
                    case Knm::WirelessSecuritySetting::EnumSecurityType::Wpa2Eap:
                        rsnFlags |= NetworkManager::AccessPoint::KeyMgmt8021x;
                        break;
                    case Knm::WirelessSecuritySetting::EnumSecurityType::None:
                    default:
                        break;
                }
            }

        ic->m_interfaceCapabilities = m_interface->wirelessCapabilities();
        ic->m_ssid = wirelessSetting->ssid();
        ic->m_strength = strength;
        ic->m_apCapabilities = caps;
        ic->m_wpaFlags = wpaFlags;
        ic->m_rsnFlags = rsnFlags;
        ic->m_operationMode = mode;
    }
}
QVariantMap WirelessSecurityDbus::toMap()
{
    QVariantMap map;
    Knm::WirelessSecuritySetting *setting = static_cast<Knm::WirelessSecuritySetting *>(m_setting);

    if (setting->securityType() != Knm::WirelessSecuritySetting::EnumSecurityType::None)
        // don't return anything if there is no security
    {
        switch (setting->keymgmt())
        {
            case Knm::WirelessSecuritySetting::EnumKeymgmt::None:
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_KEY_MGMT), "none");
                qDebug() << "Adding key-mgmt: none";
                break;
            case Knm::WirelessSecuritySetting::EnumKeymgmt::Ieee8021x:
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_KEY_MGMT), "ieee8021x");
                qDebug() << "Adding key-mgmt: ieee8021x";
                break;
            case Knm::WirelessSecuritySetting::EnumKeymgmt::WPANone:
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_KEY_MGMT), "wpa-none");
                qDebug() << "Adding key-mgmt: wpa-none";
                break;
            case Knm::WirelessSecuritySetting::EnumKeymgmt::WPAPSK:
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_KEY_MGMT), "wpa-psk");
                qDebug() << "Adding key-mgmt: wpa-psk";
                break;
            case Knm::WirelessSecuritySetting::EnumKeymgmt::WPAEAP:
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_KEY_MGMT), "wpa-eap");
                qDebug() << "Adding key-mgmt: wpa-eap";
                break;
        }
        switch (setting->authalg())
        {
            case Knm::WirelessSecuritySetting::EnumAuthalg::none:
                // the none auth alg is internal
                break;
            case Knm::WirelessSecuritySetting::EnumAuthalg::open:
                // the default
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_AUTH_ALG), "open");
                break;
            case Knm::WirelessSecuritySetting::EnumAuthalg::shared:
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_AUTH_ALG), "shared");
                break;
            case Knm::WirelessSecuritySetting::EnumAuthalg::leap:
                map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_AUTH_ALG), "leap");
                break;
        }
        if (!setting->proto().isEmpty())
        {
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_PROTO), setting->proto());
        }
        if (!setting->pairwise().isEmpty())
        {
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_PAIRWISE), setting->pairwise());
        }
        if (!setting->group().isEmpty())
        {
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_GROUP), setting->group());
        }
        if (!setting->leapusername().isEmpty())
        {
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME), setting->leapusername());
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD_FLAGS), (int)setting->leappasswordflags());
        }

        map.unite(toSecretsMap());

        if (!setting->wepkey0().isEmpty() || !setting->wepkey1().isEmpty() ||  !setting->wepkey2().isEmpty() || !setting->wepkey3().isEmpty())
        {
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_WEP_KEY_FLAGS), (int)setting->wepkeyflags());
        }
        if (!setting->psk().isEmpty())
        {
            map.insert(QLatin1String(NM_SETTING_WIRELESS_SECURITY_PSK_FLAGS), (int)setting->pskflags());
        }

    } // end of if not setting->clear()
    return map;
}