Changeset 10769


Ignore:
Timestamp:
Jun 16, 2010, 2:58:44 AM (12 years ago)
Author:
Longinus00
Message:

(qt) #3147: qtr delayed to connect to remote session automatically

Location:
trunk/qt
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/mainwin.cc

    r10730 r10769  
    710710    const QUrl url( mySession.getRemoteUrl( ) );
    711711    if( !url.isEmpty() )
    712         title += tr( " - %1" ).arg( url.toString(QUrl::RemoveUserInfo) );
     712        title += tr( " - %1:%2" ).arg( url.host() ).arg( url.port() );
    713713    setWindowTitle( title );
    714714}
  • trunk/qt/session.cc

    r10738 r10769  
    2020#include <QDesktopServices>
    2121#include <QMessageBox>
     22#include <QNetworkReply>
     23#include <QNetworkRequest>
    2224#include <QSet>
    2325#include <QStyle>
     
    221223    myCumulativeStats = myStats;
    222224
    223     connect( &myHttp, SIGNAL(requestStarted(int)), this, SLOT(onRequestStarted(int)));
    224     connect( &myHttp, SIGNAL(requestFinished(int,bool)), this, SLOT(onRequestFinished(int,bool)));
    225     connect( &myHttp, SIGNAL(dataReadProgress(int,int)), this, SIGNAL(dataReadProgress()));
    226     connect( &myHttp, SIGNAL(dataSendProgress(int,int)), this, SIGNAL(dataSendProgress()));
    227     connect( &myHttp, SIGNAL(authenticationRequired(QString, quint16, QAuthenticator*)), this, SIGNAL(httpAuthenticationRequired()) );
     225    connect( &myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)) );
     226    connect( &myNAM, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SIGNAL(httpAuthenticationRequired()) );
    228227    connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
    229 
    230     myBuffer.open( QIODevice::ReadWrite );
    231228}
    232229
     
    243240Session :: stop( )
    244241{
    245     myHttp.abort( );
     242    foreach( Reply myReply, myReplies )
     243        myReply.networkReply->abort();
    246244    myUrl.clear( );
    247245
     
    275273        url.setHost( host );
    276274        url.setPort( port );
     275        url.setPath( "/transmission/rpc" );
    277276        if( auth ) {
    278277            url.setUserName( user );
     
    280279        }
    281280        myUrl = url;
    282 
    283         myHttp.setHost( host, port );
    284         myHttp.setUser( user, pass );
    285281    }
    286282    else
     
    571567
    572568void
    573 Session :: exec( const char * request )
     569Session :: exec( const char * json )
    574570{
    575571    if( mySession  )
    576572    {
    577         tr_rpc_request_exec_json( mySession, request, strlen( request ), localSessionCallback, this );
     573        tr_rpc_request_exec_json( mySession, json, strlen( json ), localSessionCallback, this );
    578574    }
    579575    else if( !myUrl.isEmpty( ) )
    580576    {
    581         static const QString path( "/transmission/rpc" );
    582         QHttpRequestHeader header( "POST", path );
    583         header.setValue( "User-Agent", QCoreApplication::instance()->applicationName() + "/" + LONG_VERSION_STRING );
    584         header.setValue( "Content-Type", "application/json; charset=UTF-8" );
     577        QNetworkRequest request;
     578        request.setUrl( myUrl );
     579        request.setRawHeader( "User-Agent", QString( QCoreApplication::instance()->applicationName() + "/" + LONG_VERSION_STRING ).toAscii() );
     580        request.setRawHeader( "Content-Type", "application/json; charset=UTF-8" );
    585581        if( !mySessionId.isEmpty( ) )
    586             header.setValue( TR_RPC_SESSION_ID_HEADER, mySessionId );
     582            request.setRawHeader( TR_RPC_SESSION_ID_HEADER, mySessionId.toAscii() );
     583
    587584        QBuffer * reqbuf = new QBuffer;
    588         reqbuf->setData( QByteArray( request ) );
    589         myHttp.request( header, reqbuf, &myBuffer );
     585        reqbuf->setData( QByteArray( json ) );
     586
     587        QNetworkReply * reply = myNAM.post( request, reqbuf );
     588        connect( reply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(dataReadProgress()));
     589        connect( reply, SIGNAL(uploadProgress(qint64,qint64)), this, SIGNAL(dataSendProgress()));
     590
     591        Reply myReply;
     592        myReply.networkReply = reply;
     593        myReply.buffer = reqbuf;
     594        myReplies << myReply;
    590595#ifdef DEBUG_HTTP
    591         std::cerr << "sending " << qPrintable(header.toString()) << "\nBody:\n" << request << std::endl;
     596        std::cerr << "sending " << "POST " << qPrintable( myUrl.path() ) << std::endl;
     597        foreach( QByteArray b, request.rawHeaderList() )
     598            std::cerr << b.constData()
     599                      << ": "
     600                      << request.rawHeader( b ).constData()
     601                      << std::endl;
     602        std::cerr << "Body:\n" << json << std::endl;
    592603#endif
    593604    }
     
    595606
    596607void
    597 Session :: onRequestStarted( int id )
    598 {
    599     Q_UNUSED( id );
    600 
    601     assert( myBuffer.atEnd( ) );
    602 }
    603 
    604 void
    605 Session :: onRequestFinished( int id, bool error )
    606 {
    607     Q_UNUSED( id );
    608     QIODevice * sourceDevice = myHttp.currentSourceDevice( );
    609 
    610     QHttpResponseHeader response = myHttp.lastResponse();
     608Session :: onFinished( QNetworkReply * reply )
     609{
     610    QBuffer * buffer;
     611    for( QList<Reply>::iterator i = myReplies.begin(); i != myReplies.end(); ++i )
     612    {
     613        if( reply == i->networkReply )
     614        {
     615            buffer = i->buffer;
     616            myReplies.erase( i );
     617            break;
     618        }
     619    }
    611620
    612621#ifdef DEBUG_HTTP
    613     std::cerr << "http request " << id << " ended.. response header: "
    614               << qPrintable( myHttp.lastResponse().toString() )
    615               << std::endl
    616               << "json: " << myBuffer.buffer( ).constData( )
    617               << std::endl;
     622    std::cerr << "http response header: " << std::endl;
     623    foreach( QByteArray b, reply->rawHeaderList() )
     624        std::cerr << b.constData()
     625                  << ": "
     626                  << reply->rawHeader( b ).constData()
     627                  << std::endl;
     628    std::cerr << "json:\n" << reply->peek( reply->bytesAvailable() ).constData() << std::endl;
    618629#endif
    619630
    620     if( ( response.statusCode() == 409 ) && ( myBuffer.buffer().indexOf("invalid session-id") != -1 ) )
     631    if( ( reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt() == 409 )
     632        && ( reply->hasRawHeader( TR_RPC_SESSION_ID_HEADER ) ) )
    621633    {
    622634        // we got a 409 telling us our session id has expired.
    623635        // update it and resubmit the request.
    624         mySessionId = response.value( TR_RPC_SESSION_ID_HEADER );
    625         exec( qobject_cast<QBuffer*>(sourceDevice)->buffer().constData() );
    626     }
    627     else if( error )
    628     {
    629         std::cerr << "http error: " << qPrintable(myHttp.errorString()) << std::endl;
     636        mySessionId = QString( reply->rawHeader( TR_RPC_SESSION_ID_HEADER ) );
     637        exec( buffer->buffer().constData() );
     638    }
     639    else if( reply->error() != QNetworkReply::NoError )
     640    {
     641        std::cerr << "http error: " << qPrintable( reply->errorString() ) << std::endl;
    630642    }
    631643    else
    632644    {
    633         const QByteArray& response( myBuffer.buffer( ) );
     645        const QByteArray response( reply->readAll() );
    634646        const char * json( response.constData( ) );
    635647        int jsonLength( response.size( ) );
     
    638650    }
    639651
    640     delete sourceDevice;
    641     myBuffer.buffer( ).clear( );
    642     myBuffer.reset( );
    643     assert( myBuffer.bytesAvailable( ) < 1 );
     652    delete buffer;
     653    reply->deleteLater();
    644654}
    645655
  • trunk/qt/session.h

    r9868 r10769  
    1818#include <QBuffer>
    1919#include <QFileInfoList>
     20#include <QNetworkAccessManager>
    2021#include <QString>
    21 #include <QHttp>
    2222#include <QUrl>
    2323
     
    3232
    3333class Prefs;
     34
     35struct Reply
     36{
     37    QNetworkReply * networkReply;
     38    QBuffer * buffer;
     39};
     40typedef QList<Reply> ReplyList;
    3441
    3542class Session: public QObject
     
    7683
    7784    public:
    78         void exec( const char * request );
     85        void exec( const char * json );
    7986        void exec( const struct tr_benc * request );
    8087
     
    117124
    118125    private slots:
    119         void onRequestStarted( int id );
    120         void onRequestFinished( int id, bool error );
     126        void onFinished( QNetworkReply * reply );
    121127
    122128    signals:
     
    141147        QString mySessionId;
    142148        QUrl myUrl;
    143         QBuffer myBuffer;
    144         QHttp myHttp;
     149        QNetworkAccessManager myNAM;
     150        ReplyList myReplies;
    145151        struct tr_session_stats myStats;
    146152        struct tr_session_stats myCumulativeStats;
Note: See TracChangeset for help on using the changeset viewer.