Diffstat (limited to 'core/launcher/qcopbridge.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/launcher/qcopbridge.cpp | 222 |
1 files changed, 102 insertions, 120 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 @@ -22,7 +22,10 @@ #include "transferserver.h" -#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> #include <qdir.h> @@ -30,15 +33,21 @@ #include <qtextstream.h> #include <qdatastream.h> +#include <qcstring.h> #include <qstringlist.h> #include <qfileinfo.h> #include <qregexp.h> -#ifdef QWS +#include <qtimer.h> +#ifdef Q_WS_QWS #include <qcopchannel_qws.h> #endif +#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 #if defined(_OS_LINUX_) @@ -46,9 +55,11 @@ #endif +#include "launcherglobal.h" + //#define INSECURE const int block_size = 51200; -QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent , +QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent, const char* name ) : QServerSocket( port, 1, parent, name ), @@ -69,4 +80,5 @@ QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent , } sendSync = FALSE; + openConnections.setAutoDelete( TRUE ); } @@ -78,9 +90,26 @@ 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 ) { QCopBridgePI *pi = new QCopBridgePI( socket, this ); 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 QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::DisableSuspend; @@ -93,10 +122,15 @@ 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 - QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; + QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; #endif } @@ -111,73 +145,6 @@ 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()" ) { // we need to buffer it a bit @@ -186,7 +153,8 @@ 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 ); } } @@ -199,5 +167,5 @@ void QCopBridge::timerEvent( QTimerEvent * ) -QCopBridgePI::QCopBridgePI( int socket, QObject *parent , const char* name ) +QCopBridgePI::QCopBridgePI( int socket, QObject *parent, const char* name ) : QSocket( parent, name ) { @@ -210,25 +178,25 @@ QCopBridgePI::QCopBridgePI( int socket, QObject *parent , const char* name ) if ( !SyncAuthentication::isAuthorized(peeraddress) ) { state = Forbidden; - startTimer( 0 ); + close(); } else -#endif +#endif { 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 += ";"; intro += "displayname="; intro += SyncAuthentication::ownerName(); intro += ";"; send( intro ); state = Wait_USER; - - // idle timer to close connections when not used anymore - startTimer( 60000 ); - connected = TRUE; } + sendSync = FALSE; + connect( this, SIGNAL( connectionClosed() ), SLOT( myConnectionClosed() ) ); + + // idle timer to close connections when not used anymore + timer = new QTimer(this); + connect( timer, SIGNAL(timeout()), this, SLOT(myConnectionClosed()) ); + timer->start( 300000, TRUE ); } @@ -236,23 +204,44 @@ QCopBridgePI::QCopBridgePI( int socket, QObject *parent , const char* name ) 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; } void QCopBridgePI::sendDesktopMessage( const QString &msg ) { - QString str = "CALL QPE/Desktop " + msg; + QString str = "CALL QPE/Desktop " + msg; // No tr send ( str ); } +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); +} + void QCopBridgePI::send( const QString& msg ) { + if ( !isOpen() ) // eg. Forbidden + return; QTextStream os( this ); os << msg << endl; @@ -262,6 +251,8 @@ void QCopBridgePI::send( const QString& msg ) void QCopBridgePI::read() { - while ( canReadLine() ) + while ( canReadLine() ) { + timer->start( 300000, TRUE ); process( readLine().stripWhiteSpace() ); + } } @@ -284,6 +275,6 @@ void QCopBridgePI::process( const QString& message ) // we always respond to QUIT, regardless of state if ( cmd == "QUIT" ) { - send( "211 Have a nice day!" ); - delete this; + send( "211 Have a nice day!" ); // No tr + close(); return; } @@ -297,8 +288,8 @@ 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; return; @@ -309,8 +300,8 @@ 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; if ( sendSync ) { @@ -323,6 +314,5 @@ void QCopBridgePI::process( const QString& message ) // noop (NOOP) else if ( cmd == "NOOP" ) { - connected = TRUE; - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr } @@ -333,5 +323,5 @@ void QCopBridgePI::process( const QString& message ) if ( msg.count() < 3 ) { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr } else { @@ -344,11 +334,11 @@ void QCopBridgePI::process( const QString& message ) int paren = command.find( "(" ); if ( paren <= 0 ) { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr return; } 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; } @@ -362,5 +352,5 @@ void QCopBridgePI::process( const QString& message ) QStringList paramList = QStringList::split( ",", params ); if ( paramList.count() > msg.count() - 3 ) { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr return; } @@ -382,5 +372,5 @@ void QCopBridgePI::process( const QString& message ) ds << arg.toInt(); else { - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr return; } @@ -396,5 +386,5 @@ void QCopBridgePI::process( const QString& message ) } #endif - + #ifndef QT_NO_COP if ( paramList.count() ) @@ -403,5 +393,5 @@ void QCopBridgePI::process( const QString& message ) QCopChannel::send( channel.latin1(), command.latin1() ); - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr #endif } @@ -409,14 +399,6 @@ void QCopBridgePI::process( const QString& message ) // not implemented else - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } - -void QCopBridgePI::timerEvent( QTimerEvent * ) -{ - if ( connected ) - connected = FALSE; - else - connectionClosed(); -} |