#include "service.h"
#include <QString>
#include <qdbusconnection.h>
#include <QFile>
#include <syslog.h>

bool GreeterHelp::execConnWifiPWD(const QString connName,const QString password)
{
    QString tmpPath = "/tmp/kylin-nm-btoutput-lightdm";
    QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli device wifi connect '" + connName + "' password '" + password + "' > " + tmpPath;
	
    qDebug()<<"cmdStr = "<<cmdStr;
    int status =  system(cmdStr.toUtf8().data());
    if (status != 0){ syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'execConnWifiPWD' failed");}

    QFile file(tmpPath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        syslog(LOG_DEBUG, "Can't open the file /tmp/kylin-nm-btoutput !");
        qDebug()<<"Can't open the file /tmp/kylin-nm-btoutput !"<<endl;
    }
    QString line = file.readLine();
    file.close();

    if (line.indexOf("successfully") != -1) {
       return true;
    } else {
       return false;
    }
}

void GreeterHelp::execConnWifiHiden(const QString connName,const QString password)
{
    QString tmpPath = "/tmp/kylin-nm-btoutput-lightdm";
    QString cmd = "nmcli device wifi connect '" + connName + "' password '" + password + "' hidden yes > " + tmpPath;

    int status = system(cmd.toUtf8().data());
    if (status != 0) {
        syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'on_btnConnect_clicked' failed");
    }
}

void GreeterHelp::exitGreeterHelp()
{
    exit(0);
}

void GreeterHelp::nmNetworkSwitch(bool val)
{
    if(val){
        system("nmcli networking on");
    }
    else{
        system("nmcli networking off");
    }
}

GreeterHelp::GreeterHelp()
{
    bool res = QDBusConnection::systemBus().registerService("org.greeter.nm");
    if(!res){
    	exit(0);
    }
    res = QDBusConnection::systemBus().registerObject("/org/greeter/nm", this, QDBusConnection :: ExportAllSlots);
    if(!res){
    	exit(0);
    }
}

void GreeterHelp::nmConnAdd(QString conn,QString ifname)
{
      QString cmdStr = "nmcli connection add con-name '" + conn + "' ifname '" + ifname + "' type ethernet";
      system(cmdStr.toUtf8().data());
}

void GreeterHelp::nmWifiSwitch(bool val)
{
    if(val){
        system("nmcli radio wifi on");
    }else{
        system("nmcli radio wifi off");
    }
}

void GreeterHelp::kylin_network_set_manualall(QString con_name, QString addr, QString mask, QString gateway, QString dns)
{
    QString cmdStr = "nmcli connection modify '" + con_name + "' ipv4.method manual ipv4.address " + addr + "/" + mask + " ipv4.gateway " + gateway + " ipv4.dns " + dns;
    int status = system(cmdStr.toUtf8().data());
    if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection modify' in function 'kylin_network_set_manualall' failed");}
}

void GreeterHelp::kylin_network_del_ethernet_con(QString con_name)
{
    QString cmdStr = "nmcli connection delete '" + con_name + "'";
    int status = system(cmdStr.toUtf8().data());
    if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection delete' in function 'kylin_network_del_ethernet_con' failed");}
}

void GreeterHelp::kylin_network_set_automethod(QString con_name)
{
    QString cmdStr = "nmcli connection modify '" + con_name + "' ipv4.method auto";
    int status = system(cmdStr.toUtf8().data());
    if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection modify' in function 'kylin_network_set_automethod' failed");}
}

void GreeterHelp::kylin_network_set_con_down(QString con_name)
{
    QString cmdStr = "nmcli connection down '" + con_name + "'";
    int status = system(cmdStr.toUtf8().data());
    if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection down' in function 'kylin_network_set_con_down' failed");}
}

void GreeterHelp::nmConnDelete(QString conn)
{
    QString cmdStr = "nmcli connection delete '" + conn + "'";
    system(cmdStr.toUtf8().data());
}

bool GreeterHelp::kylin_network_get_autoconnect(QString con_name)
{
    QString tmpPath = "/tmp/kylin-nm-auto-lightdm";
    QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection show '" + con_name +"' |grep autoconnect: > " + tmpPath;

    qDebug()<<"cmdStr = "<<cmdStr;
    system(cmdStr.toUtf8().data());

    QFile file(tmpPath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug()<<"Can't open the file /tmp/kylin-nm-btoutput !"<<endl;
    }
    QString line = file.readLine();
    file.close();
    if(line.contains("yes"))
        return true;
    else
        return false;
}
//设置是否自动连接
bool GreeterHelp::kylin_network_set_autoconnect(QString con_name,bool autocon)
{
    char str[100];
    QString cmdStr;
    if(autocon==false)
    {
        cmdStr = "nmcli connection modify '" + con_name + "' connection.autoconnect " + "no";
    }
    else{
        cmdStr = "nmcli connection modify '" + con_name + "' connection.autoconnect " + "yes";
    }
    int status = system(cmdStr.toUtf8().data());
    if (status != 0)
        return false;
    return true;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    GreeterHelp GreeterHelp;

    return a.exec();
}
