/*
   
        (c) 2006 by Thomas Michel <tom.michel@arcor.de>

        Kwlan is free software; you can redistribute it and/or modify
        it under the terms of the GNU Library General Public License as
        published by the Free Software Foundation; either version 2 of
        the License, or (at your option) any later version.

        Kwlan 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 Library General Public License for more details.

        You should have received a copy of the GNU Library General Public License
        along with this library; see the file COPYING.LIB.  If not, write to
        the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
        Boston, MA 02110-1301, USA.
*/

#include <stdlib.h>

#include "kuserdatarequestdlg.h"
#include "wpa_ctrl.h"
#include "kwlanmainwin.h"
#include "kwlaninterface.h"

#include <qlabel.h>
#include <qlineedit.h>
#include <klocale.h>

KUserDataRequestDlg::KUserDataRequestDlg(QWidget* parent, const char* name, bool modal, Qt::WFlags fl)
    : QDialog(parent,fl)//,name,modal,fl) //KUserDataRequest(parent,name, modal,fl)
{
    setupUi(this);
}

KUserDataRequestDlg::~KUserDataRequestDlg()
{}

/*$SPECIALIZATION$*/
int KUserDataRequestDlg::setParams( KwlanInterface *interface, const char * requestMessage )
{
    char *tmp, *pos, *pos2;
    m_interface = interface;
    tmp = strdup(requestMessage);
    if (tmp == NULL)
        return -1;
    pos = strchr(tmp, '-');
    if (pos == NULL) {
        free(tmp);
        return -1;
    }
    *pos++ = '\0';
    m_field = tmp;
    pos2 = strchr(pos, ':');
    if (pos2 == NULL) {
        free(tmp);
        return -1;
    }
    *pos2++ = '\0';
    
    m_networkId = atoi(pos);
    queryInfo->setText(pos2);
    if (strcmp(tmp, "PASSWORD") == 0) {
        queryField->setText(i18n("Password: "));
        queryEdit->setEchoMode(QLineEdit::Password);
    } else if (strcmp(tmp, "NEW_PASSWORD") == 0) {
        queryField->setText(i18n("New password: "));
        queryEdit->setEchoMode(QLineEdit::Password);
    } else if (strcmp(tmp, "IDENTITY") == 0)
        queryField->setText(i18n("Identity: "));
    else if (strcmp(tmp, "PASSPHRASE") == 0) {
        queryField->setText(i18n("Private key passphrase: "));
        queryEdit->setEchoMode(QLineEdit::Password);
    } else
        queryField->setText(m_field + ":");
    free(tmp);
    return 0;
}

void KUserDataRequestDlg::sendReply()
{ 
    if (m_interface == NULL) {
        reject();
        return;
    }
    char reply[10];
    size_t reply_len = sizeof(reply);
    
    
    QString cmd = QString(WPA_CTRL_RSP) + m_field + '-' +
            QString::number(m_networkId) + ':' +
            queryEdit->text();
    //TODO: Add ctrlRequest to parent
    m_interface->ctrlRequest(cmd.toAscii(), reply, &reply_len);
    accept();
}



#include "kuserdatarequestdlg.moc"

