#include "widget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>
#include <QPushButton>
#include <QThread>
#include <QTime>
#include <QFile>
#include <QSettings>

#include "../Json/sjson.h"
#include "../common.h"
#define MINUTE 60000
Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    QVBoxLayout *vMainLayout = new QVBoxLayout;
    {
        {
            QHBoxLayout *hLayout = new QHBoxLayout;
            QStringList strList;
            strList << "Data" << "Filter" << "Company" << "Catalog";
            m_plw = new QListWidget[E_LIST_MAX];
            int i = 0;
            foreach(QString str , strList)
            {
                QVBoxLayout *vLayout = new QVBoxLayout;
                vLayout->addWidget(new QLabel(str));
                vLayout->addWidget(&m_plw[i++]);
                hLayout->addLayout(vLayout);
            }
            connect(&m_plw[E_LIST_COMPANY],SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),this,SLOT(CompanyItemChanged(QListWidgetItem*,QListWidgetItem*)));
            vMainLayout->addLayout(hLayout);
        }
        {
            QHBoxLayout *hLayout = new QHBoxLayout;
            {
                QVBoxLayout *vLayout = new QVBoxLayout;
                {
                    QHBoxLayout *h_0_Layout = new QHBoxLayout;
                    {
                        QVBoxLayout *v_0_Layout = new QVBoxLayout;
                        v_0_Layout->addWidget(new QLabel("Filter Process Group"));
                        m_plwFilterProcessGroup = new QListWidget;
                        connect(m_plwFilterProcessGroup,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(FilterProcessGroupClicked(QListWidgetItem*)));
                        v_0_Layout->addWidget(m_plwFilterProcessGroup);
                        m_pleFilterProcessGroup = new QLineEdit;
                        v_0_Layout->addWidget(m_pleFilterProcessGroup);
                        {
                            QHBoxLayout *hSubLayout = new QHBoxLayout;
                            QPushButton *ppbInsert = new QPushButton("Insert");
                            connect(ppbInsert, SIGNAL(released()),this, SLOT(on_filter_process_group_insert()));
                            hSubLayout->addWidget(ppbInsert);
                            QPushButton *ppbDelete = new QPushButton("Delete");
                            connect(ppbDelete, SIGNAL(released()),this, SLOT(on_filter_process_group_delete()));
                            hSubLayout->addWidget(ppbDelete);
                            QPushButton *ppbModify = new QPushButton("Modify");
                            connect(ppbModify, SIGNAL(released()),this, SLOT(on_filter_process_group_modify()));
                            hSubLayout->addWidget(ppbModify);                            
                            v_0_Layout->addLayout(hSubLayout);
                        }                        
                        h_0_Layout->addLayout(v_0_Layout);
                    }

                    {
                        QVBoxLayout *v_0_Layout = new QVBoxLayout;
                        v_0_Layout->addWidget(new QLabel("Process List"));
                        m_plwProcess = new QListWidget;
                        v_0_Layout->addWidget(m_plwProcess);
                        {
                            QHBoxLayout *hSubLayout = new QHBoxLayout;
                            hSubLayout->addWidget(new QLabel("Timer(Minute): "));
                            m_pleExcutionTime = new QLineEdit;
                            m_pleExcutionTime->setValidator( new QIntValidator(1, 100000, this) );
                            m_pleExcutionTime->setText("60");
                            hSubLayout->addWidget(m_pleExcutionTime);
                            v_0_Layout->addLayout(hSubLayout);
                        }

                        {
                            QHBoxLayout *hSubLayout = new QHBoxLayout;
                            QPushButton *ppbInsert = new QPushButton("Insert");
                            connect(ppbInsert, SIGNAL(released()),this, SLOT(on_process_insert()));
                            hSubLayout->addWidget(ppbInsert);
                            QPushButton *ppbDelete = new QPushButton("Delete");
                            connect(ppbDelete, SIGNAL(released()),this, SLOT(on_process_delete()));
                            hSubLayout->addWidget(ppbDelete);
                            QPushButton *ppbStart = new QPushButton("Start");
                            connect(ppbStart, SIGNAL(released()),this, SLOT(on_process_start()));
                            hSubLayout->addWidget(ppbStart);
                            QPushButton *ppbStop = new QPushButton("Stop");
                            connect(ppbStop, SIGNAL(released()),this, SLOT(on_process_stop()));
                            hSubLayout->addWidget(ppbStop);

                            v_0_Layout->addLayout(hSubLayout);
                        }
                        h_0_Layout->addLayout(v_0_Layout);
                    }
                    vLayout->addLayout(h_0_Layout);

                    {
                        vLayout->addWidget(new QLabel("Filter Process"));
                        m_plwFilterProcess = new QListWidget;
                        vLayout->addWidget(m_plwFilterProcess);
                    }
                }
                {
                    QHBoxLayout *hLayoutEdit = new QHBoxLayout;
                    {
                        QPushButton *ppbLoad = new QPushButton("Load");
                        connect(ppbLoad, SIGNAL(released()),this, SLOT(on_load()));
                        hLayoutEdit->addWidget(ppbLoad);
                    }
                    {
                        QPushButton *ppbCategory = new QPushButton("Category Make");
                        connect(ppbCategory, SIGNAL(released()),this, SLOT(on_category()));
                        hLayoutEdit->addWidget(ppbCategory);
                    }
                    {
                        QPushButton *ppbSend = new QPushButton("Send");
                        connect(ppbSend, SIGNAL(released()),this, SLOT(on_send()));
                        hLayoutEdit->addWidget(ppbSend);
                    }
                    /*
                    {
                        QPushButton *ppbCount = new QPushButton("Influence Count");
                        connect(ppbCount, SIGNAL(released()),this, SLOT(on_influence_count()));
                        hLayoutEdit->addWidget(ppbCount);
                    }
                    {
                        QPushButton *ppbCount = new QPushButton("Spammers Count");
                        connect(ppbCount, SIGNAL(released()),this, SLOT(on_spammers_count()));
                        hLayoutEdit->addWidget(ppbCount);
                    }
                    {
                        QPushButton *ppbCount = new QPushButton("Power Cafe Count");
                        connect(ppbCount, SIGNAL(released()),this, SLOT(on_power_cafe_count()));
                        hLayoutEdit->addWidget(ppbCount);
                    }
                    {
                        //QPushButton *ppbCount = new QPushButton("Consumer Supervison Count");
                        //connect(ppbCount, SIGNAL(released()),this, SLOT(on_consumer_supervison_count()));
                        //hLayoutEdit->addWidget(ppbCount);
                    }
                    */
                    {
                        QPushButton *ppbDelete = new QPushButton("Delete");
                        connect(ppbDelete, SIGNAL(released()),this, SLOT(on_delete()));
                        hLayoutEdit->addWidget(ppbDelete);
                    }
                    vLayout->addLayout(hLayoutEdit);
                }
                hLayout->addLayout(vLayout);
            }

            {
                QVBoxLayout *vLayout = new QVBoxLayout;
                vLayout->addWidget(new QLabel("Log"));
                m_plwLog = new QListWidget;
                vLayout->addWidget(m_plwLog);
                hLayout->addLayout(vLayout);
            }
            vMainLayout->addLayout(hLayout);
        }
        QPushButton *ppbRefresh = new QPushButton("Refresh");
        connect(ppbRefresh, SIGNAL(released()),this, SLOT(on_refresh()));
        vMainLayout->addWidget(ppbRefresh);
    }
    /*
    QPushButton *ppbSend = new QPushButton("Send");
    vMainLayout->addWidget(ppbSend);
    connect(ppbSend, SIGNAL(released()),this, SLOT(on_send()));
    */
    connect(&m_statusTimer, SIGNAL(timeout()), this, SLOT(time_update()));
    connect(&m_executionTimer, SIGNAL(timeout()), this, SLOT(time_execute()));
    //m_pThread = new SGetThread;
    setLayout(vMainLayout);
    //m_pPutThread = new SPutThread[QThread::idealThreadCount()];
    if (ReloadSetup() == false)
    {
        InsertLog("setup.ini don't find");
    }
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    //m_statusTimer.start(5000);
    Refresh();
    for(int i = 0; i < D_PROCESS_MAX ;i++)
    {
        connect(&m_aProcess[i],SIGNAL(readyReadStandardOutput()),SLOT(process_output()));
    }
}

Widget::~Widget()
{    
    for (int i = 0; i < D_PROCESS_MAX;i++)
        Kill(i);
    exit(0);
}

void Widget::InsertLog(QString str)
{
    QTime time = QTime::currentTime();
    QString strOut = time.toString("[hh:mm:ss] ") + str;
    m_plwLog->addItem(strOut);

    if (m_plwLog->count() > 1024)
    {
        m_plwLog->removeItemWidget(m_plwLog->item(0));
        QListWidgetItem* item = m_plwLog->takeItem(0);
        delete item;
    }
    m_plwLog->setCurrentRow( m_plwLog->count() - 1 );
    m_plwLog->repaint();
}

bool Widget::ReloadSetup()
{
    QSettings settings(QString("setup.ini"), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    settings.beginGroup(QString("database"));
    if (settings.childKeys().size() == 0) return false;
    m_strWebDataBaseHost = settings.value("host").toString();
    m_strWebDataBaseUser = settings.value("user").toString();
    m_strWebDataBasePass = settings.value("pass").toString();
    m_strWebDataBaseName = settings.value("name").toString();
    settings.endGroup();
    return true;
}


void Widget::Refresh()
{    
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }

    for (int i = 0; i < E_LIST_MAX ; i++)
        m_plw[i].clear();

    {
        QSqlQuery query("select id,name from filtergroup");
        while (query.next())
        {
            QListWidgetItem *pItem = new QListWidgetItem(query.value(1).toString(),&m_plw[E_LIST_FILTER]);
            pItem->setData(Qt::UserRole, QVariant(query.value(0)));
        }
    }

    {
        QSqlQuery query("select id,name FROM datagroup");
        while(query.next())
        {
            QListWidgetItem *pItem = new QListWidgetItem(query.value(1).toString() + QString("(") + query.value(0).toString() + QString(")") ,&m_plw[E_LIST_DATA]);
            pItem->setData(Qt::UserRole, QVariant(query.value(0)));
        }
    }
    {
        m_plwFilterProcessGroup->clear();
        QSqlQuery query("select id,name,state from filterprocessgroup");
        while (query.next())
        {
            QString str;
            str = query.value(1).toString();
            /*switch(query.value(2).toInt())
            {
            case 0:str += " (idle)";break;
            case 1:str += " (warking)";break;
            }
            */
            QListWidgetItem *pItem = new QListWidgetItem(str,m_plwFilterProcessGroup);
            pItem->setData(Qt::UserRole, QVariant(query.value(0)));
        }        
    }
    m_db.close();
    FilterProcssRefresh("-1");

    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);

    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }

    {
        QSqlQuery query("select num,name FROM company");
        while(query.next())
        {
            QListWidgetItem *pItem = new QListWidgetItem(query.value(1).toString(),&m_plw[E_LIST_COMPANY]);
            pItem->setData(Qt::UserRole, QVariant(query.value(0)));
        }
    }
    m_db.close();
}

void Widget::CompanyItemChanged(QListWidgetItem* _current,QListWidgetItem*)
{
    if (_current == 0) return;
    m_db.setHostName(m_strWebDataBaseHost);
    m_db.setUserName(m_strWebDataBaseUser);
    m_db.setPassword(m_strWebDataBasePass);
    m_db.setDatabaseName(m_strWebDataBaseName);
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    m_plw[E_LIST_CATALOG].clear();

    int nServiceCnt[E_SERVICE_MAX] = {0};

    QSqlQuery query("select num,name,servicenum FROM category where company_num = '" +_current->data(Qt::UserRole).toString()+ "' order by servicenum asc, num asc");
    while(query.next())
    {
        int num = query.value(0).toInt();
        int servicenum = query.value(2).toInt();
        QString name = query.value(1).toString();
        QString strService;


        switch (servicenum - 1)
        {
        case E_SERVICE_INFLUENCER:
        {
            if (0 == nServiceCnt[E_SERVICE_INFLUENCER]++)
                continue;
            strService = "[Influencer]";
            break;
        }
        case E_SERVICE_CONSUMER:
        {
            if (0 == nServiceCnt[E_SERVICE_CONSUMER]++)
                continue;
            strService = "[Consumer]";
            break;
        }
        case E_SERVICE_SPAMMER:
        {
            if (0 == nServiceCnt[E_SERVICE_SPAMMER]++)
                continue;
            strService = "[Spammer]";
            break;
        }
        case E_SERVICE_POWERCAFE:
        {
            if (0 == nServiceCnt[E_SERVICE_POWERCAFE]++)
                continue;
            strService = "[Powercafe]";
            break;
        }
        }

        QListWidgetItem *pItem = new QListWidgetItem(strService + ":" + name ,&m_plw[E_LIST_CATALOG]);

        QList<QVariant> qlist;
        qlist.append(QVariant(num));
        qlist.append(QVariant(servicenum));
        pItem->setData(Qt::UserRole, QVariant(qlist));
    }
    m_db.close();
}

void Widget::on_filter_process_group_insert()
{
    if (m_pleFilterProcessGroup->text().trimmed().isEmpty()) return;
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false) InsertLog(m_db.lastError().text());
    QSqlQuery query;
    QString strQuery;
    strQuery = "insert into filterprocessgroup (name) value ('" + m_pleFilterProcessGroup->text() + "')";
    if(query.exec(strQuery.toUtf8()) == false) {InsertLog(query.lastError().text());}
    m_db.close();

    Refresh();
}

void Widget::on_filter_process_group_delete()
{
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false) InsertLog(m_db.lastError().text());

    foreach (QListWidgetItem *item,m_plwFilterProcessGroup->selectedItems())
    {
        QSqlQuery query;
        QString strQuery;
        strQuery = "delete from filterprocessgroup where id = " + item->data(Qt::UserRole).toString();
        if(query.exec(strQuery.toUtf8()) == false) {qDebug() << strQuery ; InsertLog(query.lastError().text());}
        strQuery = "delete from filterprocess where filterprocessgroup_id = " + item->data(Qt::UserRole).toString();
        if(query.exec(strQuery.toUtf8()) == false) {qDebug() << strQuery ; InsertLog(query.lastError().text());}
    }
    m_db.close();
    Refresh();
}

void Widget::on_filter_process_group_modify()
{
    if (m_pleFilterProcessGroup->text().trimmed().isEmpty()) return;
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false) InsertLog(m_db.lastError().text());

    foreach (QListWidgetItem *item,m_plwFilterProcessGroup->selectedItems())
    {
        QSqlQuery query;
        QString strQuery;
        strQuery = "update filterprocessgroup set name = '" + m_pleFilterProcessGroup->text().trimmed() + "' where id = " + item->data(Qt::UserRole).toString();
        if(query.exec(strQuery.toUtf8()) == false) {qDebug() << strQuery ; InsertLog(query.lastError().text());}
    }
    m_db.close();
    Refresh();
}

void Widget::FilterProcessGroupClicked(QListWidgetItem* _item)
{
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    m_pleFilterProcessGroup->setText(_item->text());
    FilterProcssRefresh(_item->data(Qt::UserRole).toString());
    m_db.close();
}

struct SFilterProcess
{
    QString m_strID;
    QString m_strName;
};

void Widget::InsertFilterProcess(int _nProcessType)
{
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");

    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    SFilterProcess astProcessFilter[E_LIST_MAX] = {{"datagroup_id","datagroup_name"},
                                                   {"filtergroup_id","filtergroup_name"},
                                                   {"company_num","company_name"},
                                                   {"category_num","category_name"}};
    QSqlQuery query;
    QString strSelect;
    foreach (QListWidgetItem *itemProcessGroup,m_plwFilterProcessGroup->selectedItems())
    {
        QString strQuery = "insert into filterprocess set ";
        strQuery += "filterprocess_type = " + QString::number(_nProcessType) + ",";
        strSelect = itemProcessGroup->data(Qt::UserRole).toString();
        strQuery += "filterprocessgroup_id = " + strSelect + ",";
        for(int i = 0; i < E_LIST_MAX; i++)
        {
            foreach (QListWidgetItem *item,m_plw[i].selectedItems())
            {
                if (i == E_LIST_CATALOG)
                {
                    QList<QVariant> qlist = item->data(Qt::UserRole).toList();
                    int nCategory = qlist.at(0).toInt();
                    int nService = qlist.at(1).toInt();
                    strQuery += astProcessFilter[i].m_strID + "=";
                    strQuery += QString::number(nCategory) + ",";
                    strQuery += astProcessFilter[i].m_strName + "='";
                    strQuery += item->text() + "',";
                    strQuery += "service_num = " + QString::number(nService) + ",";
                }
                else
                {
                    strQuery += astProcessFilter[i].m_strID + "=";
                    strQuery += item->data(Qt::UserRole).toString() + ",";
                    strQuery += astProcessFilter[i].m_strName + "='";
                    strQuery += item->text() + "',";
                }

            }
        }
        strQuery = strQuery.left(strQuery.size()-1);
        if(query.exec(strQuery.toUtf8()) == false)
        {
            InsertLog(query.lastError().text());
            qDebug() << strQuery;
        }
    }
    m_db.close();
    if (strSelect.isEmpty() == false)
        FilterProcssRefresh(strSelect);
}

void Widget::on_load()
{
    InsertFilterProcess(E_PROCSS_TYPE_LOAD);
}

void Widget::on_send()
{
    InsertFilterProcess(E_PROCSS_TYPE_SEND);
}

void Widget::on_category()
{
    InsertFilterProcess(E_PROCSS_TYPE_CATEGORY_MAKE);
}

void Widget::on_influence_count()
{
    InsertFilterProcess(E_PROCSS_TYPE_INFLUENCE_COUNT);
}

void Widget::on_spammers_count()
{
    InsertFilterProcess(E_PROCSS_TYPE_SPAMMERS_COUNT);
}

void Widget::on_power_cafe_count()
{
    InsertFilterProcess(E_PROCSS_TYPE_POWER_CAFE_COUNT);
}

void Widget::on_consumer_supervison_count()
{
    //ConsumerSupervisionCount();
}

void Widget::on_delete()
{
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    foreach (QListWidgetItem *item,m_plwFilterProcess->selectedItems())
    {
        QSqlQuery query;
        QString sql("delete from filterprocess where id = " + item->data(Qt::UserRole).toString());
        if(query.exec(sql) == false)
        {
            InsertLog(query.lastError().text());
        }
    }
    m_db.close();

    foreach (QListWidgetItem *itemProcessGroup,m_plwFilterProcessGroup->selectedItems())
        FilterProcssRefresh(itemProcessGroup->data(Qt::UserRole).toString());
}

void Widget::FilterProcssRefresh(QString _strGroupID)
{        
    qDebug() << __FILE__ << __LINE__;
    m_db.setHostName("bigbird.iptime.org");
    m_db.setUserName("admin");
    m_db.setPassword("admin123");
    m_db.setDatabaseName("concepters");
    if (m_db.open() == false)
    {
        InsertLog(m_db.lastError().text());
        return;
    }
    QString sql("select id,filterprocess_type,filterprocess_order,datagroup_id,datagroup_name,filtergroup_name,company_name,category_name from filterprocess where filterprocessgroup_id = " + _strGroupID);
    QSqlQuery query;
    if(query.exec(sql) == false)
    {
        InsertLog(query.lastError().text());
    }
    m_plwFilterProcess->clear();
    while(query.next())
    {
        QString str;
        switch(query.value(1).toInt())
        {
        case E_PROCSS_TYPE_LOAD:
            str += "Loading : Data [";
            str += query.value(4).toString();
            str += "]";
            break;
        case E_PROCSS_TYPE_CATEGORY_MAKE:
            str += "Category filter [";
            str += query.value(5).toString();
            str += "] Make Category [";
            str += query.value(7).toString();
            str += "]";
            break;
        case E_PROCSS_TYPE_SEND:
            str += "Send : Company [";
            str += query.value(6).toString();
            str += "]";
            break;
        case E_PROCSS_TYPE_INFLUENCE_COUNT:
            str += "Influence Count filter [" ;
            str += query.value(5).toString();
            str += "] and Send : Company [";
            str += query.value(6).toString();
            str += "]";
            break;
        case E_PROCSS_TYPE_SPAMMERS_COUNT:
            str += "Spammers Count filter [" ;
            str += query.value(5).toString();
            str += "] and Send : Company [";
            str += query.value(6).toString();
            str += "]";
            break;
        case E_PROCSS_TYPE_POWER_CAFE_COUNT:
            str += "Power Cafe Count filter [" ;
            str += query.value(5).toString();
            str += "] and Send : Company [";
            str += query.value(6).toString();
            str += "]";
            break;
        }
        QListWidgetItem *pItem = new QListWidgetItem(str,m_plwFilterProcess);
        pItem->setData(Qt::UserRole, QVariant(query.value(0)));
    }
    m_db.close();    
}

void Widget::on_refresh()
{
    Refresh();
}

void Widget::time_update()
{    
    process_refresh();
}

void Widget::process_refresh()
{
    m_plwProcess->clear();
    for(int i = 0; i < D_PROCESS_MAX; i++)
    {
        if (m_aProcess[i].arguments().size() == 0) continue;
        QString str = m_aProcess[i].arguments().at(1);
        switch(m_aProcess[i].state())
        {
        case QProcess::NotRunning:str += "(Wait)";break;
        case QProcess::Running:str += "(Run)";break;
        case QProcess::Starting:str += "(Start)";break;
        }
        QListWidgetItem *pItem = new QListWidgetItem(str,m_plwProcess);
        pItem->setData(Qt::UserRole, QVariant(m_aProcess[i].arguments().at(0)));
    }

}

void Widget::on_process_insert()
{
    foreach(QListWidgetItem *item,m_plwFilterProcessGroup->selectedItems())
    {
        QStringList strlist;
        strlist.append(item->data(Qt::UserRole).toString());
        strlist.append(item->text().trimmed());
        for (int i = 0; i < D_PROCESS_MAX;i++)
        {
            if (m_aProcess[i].arguments().size() == 0)
            {

#if defined(Q_OS_WIN32)
                m_aProcess[i].setProgram("FilterProcess.exe");

#else
                m_aProcess[i].setProgram("./FilterProcess");

#endif
                m_aProcess[i].setArguments(strlist);
                break;
            }
        }
    }
    process_refresh();
}

void Widget::on_process_start()
{

    bool ok;
    int time = m_pleExcutionTime->text().toInt(&ok);
    if(!ok)
        return;

    if (m_statusTimer.isActive() == false)
        m_statusTimer.start(5000);

    if (m_executionTimer.isActive() == false)
        m_executionTimer.start(time*MINUTE);

    time_update();
    time_execute();
}

void Widget::on_process_stop()
{
    if (m_statusTimer.isActive() == true)
        m_statusTimer.stop();
    if (m_executionTimer.isActive() == true)
        m_executionTimer.stop();

    process_refresh();
}

void Widget::on_process_delete()
{
    foreach(QListWidgetItem *item,m_plwProcess->selectedItems())
    {
        for (int i = 0; i < D_PROCESS_MAX;i++)
        {
            if (m_aProcess[i].arguments().size() > 0 && m_aProcess[i].arguments().at(0) == item->data(Qt::UserRole).toString())
            {
                Kill(i);
                m_aProcess[i].setArguments(QStringList());
                process_refresh();
                return;
            }
        }
    }
}


void Widget::time_execute()
{
    for(int i = 0; i < D_PROCESS_MAX; i++)
    {
        if (m_aProcess[i].arguments().size() == 0) continue;

        switch(m_aProcess[i].state())
        {
        case QProcess::NotRunning: m_aProcess[i].start();
            break;
        }
    }

    process_refresh();
}

void Widget::process_output()
{
    QProcess *pPro = (QProcess*)sender();
    QString str = pPro->readAllStandardOutput();
    InsertLog(str);
}

void Widget::Kill(int _nProcess)
{
    if (m_aProcess[_nProcess].state() == QProcess::Running)
    {
        m_aProcess[_nProcess].kill();
        m_aProcess[_nProcess].waitForFinished();
        m_aProcess[_nProcess].setArguments(QStringList());
    }
}
