00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include <qvariant.h>
00025 #include <qcolor.h>
00026 #include "../kdatastream.h"
00027 #include "../dcopclient.h"
00028 #include "../dcopref.h"
00029 #include <stdlib.h>
00030 #include <stdio.h>
00031 #include <ctype.h>
00032 
00033 #include "marshall.cpp"
00034 
00035 static DCOPClient* dcop = 0;
00036 static bool bAppIdOnly = 0;
00037 static bool bLaunchApp = 0;
00038 
00039 bool findObject( const char* app, const char* obj, const char* func, QCStringList args )
00040 {
00041     QString f = func; 
00042     int left = f.find( '(' );
00043     int right = f.find( ')' );
00044 
00045     if ( right <  left )
00046     {
00047     qWarning( "parentheses do not match" );
00048         exit(1);
00049     }
00050 
00051     if ( !f.isEmpty() && (left < 0) )
00052     f += "()";
00053 
00054     
00055     
00056     
00057     
00058     QStringList intTypes;
00059     intTypes << "int" << "unsigned" << "long" << "bool" ;
00060 
00061     QStringList types;
00062     if ( left >0 && left + 1 < right - 1) {
00063     types = QStringList::split( ',', f.mid( left + 1, right - left - 1) );
00064     for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
00065         QString lt = (*it).simplifyWhiteSpace();
00066 
00067         int s = lt.find(' ');
00068 
00069         
00070         
00071         
00072         
00073         
00074         
00075         if ( s > 0 )
00076         {
00077         QStringList partl = QStringList::split(' ' , lt);
00078 
00079         
00080         
00081         
00082         
00083         
00084         
00085             s=1;
00086 
00087         while (s < (int)partl.count() && intTypes.contains(partl[s]))
00088         {
00089             s++;
00090         }
00091 
00092         if (s<(int)partl.count()-1)
00093         {
00094             qWarning("The argument `%s' seems syntactically wrong.",
00095                 lt.latin1());
00096         }
00097         if (s==(int)partl.count()-1)
00098         {
00099             partl.remove(partl.at(s));
00100         }
00101 
00102             lt = partl.join(" ");
00103         lt = lt.simplifyWhiteSpace();
00104         }
00105 
00106         (*it) = lt;
00107     }
00108     QString fc = f.left( left );
00109     fc += '(';
00110     bool first = true;
00111     for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
00112         if ( !first )
00113         fc +=",";
00114         first = false;
00115         fc += *it;
00116     }
00117     fc += ')';
00118     f = fc;
00119     }
00120 
00121     if ( types.count() != args.count() ) {
00122     qWarning( "arguments do not match" );
00123     exit(1);
00124     }
00125 
00126     QByteArray data;
00127     QDataStream arg(data, IO_WriteOnly);
00128 
00129     uint i = 0;
00130     for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
00131         marshall(arg, args, i, *it);
00132     }
00133     if ( (uint) i != args.count() ) {
00134     qWarning( "arguments do not match" );
00135     exit(1);
00136     }
00137 
00138     QCString foundApp;
00139     QCString foundObj;
00140     if ( dcop->findObject( app, obj, f.latin1(),  data, foundApp, foundObj) )
00141     {
00142        if (bAppIdOnly)
00143           puts(foundApp.data());
00144        else
00145           printf("DCOPRef(%s,%s)\n", qStringToC(foundApp), qStringToC(foundObj));
00146        return true;
00147     }
00148     return false;
00149 }
00150 
00151 bool launchApp(QString app)
00152 {
00153     int l = app.length();
00154     if (l && (app[l-1] == '*'))
00155        l--;
00156     if (l && (app[l-1] == '-'))
00157        l--;
00158     if (!l) return false;
00159     app.truncate(l);
00160 
00161     QStringList URLs;
00162     QByteArray data, replyData;
00163     QCString replyType;
00164     QDataStream arg(data, IO_WriteOnly);
00165     arg << app << URLs;
00166 
00167     if ( !dcop->call( "klauncher", "klauncher", "start_service_by_desktop_name(QString,QStringList)",
00168                       data, replyType, replyData) ) {
00169     qWarning( "call to klauncher failed.");
00170         return false;
00171     }
00172     QDataStream reply(replyData, IO_ReadOnly);
00173 
00174     if ( replyType != "serviceResult" )
00175     {
00176         qWarning( "unexpected result '%s' from klauncher.", replyType.data());
00177         return false;
00178     }
00179     int result;
00180     QCString dcopName;
00181     QString error;
00182     reply >> result >> dcopName >> error;
00183     if (result != 0)
00184     {
00185         qWarning("Error starting '%s': %s", app.local8Bit().data(), error.local8Bit().data());
00186         return false;
00187     }
00188     return true;
00189 }
00190 
00191 void usage()
00192 {
00193    fprintf( stderr, "Usage: dcopfind [-l] [-a] application [object [function [arg1] [arg2] [arg3] ... ] ] ] \n" );
00194    exit(0);
00195 }
00196 
00197 
00198 int main( int argc, char** argv )
00199 {
00200     int argi = 1;
00201 
00202     while ((argi < argc) && (argv[argi][0] == '-'))
00203     {
00204        switch ( argv[argi][1] ) {
00205        case 'l':
00206             bLaunchApp = true;
00207             break;
00208        case 'a':
00209             bAppIdOnly = true;
00210             break;
00211        default:
00212             usage();
00213        }
00214        argi++;
00215     }
00216 
00217     if (argc <= argi)
00218        usage();
00219 
00220     DCOPClient client;
00221     client.attach();
00222     dcop = &client;
00223 
00224     QCString app;
00225     QCString objid;
00226     QCString function;
00227     char **args = 0;
00228     if ((argc > argi) && (strncmp(argv[argi], "DCOPRef(", 8)) == 0)
00229     {
00230        char *delim = strchr(argv[argi], ',');
00231        if (!delim)
00232        {
00233           fprintf(stderr, "Error: '%s' is not a valid DCOP reference.\n", argv[argi]);
00234           return 1;
00235        }
00236        *delim = 0;
00237        app = argv[argi++] + 8;
00238        delim++;
00239        delim[strlen(delim)-1] = 0;
00240        objid = delim;
00241     }
00242     else
00243     {
00244        if (argc > argi)
00245           app = argv[argi++];
00246        if (argc > argi)
00247           objid = argv[argi++];
00248     }
00249     if (argc > argi)
00250        function = argv[argi++];
00251 
00252     if (argc > argi)
00253     {
00254        args = &argv[argi];
00255        argc = argc-argi;
00256     }
00257     else
00258     {
00259        argc = 0;
00260     }
00261 
00262     QCStringList params;
00263     for( int i = 0; i < argc; i++ )
00264         params.append( args[ i ] );
00265     bool ok = findObject( app, objid, function, params );
00266     if (ok)
00267        return 0;
00268     if (bLaunchApp)
00269     {
00270        ok = launchApp(app);
00271        if (!ok)
00272           return 2;
00273        ok = findObject( app, objid, function, params );
00274     }
00275 
00276     return 1;
00277 }