/*
   
        (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 "configuration.h"

#include <qtimer.h>

#include <kapplication.h>      
#include <qstringlist.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kconfiggroup.h>


Configuration::Configuration(QString configFile) {
    /// initialises the Configuration object and reads the 
    /// configuration file 
    m_confFile = configFile;
    readSettings();
}

Configuration::~Configuration() {
}

/// reads the global parameters from the configuration file
void Configuration::readSettings() {
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    //read settings
    KConfigGroup grp = conf->group("config");
    m_startWpa = grp.readEntry("startwpa",FALSE);
    m_startWpaNoQuestion = grp.readEntry("startwpanoquestion",FALSE);
    m_startMinimized = grp.readEntry("startminimized",TRUE);
    m_useCustomWpaConf =grp.readEntry("usecustomwpaconf",FALSE);
    m_customWpaConf = grp.readEntry("customwpaconf","");
    m_ap_scan = grp.readEntry("ap_scan",1);
    delete conf;
}



/// saves the global parameters to the configuration file
void Configuration::writeSettings() {
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group("config");
    //conf->writeEntry("startdhcp",m_startDhcp);
    grp.writeEntry("startwpa",m_startWpa);
    grp.writeEntry("startwpanoquestion",m_startWpaNoQuestion);
    grp.writeEntry("startminimized",m_startMinimized);
    grp.writeEntry("usecustomwpaconf",m_useCustomWpaConf);
    grp.writeEntry("customwpaconf",m_customWpaConf);
    grp.writeEntry("ap_scan",m_ap_scan);
    delete conf;
}

int Configuration::readIpSettings(QString ssid, bool &useDhcp, QString & ipaddress, QString & netmask, QString & gateway, QString & dns1, QString & dns2, QString & domain, bool & dontOverrideGw, bool & dontOverrideDns, QStringList & dnsSearchlist)
{
    useDhcp = true;
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return -1;
    // switch 
    if (!conf->hasGroup(ssid)) return -1;
    KConfigGroup grp = conf->group(ssid);
    //read settings
    useDhcp = grp.readEntry("usedhcp",TRUE);
    ipaddress = grp.readEntry("ipaddress","");
    gateway = grp.readEntry("gateway","");
    netmask= grp.readEntry("netmask","");
    dns1 = grp.readEntry("dns1","");
    dns2 = grp.readEntry("dns2","");
    domain = grp.readEntry("domain","");
    dontOverrideGw = grp.readEntry("dontoverridegw",FALSE);
    dontOverrideDns = grp.readEntry("dontoverridedns",FALSE);
    dnsSearchlist = grp.readEntry("dnssearchlist",QStringList());
    delete conf;
    return 0;
}

int Configuration::writeIpSettings(QString ssid, bool useDhcp, QString ipaddress, QString netmask, QString gateway, QString dns1, QString dns2,QString domain, bool dontOverrideGw, bool dontOverrideDns,QStringList searchlist)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return -1;
    KConfigGroup grp = conf->group(ssid);
    grp.writeEntry("usedhcp",useDhcp);
    grp.writeEntry("ipaddress",ipaddress);
    grp.writeEntry("gateway",gateway);
    grp.writeEntry("netmask",netmask);
    grp.writeEntry("dns1",dns1);
    grp.writeEntry("dns2",dns2);
    grp.writeEntry("domain",domain);
    grp.writeEntry("dontoverridegw",dontOverrideGw);
    grp.writeEntry("dontoverridedns",dontOverrideDns);
    grp.writeEntry("dnssearchlist",searchlist);
    delete conf;
    return 0;
}


void Configuration::writeEncryption (QString ssid, bool encryption)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group(ssid);
    grp.writeEntry("encryption", encryption);
    //if (encryption) kDebug() << "Network is encrypted";
    delete conf;
}

void Configuration::readEncryption (QString ssid, bool & encryption)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group(ssid);
    encryption = grp.readEntry("encryption", TRUE);
    delete conf;
}
 
void Configuration::readScriptAfterConnect(QString ssid, QString &script, bool &useRootRights)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group(ssid);
    useRootRights = grp.readEntry("scriptafterconnectuseroot", TRUE);
    script = grp.readEntry("sriptafterconnect","");
    delete conf;
}
     
void Configuration::writeScriptAfterConnect(QString ssid, QString script, bool useRootRights)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group(ssid);
    grp.writeEntry("scriptafterconnectuseroot", useRootRights);
    grp.writeEntry("sriptafterconnect",script);
    delete conf;
}

void Configuration::readScriptBeforeDisconnect(QString ssid, QString &script, bool &useRootRights)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group(ssid);
    useRootRights = grp.readEntry("scriptbeforedisconnectuseroot", TRUE);
    script = grp.readEntry("sriptbeforedisconnect","");
    delete conf;
}

void Configuration::writeScriptBeforeDisconnect(QString ssid, QString script, bool useRootRights)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group(ssid);
    grp.writeEntry("scriptbeforedisconnectuseroot", useRootRights);
    grp.writeEntry("sriptbeforedisconnect",script);
    delete conf;
}

void Configuration::readDriver(QString &driver)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group("");
    driver = grp.readEntry("driver","");
    delete conf;
}

void Configuration::writeDriver(QString driver)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group("");
    grp.writeEntry("driver",driver);
    delete conf;
}

void Configuration::readLastSsid(QString &ssid)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group("");
    ssid = grp.readEntry("ssid","");
    delete conf;
}

void Configuration::writeLastSsid( QString ssid)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    KConfigGroup grp = conf->group("");
    grp.writeEntry("ssid",ssid);
    delete conf;
}

QStringList Configuration::getNetworks()
{
    QStringList tmpList;
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return tmpList;
    tmpList = conf->groupList();
    if (tmpList.isEmpty()) return tmpList;
    if (*(tmpList.begin())==QString("<default>")) 
        tmpList.pop_front();
    return tmpList;
}

void Configuration::deleteNetwork(QString ssid)
{
    KConfig *conf= new KConfig(m_confFile,KConfig::SimpleConfig);
    if (!conf) return;
    kDebug() << "Deleting group " <<ssid << " in " << m_confFile;
    conf->deleteGroup(ssid);
    delete conf;
}



Configuration& Config(QString confFile) {
    static Configuration conf(confFile);
    return conf;
}


mainConfigurationClass::mainConfigurationClass() {
    m_configuration= new KConfig("kwlanrc.global",KConfig::SimpleConfig);
}

mainConfigurationClass::~mainConfigurationClass() {
    delete m_configuration;
}

bool mainConfigurationClass::readMonitorInterface( QString interface) 
{
    if (!m_configuration) return TRUE;
    //read settings
    
    KConfigGroup grp = m_configuration->group("Monitoring");
    return grp.readEntry(interface,TRUE);
}


void mainConfigurationClass::writeMonitorInterface( QString interface, bool monitored)
{
    if (!m_configuration) return;
    KConfigGroup grp = m_configuration->group("Monitoring");
    grp.writeEntry(interface, monitored);
}

bool mainConfigurationClass::readConfigureInterface( QString interface) 
{
    if (!m_configuration) return TRUE;
    //read settings
    KConfigGroup grp = m_configuration->group("Configuring");
    return grp.readEntry(interface,TRUE);
}


void mainConfigurationClass::writeConfigureInterface( QString interface, bool configure)
{
    if (!m_configuration) return;
    KConfigGroup grp = m_configuration->group("Configuring");
    grp.writeEntry(interface, configure);
}

bool mainConfigurationClass::readStartMinimized()
{
    if (!m_configuration) return TRUE;
    //read settings
    KConfigGroup grp = m_configuration->group("");
    return grp.readEntry("startminimized",FALSE);
}

void mainConfigurationClass::writeStartMinimized(bool startMinimized)
{
    if (!m_configuration) return;
    KConfigGroup grp = m_configuration->group("");
    grp.writeEntry("startminimized", startMinimized);
}

bool mainConfigurationClass::readShowAvailableOnly()
{
    if (!m_configuration) return TRUE;
    //read settings
    KConfigGroup grp = m_configuration->group("");
    return grp.readEntry("showavailableonly",FALSE);
}

void mainConfigurationClass::writeShowAvailableOnly(bool showAvailable)
{
    if (!m_configuration) return;
    KConfigGroup grp = m_configuration->group("");
    grp.writeEntry("showavailableonly", showAvailable);
}

bool mainConfigurationClass::readShowCommands()
{
    if (!m_configuration) return TRUE;
    //read settings
    KConfigGroup grp = m_configuration->group("");
    return grp.readEntry("showcommands",FALSE);
}

void mainConfigurationClass::writeShowCommands(bool showCommands)
{
    if (!m_configuration) return;
    KConfigGroup grp = m_configuration->group("");
    grp.writeEntry("showcommands", showCommands);
}


mainConfigurationClass& mainConfiguration() {
    static mainConfigurationClass conf;
    return conf;
}

/*
PasswordCacheClass::PasswordCacheClass() {
    m_password = "";
    readPasswordExpire();
}

PasswordCacheClass::~PasswordCacheClass() {
    m_password.fill(0);
    kDebug() << "Password cleared by destructor";
}


void PasswordCacheClass::setPassword(const QCString &password)
{
    m_password = password;
    if (m_pwexpire !=0) QTimer::singleShot(m_pwexpire * 60000, this, SLOT( wipePassword() ));
}

const QCString& PasswordCacheClass::password() const
{
    return m_password;
}

void PasswordCacheClass::wipePassword()
{
    m_password.fill(0);
    kDebug() << "Password cleared by timer";
}

void PasswordCacheClass::readPasswordExpire() {
    QString m_confFile("kwlanrc.global");
    KConfig *conf= new KConfig(m_confFile,false,false,"config");
    if (!conf) return;
    //read settings
    m_pwexpire = conf->readNumEntry("passwordexpiretimeout", 15);
    delete conf;
}

void PasswordCacheClass::writePasswordExpire() {
    QString m_confFile("kwlanrc.global");
    KConfig *conf= new KConfig(m_confFile,false,false,"config");
    if (!conf) return;
    conf->writeEntry("passwordexpiretimeout", m_pwexpire);
    delete conf;
}

PasswordCacheClass& PasswordCache() {
    static PasswordCacheClass pw;
    return pw;
}

*/
#include "configuration.moc"
             
