Exemplo n.º 1
0
void DataInput::startQml()
{
    QNetworkRequest nr;
    QString url;
    int typeNumber = typeToInt(type);
    switch(typeNumber)
    {
    case 0: {
        url = "http://api.thingm.com/blink1/events/" + iftttKey;
//        url = "http://api.thingm.com/blink1/events/04CE5FA11A002B8E";
        nr.setUrl(QUrl(url));
        reply = networkManager->get(nr);
        connect(reply, SIGNAL(finished()), this, SLOT(onFinishedQml()));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
    }
        break;

    case 1: {
        url = rule;
        QUrl correctUrl(url);
        if(correctUrl.isValid())
        {
            nr.setUrl(QUrl(url));
            reply = networkManager->get(nr);
            connect(reply, SIGNAL(finished()), this, SLOT(onFinishedQml()));
            connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
        }
        else
        {
            emit setValueRet("NOT FOUND");
            emit toDelete(this);
        }
    }
        break;

    case 2: {
        QFile f(rule);
        if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            //qDebug() << "File error:";
            //qDebug() << "File not found.";
            emit setValueRet("NOT FOUND");
            emit toDelete(this);
            return;
        }
        QString txt = "";
        QTextStream in(&f);
        txt.append(in.readAll());
        int idx=txt.indexOf(QRegExp("#([0-9a-fA-F]{6})"));
        QColor c=QColor("#000000");
        f.close();
        if(idx!=-1)
        {
            c=QColor(txt.mid(idx,7));
            emit setColor(c);
            emit setValueRet(c.name().toUpper());
        }
        else
        {
            emit setColor(c);
            emit setValueRet("NO VALUE");

        }
        emit toDelete(this);
    }
        break;

    case 3: {
        process = new QProcess;
        connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(onProcessOutput()));
        connect(process, SIGNAL(readyReadStandardError()), this, SLOT(onError()));
        connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError()));
        connect(process, SIGNAL(finished(int)), this, SLOT(onProcessFinishedQml()));
        QString path = QStandardPaths::locate(QStandardPaths::DocumentsLocation, rule);
        QFile f(path);
        if(f.exists())
            process->start(path);
        else
        {
            //qDebug() << "Script error:";
            //qDebug() << "Script doesn't' exist!";
            emit setValueRet("NOT FOUND");
            emit toDelete(this);
        }
    }
        break;

    default: {
        emit toDelete(this);
    }
        break;
    }
}
Exemplo n.º 2
0
void DataInput::start()
{
    QNetworkRequest nr;
    QString url;
    //qDebug() << "DataInput:start:"<<type<<":"<<input->arg1();

    if( type == "ifttt" ) {
        // url = "http://api.thingm.com/blink1/eventsall/" + iftttKey;
        url = "http://feed.thingm.com/blink1/eventsall/" + iftttKey;
        //url = "http://localhost:3232/blink1/eventsall/" + iftttKey;
        nr.setUrl(QUrl(url));
        reply = networkManager->get(nr);
        connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
    }
    else if( type == "url" ) { 
        url = input->arg1();
        //qDebug() << "datainput:start url: "<<url; 
        if(!url.startsWith("http://") && !url.startsWith("https://"))
            url="http://"+url;
        QUrl correctUrl(url);
        if(correctUrl.isValid()) {
            nr.setUrl(QUrl(url));
            reply = networkManager->get(nr);
            connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
            connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
        }
        else {
            input->setArg2("Bad URL");
            input->setDate(-1);  // FIXME: don't like -1 here
            emit toDelete(this);
        }
    }
    else if( type == "file" ) { 
        QFileInfo fileInfo;
        fileInfo.setFile(input->arg1());
        if( !fileInfo.exists() ) {
            qDebug() << "datainput:start: no file";
            input->setArg2("Not Found");
            input->setDate(-1);
        }
        else { 
            int lastModTime = fileInfo.lastModified().toTime_t(); // why was cast to uint?
            if( lastModTime > input->date()) {
                qDebug() << "datainput:start: file newer";
                QFile f(input->arg1());
                if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    input->setArg2("Couldn't Open");
                    input->setDate(-1);  // FIXME: why -1? what does it mean?
                    emit toDelete(this);
                    return;
                }
                input->setDate( lastModTime); //fileInfo.lastModified().toTime_t());
                QString txt = "";
                QTextStream in(&f);
                txt.append(in.readAll());

                bool good = parsePatternOrColor( txt, type, lastModTime );
                if( !good ) { 
                    input->setArg2("Bad Parse");
                }
            } // last modified
            else { 
                //input->setArg2("Old File");  // FIXME: should do something to indicate older file
                //input->setDate(-1);
            }
        }
        emit toDelete(this);
    }
    else if( type == "script" ) { 
        //QString path = QStandardPaths::locate(QStandardPaths::DocumentsLocation, input->arg1());
        QFileInfo fileInfo;
        fileInfo.setFile( input->arg1() );
        if( !fileInfo.exists() ) {
            input->setArg2("Not Found");
            input->setDate(-1);
            emit toDelete(this);
        } 
        else if( !fileInfo.isExecutable() ) { 
            input->setArg2("Not Executable");
            input->setDate(-1);
            emit toDelete(this);
        }
        else { 
            // FIXME: should check new value compare to lastVal
            // (and FIXME: need to refactor to properly use lastVal for all monitor types)
            //if(fileInfo.lastModified().toTime_t() != (uint)input->date()){
            // no, don't do lastModTime check on exec file, jeez
            input->setDate(fileInfo.lastModified().toTime_t());
            process = new QProcess;
            connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(onProcessOutput()));
            connect(process, SIGNAL(readyReadStandardError()), this, SLOT(onError()));
            connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError()));
            connect(process, SIGNAL(finished(int)), this, SLOT(onProcessFinished()));
            // start process running
            process->start( fileInfo.canonicalFilePath() );
        }
    }
Exemplo n.º 3
0
void DataInput::start()
{
    QNetworkRequest nr;
    QString url;
    int typeNumber = typeToInt(type);
    switch(typeNumber)
    {
    case 0: {
        url = "http://api.thingm.com/blink1/eventsall/" + iftttKey;
//        url = "http://api.thingm.com/blink1/events/04CE5FA11A002B8E";
        nr.setUrl(QUrl(url));
        reply = networkManager->get(nr);
        connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
    }
        break;

    case 1: {
        url = input->arg1();
        if(!url.startsWith("http://") && !url.startsWith("https://"))
            url="http://"+url;
        QUrl correctUrl(url);
        if(correctUrl.isValid())
        {
            nr.setUrl(QUrl(url));
            reply = networkManager->get(nr);
            connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
            connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError()));
        }
        else
        {
            emit setValueRet("NOT FOUND");
            input->setArg2("NOT FOUND");
            input->setDate(-1);
            emit toDelete(this);
        }
    }
        break;

    case 2: {
        QFileInfo fileInfo;
        fileInfo.setFile(input->arg1());
        if(!fileInfo.exists()){
            input->setArg2("NOT FOUND");
            input->setDate(-1);
        }
        if(fileInfo.lastModified().toTime_t() != (uint)input->date()) {
            QFile f(input->arg1());
            if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
            {
                //qDebug() << "File error:";
                //qDebug() << "File not found.";
                emit setValueRet("NOT FOUND");
                input->setArg2("NOT FOUND");
                input->setDate(-1);
                emit toDelete(this);
                return;
            }
            input->setDate(fileInfo.lastModified().toTime_t());
            QString txt = "";
            QTextStream in(&f);
            txt.append(in.readAll());
            int idx=txt.indexOf(QRegExp("#([0-9a-fA-F]{6})"));
            QColor c=QColor("#000000");
            f.close();
            if(idx!=-1)
            {
                c=QColor(txt.mid(idx,7));
                emit setColor(c);
                emit addReceiveEvent(fileInfo.lastModified().toTime_t(), c.name().toUpper(), "FILE");
                input->setArg2(c.name());
            }
            else
            {
                QStringList list;
                list.append("pattern:"); list.append("\"pattern\":");
                QString patternName;
                int index=-1;
                for(int i=0; i<list.count(); i++) {
                    index = txt.indexOf(list.at(i),0);
                    if(index != -1)
                        break;
                }
                if(index != -1) {
                    index = txt.indexOf(":", index);
                    int idx1, idx2;
                    idx1 = txt.indexOf("\"", index);
                    idx2 = txt.indexOf("\"", idx1+1);
                    patternName = txt.mid(idx1+1, idx2-idx1-1);
                    for(int i=0; i<patternList.count(); i++) {
                        if(patternList[i] == patternName) {
                            emit runPattern(patternName, false);
                            emit addReceiveEvent(fileInfo.lastModified().toTime_t(), patternName, "FILE");
                            input->setArg2(patternName);
                            break;
                        }
                    }

                }else{
                    input->setArg2("NO VALUE");
                }
            }
        }
        emit toDelete(this);
    }
        break;

    case 3: {
        process = new QProcess;
        connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(onProcessOutput()));
        connect(process, SIGNAL(readyReadStandardError()), this, SLOT(onError()));
        connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError()));
        connect(process, SIGNAL(finished(int)), this, SLOT(onProcessFinished()));
//        QString path = QStandardPaths::displayName(QStandardPaths::DocumentsLocation);
        QString path = QStandardPaths::locate(QStandardPaths::DocumentsLocation, input->arg1());
        QFile f(path);
        QFileInfo fileInfo;
        fileInfo.setFile(path);
        if(f.exists()){
            if(fileInfo.lastModified().toTime_t() != (uint)input->date()){
                input->setDate(fileInfo.lastModified().toTime_t());
                process->start(path);
            }else{
                emit toDelete(this);
            }
        }else
        {
            //qDebug() << "Script error:";
            //qDebug() << "Script doesn't' exist!";
            emit setValueRet("NOT FOUND");
            input->setArg2("NOT FOUND");
            input->setDate(-1);
            emit toDelete(this);
        }
    }
        break;

    default: {
        emit toDelete(this);
    }
        break;
    }
}