示例#1
0
void FileUpload::start()
{
    //qDebug() << "FileUpload::start";
    //qDebug() << "  path: " << d_ptr->path;
    //qDebug() << "  thread: " << QThread::currentThread();
    
    FileUploadPrivate *d = static_cast<FileUploadPrivate *>(d_ptr);
    
    d->exchange = new NetworkExchange(d->request, this);
    if (d->netAccessManager) {
        d->exchange->setNetworkAccessManager(d->netAccessManager);
    }
    
    d->file = new QFile(d->path, this);
    if (!d->file->open(QFile::ReadOnly)) {
        setError(QNetworkReply::UnknownContentError, d->file->errorString());
        emit error(this->error());
        emit finished();
        return;
    }
    
    connect(d->exchange, SIGNAL(replyReceived()), SLOT(onReplyReceived()));
    connect(d->exchange, SIGNAL(uploadProgress(qint64, qint64)), SLOT(onUploadProgress(qint64, qint64)));
    connect(d->exchange, SIGNAL(readyRead()), SLOT(onReadyRead()));
    connect(d->exchange, SIGNAL(redirected(const QUrl &)), SLOT(onRedirected(const QUrl &)));
    connect(d->exchange, SIGNAL(finished()), SLOT(onFinished()));
    connect(d->exchange, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(onError(QNetworkReply::NetworkError)));
    connect(&d->txTimer, SIGNAL(timeout()), this, SLOT(onDataTxTimeout()));
    
    emit started();
    
    d->exchange->post(d->file);
    d->txTimer.start(d->txTimeout);
}
void ChartLyricsPlugin::fetch(QString artist, QString title)
{
    QUrl url("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect");
    url.addQueryItem("artist", artist);
    url.addQueryItem("song", title);

    reply = nam->get(QNetworkRequest(url));
    connect(reply, SIGNAL(finished()), this, SLOT(onReplyReceived()));
}
void ChartLyricsPlugin::abort()
{
    disconnect(reply, SIGNAL(finished()), this, SLOT(onReplyReceived()));
    reply->abort();
    reply->deleteLater();
}
/*!
   \internal
    Sends an \a event, \a message and \a data to the remote host and waits for up to
    \a timeout milliseconds for a reply.  If a reply is received, the reply's message
    string is placed in \a reply.
*/
bool QstProtocol::sendMessage( const QstMessage &message, QstMessage &reply, int timeout )
{
    QstMessage msg(message);
    QPointer<QstProtocol> safeThis(this);
    bool safeDebugOn(debug_on);
    QString safeUniqueId(uniqueId());

    last_send_cmd = message.event();

    if (state() == ConnectingState) {
        wait(4000);
    }

    if (state() == ConnectedState) {
        msg.m_msg_id = tx_msg_id++;

        if (debug_on) {
            qDebug() << ( QString("%1 QstProtocol::sendMessage(%2) msgid=%3)").
                                arg(uniqueId()).
                                arg(msg.event()).
                                arg(msg.msgId()).
                                toLatin1());
        }

        send( msg );

        QTime t1;
        t1.start();
        QTime t2;
        t2.start();
        bool first_time = true;
        while ( (state() == ConnectedState) &&
               (timeout < 0 || t1.elapsed() < timeout) ) {

            if (debug_on) {
                if (first_time || t2.elapsed() > 1000) {
                    qDebug() << ( QString("%1 QstProtocol::sendMessage(%2) ... waiting for reply").
                                        arg(uniqueId()).
                                        arg(message.event()).toLatin1());
                    t2.start();
                    first_time = false;
                }
            }

            waitForSignal(this, SIGNAL(replyReceived()), 500);

            if (!safeThis) {
                if (safeDebugOn) {
                    qDebug() << ( QString("%1 QstProtocol::sendMessage(%2) ... object deleted unexpectedly").
                                        arg(safeUniqueId).
                                        arg(message.event()).toLatin1());
                }
                reply[QLatin1String("status")] = "ERROR: Connection was terminated unexpectedly. This may be caused by an application crash.";
                reply[QLatin1String("_q_inResponseTo")] = QString("%1\n%2").arg(message.event()).arg(message.toString());
                return false;
            } else {
                if (send_msg_replies.count() > 0) {
                    if (debug_on) {
                        qDebug() << ( QString("%1 QstProtocol::sendMessage(%2) ... check replies").
                                            arg(uniqueId()).
                                            arg(message.event()).toLatin1());
                    }
                    for (int i=0; i<send_msg_replies.size(); i++) {
                        QstMessage * possible_reply = send_msg_replies.at(i);
                        if (possible_reply && possible_reply->m_msg_id == msg.m_msg_id) {
                            reply = *possible_reply;
                            delete send_msg_replies.takeAt( i );
                            if (debug_on) {
                                qDebug() << ( QString("%1 QstProtocol::sendMessage(%2) ... reply received").
                                                    arg(uniqueId()).
                                                    arg(message.event()).toLatin1());
                            }

                            onReplyReceived(&reply);
                            return true;
                        }
                    }
                }
            }
        }
        if (state() != ConnectedState) {
            reply[QLatin1String("status")] = "ERROR: Connection lost. This is likely caused by a crash in the Application Under Test.";
            reply[QLatin1String("_q_inResponseTo")] = QString("%1\n%2").arg(message.event()).arg(message.toString());
        } else {
            qDebug() << "ERROR-REPLY-TIMEOUT: " << t1.elapsed() << t2.elapsed();
            reply[QLatin1String("status")] = QLatin1String("ERROR_REPLY_TIMEOUT");
            reply[QLatin1String("_q_inResponseTo")] = QString("%1\n%2").arg(message.event()).arg(message.toString());
        }
        reply[QLatin1String("location")] = QString("%1:%2").arg(__FILE__).arg(__LINE__);
    } else {
        reply[QLatin1String("status")] = QLatin1String("ERROR_NO_CONNECTION");
        reply[QLatin1String("_q_inResponseTo")] = QString("%1\n%2").arg(message.event()).arg(message.toString());
        reply[QLatin1String("location")] = QString("%1:%2").arg(__FILE__).arg(__LINE__);
    }

    if (debug_on) {
        qDebug() << ( QString("%1 QstProtocol::sendMessage(%2) ... done. Status: %3").
                            arg(uniqueId()).
                            arg(message.event()).arg(reply[QLatin1String("status")].toString()).toLatin1());
    }

    return false;
}