-rw-r--r-- | core/launcher/qcopbridge.cpp | 210 |
1 files changed, 96 insertions, 114 deletions
diff --git a/core/launcher/qcopbridge.cpp b/core/launcher/qcopbridge.cpp index 6177a7c..f780235 100644 --- a/core/launcher/qcopbridge.cpp +++ b/core/launcher/qcopbridge.cpp @@ -23,5 +23,8 @@ -#include <qpe/qcopenvelope_qws.h> -#include <qpe/qpeapplication.h> -#include <qpe/version.h> +#ifdef Q_WS_QWS +#include <qtopia/qcopenvelope_qws.h> +#endif +#include <qtopia/qpeapplication.h> +#include <qtopia/global.h> +#include <qtopia/version.h> @@ -31,2 +34,3 @@ #include <qdatastream.h> +#include <qcstring.h> #include <qstringlist.h> @@ -34,3 +38,4 @@ #include <qregexp.h> -#ifdef QWS +#include <qtimer.h> +#ifdef Q_WS_QWS #include <qcopchannel_qws.h> @@ -38,6 +43,10 @@ +#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE +#endif +#ifndef Q_OS_WIN32 #include <pwd.h> -#include <sys/types.h> #include <unistd.h> +#include <sys/types.h> +#endif @@ -47,2 +56,4 @@ +#include "launcherglobal.h" + //#define INSECURE @@ -70,2 +81,3 @@ QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent , sendSync = FALSE; + openConnections.setAutoDelete( TRUE ); } @@ -79,2 +91,14 @@ QCopBridge::~QCopBridge() +void QCopBridge::authorizeConnections() +{ + QListIterator<QCopBridgePI> it(openConnections); + while ( it.current() ) { + if ( !it.current()->verifyAuthorised() ) { + disconnect ( it.current(), SIGNAL( connectionClosed( QCopBridgePI *) ), this, SLOT( closed( QCopBridgePI *) ) ); + openConnections.removeRef( it.current() ); + } else + ++it; + } +} + void QCopBridge::newConnection( int socket ) @@ -83,3 +107,8 @@ void QCopBridge::newConnection( int socket ) openConnections.append( pi ); - connect ( pi, SIGNAL( connectionClosed( QCopBridgePI *) ), this, SLOT( connectionClosed( QCopBridgePI *) ) ); + connect ( pi, SIGNAL( connectionClosed( QCopBridgePI *) ), this, SLOT( closed( QCopBridgePI *) ) ); + + /* ### libqtopia merge FIXME */ +#if 0 + QPEApplication::setTempScreenSaverMode( QPEApplication::DisableSuspend ); +#endif #ifndef QT_NO_COP @@ -94,6 +123,11 @@ void QCopBridge::newConnection( int socket ) -void QCopBridge::connectionClosed( QCopBridgePI *pi ) +void QCopBridge::closed( QCopBridgePI *pi ) { - openConnections.remove( pi ); + emit connectionClosed( pi->peerAddress() ); + openConnections.removeRef( pi ); if ( openConnections.count() == 0 ) { + /* ### FIXME libqtopia merge */ +#if 0 + QPEApplication::setTempScreenSaverMode( QPEApplication::Enable ); +#endif #ifndef QT_NO_COP @@ -112,71 +146,4 @@ void QCopBridge::closeOpenConnections() -void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args ) +void QCopBridge::desktopMessage( const QCString &command, const QByteArray &data ) { - command.stripWhiteSpace(); - - int paren = command.find( "(" ); - if ( paren <= 0 ) { - qDebug("DesktopMessage: bad qcop syntax"); - return; - } - - QString params = command.mid( paren + 1 ); - if ( params[params.length()-1] != ')' ) { - qDebug("DesktopMessage: bad qcop syntax"); - return; - } - - params.truncate( params.length()-1 ); - - QStringList paramList = QStringList::split( ",", params ); - QString data; - if ( paramList.count() ) { - QDataStream stream( args, IO_ReadOnly ); - for ( QStringList::Iterator it = paramList.begin(); it != paramList.end(); ++it ) { - QString str; - if ( *it == "QString" ) { - stream >> str; - } else if ( *it == "QCString" ) { - QCString cstr; - stream >> cstr; - str = QString::fromLocal8Bit( cstr ); - } else if ( *it == "int" ) { - int i; - stream >> i; - str = QString::number( i ); - } else if ( *it == "bool" ) { - int i; - stream >> i; - str = QString::number( i ); - } else { - qDebug(" cannot route the argument type %s throught the qcop bridge", (*it).latin1() ); - return; - } - QString estr; - for (int i=0; i<(int)str.length(); i++) { - QChar ch = str[i]; - if ( ch.row() ) - goto quick; - switch (ch.cell()) { - case '&': - estr.append( "&" ); - break; - case ' ': - estr.append( "&0x20;" ); - break; - case '\n': - estr.append( "&0x0d;" ); - break; - case '\r': - estr.append( "&0x0a;" ); - break; - default: quick: - estr.append(ch); - } - } - data += " " + estr; - } - } - QString sendCommand = QString(command.data()) + data; - // send the command to all open connections if ( command == "startSync()" ) { @@ -187,5 +154,6 @@ void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args + // send the command to all open connections QCopBridgePI *pi; for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) { - pi->sendDesktopMessage( sendCommand ); + pi->sendDesktopMessage( command, data ); } @@ -211,3 +179,3 @@ QCopBridgePI::QCopBridgePI( int socket, QObject *parent , const char* name ) state = Forbidden; - startTimer( 0 ); + close(); } else @@ -216,9 +184,6 @@ QCopBridgePI::QCopBridgePI( int socket, QObject *parent , const char* name ) state = Connected; - sendSync = FALSE; connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); - connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); - QString intro="220 Qtopia "; intro += QPE_VERSION; intro += ";"; - intro += "challenge="; intro += SyncAuthentication::serverId(); intro += ";"; + intro += "challenge="; intro += SyncAuthentication::serverId(); intro += ";"; // No tr intro += "loginname="; intro += SyncAuthentication::loginName(); intro += ";"; @@ -227,7 +192,10 @@ QCopBridgePI::QCopBridgePI( int socket, QObject *parent , const char* name ) state = Wait_USER; + } + sendSync = FALSE; + connect( this, SIGNAL( connectionClosed() ), SLOT( myConnectionClosed() ) ); // idle timer to close connections when not used anymore - startTimer( 60000 ); - connected = TRUE; - } + timer = new QTimer(this); + connect( timer, SIGNAL(timeout()), this, SLOT(myConnectionClosed()) ); + timer->start( 300000, TRUE ); } @@ -237,10 +205,16 @@ QCopBridgePI::~QCopBridgePI() { +} +bool QCopBridgePI::verifyAuthorised() +{ + if ( !SyncAuthentication::isAuthorized(peerAddress()) ) { + state = Forbidden; + return FALSE; + } + return TRUE; } -void QCopBridgePI::connectionClosed() +void QCopBridgePI::myConnectionClosed() { emit connectionClosed( this ); - // qDebug( "Debug: Connection closed" ); - delete this; } @@ -249,3 +223,3 @@ void QCopBridgePI::sendDesktopMessage( const QString &msg ) { - QString str = "CALL QPE/Desktop " + msg; + QString str = "CALL QPE/Desktop " + msg; // No tr send ( str ); @@ -253,2 +227,15 @@ void QCopBridgePI::sendDesktopMessage( const QString &msg ) +void QCopBridgePI::sendDesktopMessage( const QCString &msg, const QByteArray& data ) +{ + if ( !isOpen() ) // eg. Forbidden + return; + const char hdr[]="CALLB QPE/Desktop "; + writeBlock(hdr,sizeof(hdr)-1); + writeBlock(msg,msg.length()); + writeBlock(" ",1); + QByteArray b64 = Opie::Global::encodeBase64(data); + writeBlock(b64.data(),b64.size()); + writeBlock("\r\n",2); +} + @@ -256,2 +243,4 @@ void QCopBridgePI::send( const QString& msg ) { + if ( !isOpen() ) // eg. Forbidden + return; QTextStream os( this ); @@ -263,5 +252,7 @@ void QCopBridgePI::read() { - while ( canReadLine() ) + while ( canReadLine() ) { + timer->start( 300000, TRUE ); process( readLine().stripWhiteSpace() ); } +} @@ -285,4 +276,4 @@ void QCopBridgePI::process( const QString& message ) if ( cmd == "QUIT" ) { - send( "211 Have a nice day!" ); - delete this; + send( "211 Have a nice day!" ); // No tr + close(); return; @@ -298,6 +289,6 @@ void QCopBridgePI::process( const QString& message ) if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { - send( "530 Please login with USER and PASS" ); + send( "530 Please login with USER and PASS" ); // No tr return; } - send( "331 User name ok, need password" ); + send( "331 User name ok, need password" ); // No tr state = Wait_PASS; @@ -310,6 +301,6 @@ void QCopBridgePI::process( const QString& message ) if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { - send( "530 Please login with USER and PASS" ); + send( "530 Please login with USER and PASS" ); // No tr return; } - send( "230 User logged in, proceed" ); + send( "230 User logged in, proceed" ); // No tr state = Ready; @@ -324,4 +315,3 @@ void QCopBridgePI::process( const QString& message ) else if ( cmd == "NOOP" ) { - connected = TRUE; - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr } @@ -334,3 +324,3 @@ void QCopBridgePI::process( const QString& message ) if ( msg.count() < 3 ) { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr } @@ -345,3 +335,3 @@ void QCopBridgePI::process( const QString& message ) if ( paren <= 0 ) { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr return; @@ -350,4 +340,4 @@ void QCopBridgePI::process( const QString& message ) QString params = command.mid( paren + 1 ); - if ( params[params.length()-1] != ')' ) { - send( "500 Syntax error, command unrecognized" ); + if ( params[(int)params.length()-1] != ')' ) { + send( "500 Syntax error, command unrecognized" ); // No tr return; @@ -363,3 +353,3 @@ void QCopBridgePI::process( const QString& message ) if ( paramList.count() > msg.count() - 3 ) { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr return; @@ -383,3 +373,3 @@ void QCopBridgePI::process( const QString& message ) else { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr return; @@ -404,3 +394,3 @@ void QCopBridgePI::process( const QString& message ) - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr #endif @@ -410,3 +400,3 @@ void QCopBridgePI::process( const QString& message ) else - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } @@ -414,9 +404 @@ void QCopBridgePI::process( const QString& message ) - -void QCopBridgePI::timerEvent( QTimerEvent * ) -{ - if ( connected ) - connected = FALSE; - else - connectionClosed(); -} |