Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qcopimpl.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2005 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 ** 
00006 ** This program is free software; you can redistribute it and/or modify it
00007 ** under the terms of the GNU General Public License as published by the
00008 ** Free Software Foundation; either version 2 of the License, or (at your
00009 ** option) any later version.
00010 ** 
00011 ** A copy of the GNU GPL license version 2 is included in this package as 
00012 ** LICENSE.GPL.
00013 **
00014 ** This program is distributed in the hope that it will be useful, but
00015 ** WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00017 ** See the GNU General Public License for more details.
00018 **
00019 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00020 **
00021 ** Contact info@trolltech.com if any conditions of this licensing are
00022 ** not clear to you.
00023 **
00024 **********************************************************************/
00025 
00026 #include "qcopimpl.h"
00027 #include <qtopia/timeconversion.h>
00028 
00029 void doqcopusage()
00030 {
00031     fprintf( stderr, "Usage: qcop [-l username] channel command [parameters] [-w channel command [timeout]]\n" );
00032 }
00033 
00034 void doqcopsyntax( const QString &where, const QString &what )
00035 {
00036     fprintf( stderr, "Syntax error in %s: %s\n", where.latin1(), what.latin1() );
00037     exit(1);
00038 }
00039 
00040 void disableqdebug( QtMsgType type, const char *msg )
00041 {
00042     // Ignore messages that are sent via qDebug.
00043    Q_UNUSED( type );
00044    Q_UNUSED( msg );
00045 }
00046 
00047 int doqcopimpl (int argc, char *argv[])
00048 {
00049     qInstallMsgHandler( disableqdebug );
00050 
00051     if ( argc > 1 ) {
00052         QString opt = argv[1];
00053         if ( opt == "-l" ) {
00054             if ( argc < 5 ) {
00055                 doqcopusage();
00056                 exit(1);
00057             }
00058 #ifndef Q_OS_WIN32
00059             const char *username = argv[2];
00060             struct passwd *pwd = getpwnam( username );
00061             if ( !pwd ) {
00062                 fprintf( stderr, "Unknown user %s\n", username );
00063                 exit(1);
00064             }
00065             int uid =  pwd->pw_uid;
00066             int gid =  pwd->pw_gid;
00067             if ( initgroups( username, gid ) != 0 ) {
00068                 fprintf( stderr, "Could not chg group for user:%s\n", username );
00069                 exit(1);
00070             }
00071 
00072             if ( setuid( uid ) != 0 ) {
00073                 fprintf( stderr, "Could not run as user %s\n", username );
00074                 exit(1);
00075             }
00076             setenv( "LOGNAME", username, 1 );
00077 #else
00078             setenv("LOGNAME", argv[2], 1);
00079 #endif
00080             
00081             argc -= 2;
00082             for ( int i = 1; i < argc; i++ ) {
00083                 argv[i] = argv[i+2];
00084             }
00085         }
00086                       
00087     }
00088 
00089     if ( argc < 3 ) {
00090         doqcopusage();
00091         exit(1);
00092     }
00093 
00094     QApplication app( argc, argv );
00095     
00096     QString channel = argv[1];
00097     QString command = argv[2];
00098     command.stripWhiteSpace();
00099 
00100     int paren = command.find( "(" );
00101     if ( paren <= 0 )
00102         doqcopsyntax( "command", command );
00103 
00104     QString params = command.mid( paren + 1 );
00105     if ( params[(int)params.length()-1] != ')' )
00106         doqcopsyntax( "command", command );
00107 
00108     params.truncate( params.length()-1 );
00109 #ifndef QT_NO_COP
00110     int argIdx = 3;
00111     {
00112         QCopEnvelope env(channel.latin1(), command.latin1());
00113 
00114         QStringList paramList = QStringList::split( ",", params );
00115         QStringList::Iterator it;
00116         for ( it = paramList.begin(); it != paramList.end(); ++it ) {
00117             QString arg = argv[argIdx];
00118             if ( *it == "QString" ) {
00119                 env << arg;
00120             } else if ( *it == "int" ) {
00121                 env << arg.toInt();
00122             } else if ( *it == "QDateTime") {
00123                 env << TimeConversion::fromISO8601(QCString(arg));
00124             } else if ( *it == "bool") {
00125                 if (arg.lower() == "false")
00126                     env << (int)false;
00127                 else if (arg.lower() == "true")
00128                     env << (int)true;
00129                 else
00130                     doqcopsyntax( "parameter value for bool should be either 'true' or 'false'", arg );
00131             } else {
00132                 doqcopsyntax( "parameter type", *it );
00133             }
00134             argIdx++;
00135         }
00136         // send env
00137     }
00138 
00139     // Check for a "-w" option, which indicates that we should
00140     // wait for a QCop command before exiting.
00141     if ( argIdx < argc && QString(argv[argIdx]) == "-w" ) {
00142         if ( ( argIdx + 3 ) > argc ) {
00143             doqcopusage();
00144             exit(1);
00145         }
00146 
00147         channel = argv[argIdx + 1];
00148         command = argv[argIdx + 2];
00149 
00150         QCopWatcher *watcher = new QCopWatcher( &app, command );
00151         QCopChannel *chan = new QCopChannel( QCString(channel), &app );
00152 
00153         QObject::connect
00154             ( chan, SIGNAL(received(const QCString&,const QByteArray&)),
00155               watcher, SLOT(received(const QCString&,const QByteArray&)) );
00156         QObject::connect
00157             ( watcher, SIGNAL(done()), &app, SLOT(quit()) );
00158 
00159         if ( ( argIdx + 3 ) < argc ) {
00160             QTimer::singleShot( QString(argv[argIdx + 3]).toInt(),
00161                                 watcher, SLOT(timeout()) );
00162         }
00163 
00164         return app.exec();
00165     }
00166 
00167 #endif
00168 
00169     QTimer::singleShot( 0, &app, SLOT(quit()) );
00170     return app.exec();
00171 }
00172 
00173 QCopWatcher::QCopWatcher( QObject *parent, const QString& msg )
00174     : QObject( parent )
00175 {
00176     this->msg = msg;
00177 }
00178 
00179 QCopWatcher::~QCopWatcher()
00180 {
00181 }
00182 
00183 void QCopWatcher::received( const QCString& msg, const QByteArray& data )
00184 {
00185     if ( msg != this->msg )
00186         return;
00187 
00188     QString command = msg;
00189     QDataStream stream( data, IO_ReadOnly );
00190 
00191     int paren = command.find( "(" );
00192     if ( paren <= 0 )
00193         doqcopsyntax( "wait-command", command );
00194 
00195     QString params = command.mid( paren + 1 );
00196     if ( params[(int)params.length()-1] != ')' )
00197         doqcopsyntax( "wait-command", command );
00198 
00199     params.truncate( params.length()-1 );
00200     QStringList paramList = QStringList::split( ",", params );
00201     QStringList::Iterator it;
00202     for ( it = paramList.begin(); it != paramList.end(); ++it ) {
00203         if ( *it == "QString" ) {
00204             QString value;
00205             stream >> value;
00206             puts( value.latin1() );
00207         } else if ( *it == "int" ) {
00208             int value;
00209             stream >> value;
00210             printf( "%d\n", value );
00211         } else if ( *it == "QDateTime") {
00212             QDateTime value;
00213             QString converted;
00214             stream >> value;
00215             converted = TimeConversion::toISO8601(value);
00216             puts( converted.latin1() );
00217         } else if ( *it == "bool") {
00218             int value;
00219             stream >> value;
00220             if ( value )
00221                 puts( "true" );
00222             else
00223                 puts( "false" );
00224         } else {
00225             doqcopsyntax( "parameter type", *it );
00226         }
00227     }
00228 
00229     emit done();
00230 }
00231 
00232 void QCopWatcher::timeout()
00233 {
00234     QApplication::exit(1);
00235 }
00236 

Generated on Sat Nov 5 16:15:29 2005 for OPIE by  doxygen 1.4.2